I have a WCF service client and it was generated using the /async
argument with svcutil.exe
, so it uses the asynchronous programming model. When I make multiple asynchronous requests with a single client though it serializes the execution of those requests.
Here is an example that shows how I am executing the parellel requests.
[TestMethod]
public void AsyncTest()
{
var req1 = Helpers.Request["Symbol"];
var req2 = Helpers.Request["Summary"];
using(var client = new SoapClientAsync(_TcpBinding, _TestRunConfig.ServiceAddress))
{
IAsyncResult[] results = new[]
{
client.BeginExec(req1, new AsyncCallback(asyncComplete), new MyAsyncState { Client = client, Request = req1 }),
client.BeginExec(req2, new AsyncCallback(asyncComplete), new MyAsyncState { Client = client, Request = req2 })
};
WaitHandle[] waits = results.Select(i => (MyAsyncState)i.AsyncState).Select(i => i.WaitEvent).ToArray();
WaitHandle.WaitAll(waits);
var states = results.Select(i => (MyAsyncState)i.AsyncState);
foreach (var state in states)
{
Console.WriteLine(string.Format("client {0}", state.Watch.ElapsedMilliseconds));
Console.WriteLine(string.Format("server {0}", state.Response.ExecutionSummary.Single(i => i.Message == "Total").Duration));
}
}
}
private void asyncRequestComplete(IAsyncResult ar)
{
TestAsyncState state = (MyAsyncState)ar.AsyncState;
state.Response = state.Client.EndExec(ar);
state.Watch.Stop();
state.WaitEvent.Set();
}
Here is the output that I am seeing:
client 26
server 24client 53
server 26
The server is reporting a consistent execution time of ~25ms. But it is pretty obviously serializing the execution of each request on the client side.
What is the proper way to execute parellel requests against a web service endpoint using WCF?