Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

c# - WCF suspended call

I have a WCF service that implements long-polling. However, I see no way to have each service call spawn a new thread upon being called.

As it stands, the long-polled contract is waiting for an event to occur and is blocking any other contracts from being called.

What is the recommended way to have one contract run asynchronously from another contract in WCF?

I thought of keeping a static thread pool but I'm not quite sure if that solution scales.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the context of your question, I assume long-polling is some kind of an operation which is periodically issuing an HTTP request to a 3rd party resource, until a desired response has been returned, or until timed out.

To implement it efficiently, you can use .NET 4.5, TAP pattern and async/await.

Example (untested):

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

This scales well, because most of the time the DoLongPolling is in the pending state, asynchronously awaiting the result of HttpClient.GetStringAsync or Task.Delay calls. Which means it doesn't block a thread from ThreadPool, so the WCF service can serve more DoLongPolling requests concurrently. Check "There Is No Thread" by Stephen Cleary for more details on how this works behind the scene.

On the client side, you can call your WCF service asynchronously, too. Tick "Allow generation of asynchronous operations" when you create the WCF service proxy and select "Generate task-based operations".

If you need to target .NET 4.0, you can find some alternative options in "Asynchronous Operations in WCF" by Jaliya Udagedara.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...