none
How to implement an async wcf client for a synchronous service RRS feed

  • Question

  • Is there a way to create an async client for a synchronous WCF service without adding a service reference? This is for a .NET 4 client.
    Monday, January 14, 2013 10:29 PM

Answers

  • Yeah but that still involves using service util to generate the client. We share the contract assembly and thus won't use service util or add service reference.

    You can always look at the code generated by the service utility to see how to generate your own async code.

    The other option is to just wrap it in a task, but that will be FAR less efficient since it's using a TP thread to handle the asynchrony.  Code for that would be something like:

    Task<int> GetNumberOfFoosAsync()
    {
        return Task.Factory.StartNew( () =>
        {
            return channel.GetNumberOfFoos();
        });
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 15, 2013 10:28 PM

All replies

  • You could write your own async client code.  Whether the client is async is really completely separate from whether the server is synchronous or not.

    Using a service reference is the simplest way, as it writes all of the plumbing for you -but you can write your own accessor the same way.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, January 14, 2013 11:09 PM
  • Hi Reed,

    My confusion lies in how to write the code to call WCF asynchronously.  I didn't see anything other than InvokeAsync() on ClientBase<TContract>.Channel.  Do you have a code snippet I can see?

    Tuesday, January 15, 2013 1:21 PM
  • Hi Reed,

    My confusion lies in how to write the code to call WCF asynchronously.  I didn't see anything other than InvokeAsync() on ClientBase<TContract>.Channel.  Do you have a code snippet I can see?

    It depends on which framework you're using, and which channel method it is.  If it's .NET 4.5, you'll get a method like:

    Task<int> GetNumberOfFoosAsync();

    Since this returns a Task or Task<T>, its inherently an async operation - you can just call it, and use await (or a task continuation) on the result.

    If you're using .NET 4 or earlier, you'll get an event + a method.  So, for GetNumberOfFoos(), you'll end up with something like:

    int result;
    // Event that fires when the async operation is done...
    channel.GetNumberOfFoosCompleted += (o, e) =>
    {
        result = e.Result;
    };
    
    channel.GetNumberOfFoosAsync(); // Start the operation

    For details, see: http://msdn.microsoft.com/en-us/library/ms730059.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 15, 2013 6:05 PM
  • Yeah but that still involves using service util to generate the client. We share the contract assembly and thus won't use service util or add service reference.
    Tuesday, January 15, 2013 6:19 PM
  • Yeah but that still involves using service util to generate the client. We share the contract assembly and thus won't use service util or add service reference.

    You can always look at the code generated by the service utility to see how to generate your own async code.

    The other option is to just wrap it in a task, but that will be FAR less efficient since it's using a TP thread to handle the asynchrony.  Code for that would be something like:

    Task<int> GetNumberOfFoosAsync()
    {
        return Task.Factory.StartNew( () =>
        {
            return channel.GetNumberOfFoos();
        });
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 15, 2013 10:28 PM