Fire and Forget WCF Service

I have a bunch of WCF REST services on Azure. In some WCF services, I call Http requests (e.g. send emails / sms) to external services. HTTP requests to third-party services that are not critical . I do not want this to block my response to a client call. You need to be guided by the template that will be used in this case. This is for fire and forget the pattern in my wcf service.

Pseudocode for 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(); } 

The entire context mode of the wcf service instance is configured on PerCall.

 InstanceContextMode = InstanceContextMode.PerCall 

There are several services that send email and SMS. I do not want these services to be blocked until the SMS requests are returned.

I read the literature and developed three methods

  • Fire and Forget with ThreadPool.QueueUserWorkItem - Have I seen some restrictions on the number of threads plus if all my wcf PerCall calls are safe in QueueUserWorkItem?

  • You have a separate WCF REST service with the oneway option enabled for the SendSMS function, which in turn makes a web request to external services (although this seems like a hack).

  • Use BeginInvoke without EndInvoke

Finally, is there anything Azure that I am missing. Any pointers in the right direction would be appreciated.

+4
source share
3 answers

See one-way contracts . Wrap your call to an external service with a service that displays this attribute.

+2
source

If you call them asynchronous calls, you must fire them and forget. I would think that this would be the easiest way. Call BeginInvoke. However, any of these methods should work fine. Azure has nothing specific to deal with.

+1
source

If you are comfortable using a third-party RestSharp control, you will have the async method to execute the request, as shown below:

  var client = new RestClient(); client.BaseUrl = serviceBaseUrl; var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = resourceUrl }; request.AddParameter("text/xml", requestBody, ParameterType.RequestBody); client.ExecuteAsync(request, callback); 

The callback method can be used if you need some notification later.

If you do not want to use a third-party dll, you can use the HttpClient object, as shown below:

  var client = new HttpClient(); client.DefaultHeaders.ContentType = "application/json"; HttpRequestMessage req = new HttpRequestMessage("GET", ""); client.SendAsync(req); 

The HttpClient namespace is Microsoft.Http

+1
source

Source: https://habr.com/ru/post/1398822/


All Articles