Invoking asynchronous call in a C# web service

21,513

Solution 1

If the 3rd party component does not support the standard asynchronous programming model (i.e it does not use IAsyncResult), you can still achieve synchronization using AutoResetEvent or ManualResetEvent. To do this, declare a field of type AutoResetEvent in your web service class:

AutoResetEvent processingCompleteEvent = new AutoResetEvent();

Then wait for the event to be signaled after calling the 3rd party component

// call 3rd party component
processingCompleteEvent.WaitOne()

And in the callback event handler signal the event to let the waiting thread continue execution:

 processingCompleteEvent.Set()

Solution 2

Assuming your 3rd party component is using the asynchronous programming model pattern used throughout the .NET Framework you could do something like this

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.stackoverflow.com");
    IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(null, null);

    asyncResult.AsyncWaitHandle.WaitOne();

    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
    using (StreamReader responseStreamReader = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        string responseText = responseStreamReader.ReadToEnd();
    }

Since you need your web service operation to block you should use the IAsyncResult.AsyncWaitHandle instead of the callback to block until the operation is complete.

Share:
21,513
Admin
Author by

Admin

Updated on January 30, 2020

Comments

  • Admin
    Admin over 4 years

    I'm consuming a third-party resource (a .dll) in a web service, and my problem is, that invoking this resource (calling a method) is done asynchronous - I need to subscribe to an event, to get the answer for my request. How do I do that in a c# web service?

    Update:

    With regard to Sunny's answer:

    I don't want to make my web service asynchronous.

  • Adrian Clark
    Adrian Clark over 15 years
    MSDN Link with more info and another example: msdn.microsoft.com/en-us/library/ms228962.aspx