SELECT * FROM sys.Procedures WHERE is_ms_shipped = 0
AND OBJECT_DEFINITION(object_ID) LIKE '%NOEXPAND%'
Place to share experiences while developing applications.
public IEnumerable Query(object key)
{
return context.Query(key);
}
|
[Serializable]
public class MessageDTO
{
public MessageDTO(){}
public Guid MessageGUID{ set; get; }
public string To { set; get; } //Phone number to whom message need to send
public string Subject { set; get; }
public string Body { set; get; }
public string From { set; get; } //Phone number from whom message need to send
public DateTime CreatedOn { set; get; }
public DateTime SentOn { set; get; }
}
public class Utility
{
private static string smsOutQueuePath = ".\private$\smsoutq";
private static string smsInQueuePath = ".\private$\smsinq";
private static Message SetMessageProperties(Message objMsg)
{
objMsg.Recoverable = true;
objMsg.UseDeadLetterQueue = true;
objMsg.UseJournalQueue = true;
objMsg.Formatter = new BinaryMessageFormatter();
return objMsg;
}
public static MessageQueue GetMessageQueue(string queuePath)
{
MessageQueue msgQ = null;
try
{
if(!MessageQueue.Exists(queuePath))
{
msgQ = MessageQueue.Create(queuePath);
}
msgQ = new MessageQueue(queuePath, QueueAccessMode.SendAndReceive);
msgQ.Formatter = new BinaryMessageFormatter();
msgQ.DefaultPropertiesToSend.UseJournalQueue = true;
msgQ.DefaultPropertiesToSend.Recoverable = true; //Messages remain if server goes down
msgQ.DefaultPropertiesToSend.UseDeadLetterQueue = true;
msgQ.MessageReadPropertyFilter.SetAll();
}
catch (Exception ex)
{
//your exception logging code
}
return msgQ;
}
public static bool PostToSMSQueue(MessageDTO objMsg, string direction = Constants.MESSAGE_DIRECTION_OUT)
{
bool success = false;
try
{
Message objMessage = new Message();
objMessage = SetMessageProperties(objMessage);
objMessage.Body = objMsg;
MessageQueue msgQ = null;
if(direction == Constants.MESSAGE_DIRECTION_OUT)
msgQ = GetMessageQueue(smsOutQueuePath);
else
msgQ = GetMessageQueue(smsInQueuePath);
msgQ.Send(objMessage);
success = true;
}
catch (Exception ex)
{
//your exception logging code
}
return success;
}
public static NameValueCollection ConvertUrlStringToNSCollection(string strUri)
{
NameValueCollection nsCol = new NameValueCollection();
if(!String.IsNullOrEmpty(strUri))
{
try
{
int endPos = 0;
string[] messageArray = null;
messageArray = strUri.Split('&');
for(int count = 0; count < messageArray.Length; count++)
{
endPos = messageArray[count].LastIndexOf('=');
nsCol.Add(messageArray[count].Substring(0, endPos),
messageArray[count].Substring(endPos + 1, messageArray[count].Length - (endPos + 1)));
}
}
catch(Exception ex)
{
throw ex;
}
}
return nsCol;
}
}
public partial class OutboundMsgPoller : ServiceBase
{
private static string smsOutQueuePath = ".\private$\smsoutq";
private static string accountSID = string.Empty;
private static string authToken = string.Empty;
private static string callbackURL = string.Empty;
private Thread m_oPollingThread = null;
private bool shallRun = false;
protected override void OnStart(string[] args)
{
try
{
this.EventLog.WriteEntry("OutboundMessagePoller service started successfully", EventLogEntryType.Information);
shallRun = true;
if (m_oPollingThread == null)
m_oPollingThread = new Thread(SendMessage);
m_oPollingThread.IsBackground = false;
m_oPollingThread.Start();
}
catch (Exception ex)
{
//your logging code
}
}
protected override void OnStop()
{
this.EventLog.WriteEntry("OutboundMessagePoller service is OnStop.", EventLogEntryType.Information);
}
private void SendMessage()
{
IList lstMsg = new List();
int pollInterval = 0;
MessageQueue queue = new MessageQueue(smsOutQueuePath, QueueAccessMode.SendAndReceive);
accountSID = "Your Account ID";
authToken = "Your Auth Token";
callbackURL = string.Empty;//in case you want to collect the message status then you can specify a url for your application where TWILIO can POST the status.
do
{
try
{
using (MessageEnumerator messageEnumerator = queue.GetMessageEnumerator2())
{
while (messageEnumerator.MoveNext(new TimeSpan(0, 0, 1)))
{
Message msg = queue.ReceiveById(messageEnumerator.Current.Id, new TimeSpan(0, 0, 1));
lstMsg.Add(msg);
}
}
queue.Close();
if (lstMsg != null && lstMsg.Count > 0)
{
//for OutboundMsgPoller service
this.SendMessageToTwilio(lstMsg, accountSID, authToken, callbackURL);
//for ReponseProcessor service, you have to write your own method
YourMethod(lstMsg);
}
lstMsg.Clear();
//Wait...
{
pollInterval = 30000; //time in milliseconds, can be made configurable
}
Thread.Sleep(pollInterval);
}
catch (Exception ex)
{
shallRun = false;
//your logging code
}
} while (shallRun);
}
private void SendMessageToTwilio(IList objMessage, string accountSID, string authToken, string callbackURL)
{
try
{
if (objMessage != null && objMessage.Count > 0)
{
// instantiate a new Twilio Rest Client
var client = new TwilioRestClient(accountSID, authToken);
// iterate over message
foreach (Message ms in objMessage)
{
ms.Formatter = new BinaryMessageFormatter();//this is important
if (ms.Body != null)
{
var objTw = client.SendSmsMessage(((MessageDTO)ms.Body).From, ((MessageDTO)ms.Body).To, ((MessageDTO)ms.Body).Body, callbackURL);
if (objTw.RestException != null)// this is important
{
throw new Exception("SendMessageToTwilio - Twilio Error Code - " + objTw.RestException.Code + "|| Error Message - " + objTw.RestException.Message);
}
else
{
//Update the SMSID received from TWILIO in your DB or other appropriate repository.
}
}
}
}
}
catch (Exception ex)
{
//your logging code
}
}
}
[Serializable]// this is important
public class TwilioRespDTO
{
public TwilioRespDTO(){}
public string SmsSid { get; set; }
public string AccountSid { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Body { get; set; }
}
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If the service is renamed, remember to update the global.asax.cs file
public class Responder
{
[WebInvoke(UriTemplate = "response/sms", Method = "POST")]
//IMPORTANT: response/sms is the part of the uri you will register with Twilio account for getting responses.
public void GetTwilioSMSResponse(Stream sr)
{
if (sr != null)
{
try
{
string postData = string.Empty;
NameValueCollection nsCol = new NameValueCollection();
using (StreamReader srt = new StreamReader(sr))
{
postData = srt.ReadToEnd();
}
nsCol = Utility.ConvertUrlStringToNSCollection(postData);
if (nsCol != null && nsCol.Count > 0)
{
TwilioResponseDTO objTw = new TwilioResponseDTO();
objTw = CreateTwilioResponseObject(nsCol);
Utility.PostToSMSQueue(objResp, Constants.MESSAGE_DIRECTION_IN);
}
}
catch (Exception ex)
{
//your error handling code here
}
}
}
private TwilioResponseDTO CreateTwilioResponseObject(NameValueCollection nsCol)
{
MethodInfo objMethod = (MethodInfo)MethodBase.GetCurrentMethod();
TwilioResponseDTO objTw = new TwilioResponseDTO();
objTw.SmsSid = nsCol.Get("SmsId");
objTw.AccountSid = nsCol.Get("AccountId");
objTw.From = HttpUtility.UrlDecode(nsCol.Get("From"));
objTw.To = HttpUtility.UrlDecode(nsCol.Get("To"));
objTw.Body = nsCol.Get("Body");
return objTw;
}
}