WCF has suspended a call

I have a WCF service that implements a lengthy survey. However, I do not see the possibility that each service call invokes a new thread on the call.

In its current form, the long-awaited contract expects events and blocks any other contracts.

How is it recommended that one contract be executed asynchronously from another contract in WCF?

I was thinking about keeping a pool of static threads, but I'm not quite sure if this solution scales.

Thank!

+3
source share
1 answer

In the context of your question, I assume that a lengthy survey is some kind of operation that periodically issues an HTTP request to a third-party resource until the desired response is returned or until the timeout expires.

, .NET 4.5, TAP async/.

():

// contract

[ServiceContract]
public interface IService
{
    //task-based asynchronous pattern
    [OperationContract]
    Task<bool> DoLongPollingAsync(string url, int delay, int timeout);
}

// implementation

public class Service : IService
{
    public async Task<bool> DoLongPollingAsync(
        string url, int delay, int timeout)
    {
        // handle timeout via CancellationTokenSource
        using (var cts = new CancellationTokenSource(timeout))
        using (var httpClient = new System.Net.Http.HttpClient())
        using (cts.Token.Register(() => httpClient.CancelPendingRequests()))
        {
            try
            {
                while (true)
                {
                    // do the polling iteration
                    var data = await httpClient.GetStringAsync(url).ConfigureAwait(false);
                    if (data == "END POLLING") // should we end polling?
                        return true;

                    // pause before the next polling iteration
                    await Task.Delay(delay, cts.Token);
                }
            }
            catch (OperationCanceledException)
            {
                // is cancelled due to timeout?
                if (!cts.IsCancellationRequested)
                    throw;
            }
            // timed out
            throw new TimeoutException();
        }
    }
}

, DoLongPolling , HttpClient.GetStringAsync Task.Delay. , ThreadPool, WCF DoLongPolling . "No Thread" Stephen Cleary , .

WCF . " " - WCF " ".

.NET 4.0, " WCF" Jaliya Udagedara.

+1

All Articles