dot net learner
dot net learner

Reputation: 155

WCF Async Operation + IO Operation

What is the advantage of writing the following WCF service operation using Async CTP?

Task.Factory.StartNew will anyway block the threadpool thread for the duration of the longRunningIOOperation?

    public Task<string> SampleMethodAsync(string msg)
    {
        return await Task.Factory.StartNew(() =>
        {
            return longRunningIOOperation();
        });
    }

Is there a better way to write this so we take advanage of IO completion threads?

Upvotes: 1

Views: 456

Answers (2)

dot net learner
dot net learner

Reputation: 155

Finally I figured out how this works. I installed .net FX4.5 and everything worked like a charm.

In my scenario, Service A makes a call to Service B like this.

public class ServiceA : IServiceA
{
    public async Task<string> GetGreeting(string name)
    {
        ServiceBClient client = new ServiceBClient();
        return await client.GetGreetingAsync();
    }
}

client.GetGreetingAsync() takes 10 seconds to process. My understading is Service A request thread will not be blocked by calling GetGreetingAsync().

Can you explaing how this is implemented by WCF behind the scenes or point me to some documentation to understand how all this works from the perspective of WCF?

Upvotes: 0

carlosfigueira
carlosfigueira

Reputation: 87228

You'll need to make the longRunningIOOperation an asynchronous operation as well. As long as any operation in your code blocks the thread, some thread will be blocked, whether it's a threadpool one or the one in which your operation was called. If your operation is asynchronous, you can write something similar to the code below.

public Task<string> SampleMethodAsync(string msg)
{
    var tcs = new TaskCompletionSource<string>();
    longRunningIOOperationAsync().ContinueWith(task =>
    {
        tcs.SetResult(task.Result);
    });
    return tcs.Task;
}

Upvotes: 1

Related Questions