lets assume that we have two operation contracts defined on wcf service, sync and async one. There are two samples:
public void SubscribeSingle(int userId)
{
var clientId = this.OperationContext.GetClientId();
var session = this.OperationContext.GetPollingDuplexSession();
if (string.IsNullOrEmpty(clientId) || session == null || userId == 0)
{
return;
}
this.InternalSubscribeSingle(userId, clientId, session.SessionId);
}
and
public IAsyncResult BeginUnsubscribeSingle(int userId, AsyncCallback callback, object state)
{
var clientId = this.OperationContext.GetClientId();
var session = this.OperationContext.GetPollingDuplexSession();
if (string.IsNullOrEmpty(clientId) || session == null)
{
return null;
}
var asyncResult = new VoidAsyncResult(callback, state);
Task.Factory.StartNew(() =>
{
try
{
this.InternalUnsubscribeSingle(userId, clientId);
asyncResult.SetAsCompleted(false);
}
catch (Exception ex)
{
asyncResult.SetAsCompleted(ex, false);
}
});
return asyncResult;
}
public void EndUnsubscribeSingle(IAsyncResult result)
{
var response = result as VoidAsyncResult;
if (response != null)
{
response.EndInvoke();
}
}
As I understand WCF service also has a thread pool inside, so each I/O operation may be finished on another thread. As well as starting new thread using Task.Factory.StartNew
Is there any difference between sync and async server calls from performance point of view, if database access is made through EntityFramework and though blocking?
There’s no value to the async method here, since you’re still calling a sync method that will block a thread at some point. The async only earns something when it goes all the way down to a non-blocking operation like disk/network IO. Just write the simple sync version and let WCF do its thing.