Reputation: 33
I have a scenario where I need to invoke multiple WCF asynchronous calls that can proceed independently of each other, with each one having its own continuation for updating the UI.
Lets say I have a function like this:
private Task<bool> ExecuteSomeWorkAsync(int delay)
{
int _delay = delay;
var t = Task<bool>.Factory.StartNew(
() =>
{
Thread.Sleep(_delay);
return true;
});
return t;
}
If I create a local WPF application that makes several calls, each of which can vary in time to return a result, it works as expected. In the example the second task is completed before the first and the UI element that is bound to the second property is updated before the first one.
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
await Task.WhenAll(
ExecuteSomeWorkAsync(2000).ContinueWith(task1 => { SomePropertyBoundToUIElement1 = task1.Result; }, uiScheduler),
ExecuteSomeWorkAsync(1000).ContinueWith(task2 => { SomePropertyBoundToUIElement2 = task2.Result; }, uiScheduler)
);
If however I try to implement this in WCF where ExecuteSomeWorkAsync is called via client proxy, then the UI is updated only after the slowest Task completes.
So, is there a way to make this work in WCF ?
Upvotes: 1
Views: 78
Reputation: 33
To answer my own question, there is a way to make the example work in WCF. The solution is to specify an adequate ConcurrencyMode option.
By decorating the WCF service with this service behavior, I get the parallelism I was after.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
Apparently the WCF default is Single ConcurrencyMode, so the service resolved the requests sequentially.
Upvotes: 1