4

I have .net 4.5 WCF service. I will rewrite the service implementation to use EF6 to access DB. This service has many clients and handle many calls. I cannot change service contract and clients. Does it make sense to use Async EF operations like SaveAsync, at the end my service must return T not Task (becouse of the old clients).

Update

Exaple operation contract

[OperationContract]
public object AddEntity(object entity)
{
    using(var context = new MyContext())
    {
        context.Add(entity)
        var task = context.SaveChangesAsync()

        return task.Result;
    }
}

So even I am usining async the thread will be block by task.Result. I cannot use await becouse than the operation contract must be changed to return Task. How to implement such scenario?

Konrad
  • 135
  • 8

2 Answers2

4

Does it make sense to use Async EF operations like SaveAsync, at the end my service must return T not Task (becouse of the old clients).

Yes, it does make sense and will boost scalability of your WCF service. You don't have to change the contracts to take advantage of async APIs on the server side. The conversion can be totally transparent to the client, check this:

Different forms of the WCF service contract interface

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Please could you review my sample? what i am missing? how to implent async operation here? – Konrad Jun 23 '14 at 12:51
  • 1
    @KonradSikorski, you can use the code posted by Guillaume. You don't need to change or even recompile the client code. Note that asynchrony on the client and on the server are totally independent from each in this case. – noseratio Jun 23 '14 at 13:01
1
[OperationContract]
public async Task<object> AddEntityAsync(object entity)
{
    using(var context = new MyContext())
    {
        context.Add(entity)
        return await context.SaveChangesAsync();
    }
}

WCF should detect the async method and will route calls properly with no modification required on client side.

I guess that SaveChangesAsync returns a Task and not a Task<object> but you know what to do ;)

Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • shouldn't I use context.SaveChangesAsync().ConfigureAwait(false)? – Konrad Jun 23 '14 at 13:46
  • @KonradSikorski, it depends on your environment and what you are doing after SaveChangesAsync(). If you do nothing or don't use the context I think ConfigureAwait(false) is ok. – Guillaume Jun 26 '14 at 12:54