I have a bunch of WCF REST services on Azure. Inside some of the WCF services I invoke Http requests (say to send Email / sms) to external services. The http requests to the third party services that are non-critical. I do not want that to be blocking my response to the client call. Need some guidance on the pattern to be used in this case. This is for a fire and forget pattern within my wcf service.
Pseudo code for the WCF service
[WebInvoke(UriTemplate = "process", Method = "POST")]
[OperationContract]
public bool DoSomeProcessing(DataEntity data)
{
bool result = ProcessData(data);
//I do not want this call to block
SendSMS();
}
//Call to third party
public void SendSMS()
{
HttpWebRequest objWebRequest = null;
HttpWebResponse objWebResponse = null;
objWebRequest = (HttpWebRequest)WebRequest.Create(url);
objWebRequest.Method = "GET";
//I do not want to wait for this response since often the web requests takes a bit of time.
//objWebResponse = (HttpWebResponse)objWebRequest.GetResponse();
}
All my wcf services instance context mode are set to PerCall.
InstanceContextMode = InstanceContextMode.PerCall
There are some services that send Email and SMS. I do not want these services to block until the SMS calls return.
I read up on the literature and three methods stood out
Fire and Forget using ThreadPool.QueueUserWorkItem - I am saw some limits on the number of threads plus if all my wcf calls are PerCall would QueueUserWorkItem be safe?
Have a separate WCF REST service with the oneway option enabled for the SendSMS function that in turn make s the web request to the external services(although this seems like a hack).
Use BeginInvoke without EndInvoke
Finally, is there anything Azure specific that I am missing. Any pointers in the right direction would be appreciated.