7

Given this interface

[ServiceContract]
public interface IProductService
{
    [OperationContract]
    Product Get(int id);
}

I would like to manually (that is, without using scvutil or Add Service Reference in VS) create a client-side proxy.

I do it in the following way

public class ProductService: IProductService
{
    readonly ChannelFactory<IProductService> factory;

    public ProductService()
    {
        factory = new ChannelFactory<IProductService>("*");
    }

    public Product Get(int id)
    {
        var channel = factory.CreateChannel();
        return channel.Get(id);
    }
}

My problem is that I also want async/await version of this method, only on client-side, server side is still synchronous.

I want this to be a generic solution because I have many methods and services of this sort.

2
  • 1
    Wouldn't it be better to use async all the way starting at the server rather than using the inevitable Task.Run() or FromResult()?
    – user585968
    Mar 29, 2016 at 8:10
  • It would of course, the only reason not to is high cost :)
    – Elad
    Mar 29, 2016 at 8:17

2 Answers 2

9

If you're using ChannelFactory to allow for async-await your interface needs to return a Task or Task<T>.

It will force your server side to also return a task but you can do that synchronously with Task.CompletedTask and Task.FromResult if you insist on keeping it synchronous (though why would you if you have the option).

For example:

[ServiceContract]
interface IProductService
{
    [OperationContract]
    Task<Product> GetAsync(int id);
}

class ProductService : IProductService
{
    ChannelFactory<IProductService> factory;

    public ProductService()
    {
        factory = new ChannelFactory<IProductService>("*");
    }

    public Task<Product> GetAsync(int id)
    {
        var channel = factory.CreateChannel();
        return channel.GetAsync(id);
    }
}

class ProductAPI : IProductService
{
    public Task<Product> GetAsync(int id) => Task.FromResult(Get(id))
}
4
  • 1
    And when calling the operation from the client side would immediately free the calling thread and reclaim it when the task finishes? (that is, is it "truly" async?)
    – Elad
    Mar 29, 2016 at 8:19
  • 2
    @Elad yes. ChannelFactory supports async natively. It doesn't create a fake Task over a blocking operation.
    – i3arnon
    Mar 29, 2016 at 8:30
  • Which of those two classes is intended to be the client?
    – StingyJack
    Apr 27, 2017 at 10:50
  • @StingyJack "Which of those two classes is intended to be the client?" ProductService is the service, ProductAPI is the client.
    – MikeZ
    Dec 1, 2017 at 13:47
4

You can actually do that without changing the service itself. You can simply define a second interface which contains async and Task returning versions of the methods and is marked with [ServiceContract(Name = "NameOfTheIterfaceWhichIsActuallyExposedOnTheServer")]

In the example you mentioned you would define a second interface with GetAsync() operation:

[ServiceContract(Name = "IProductService")]
public interface IProductServiceAsync
{
    [OperationContract]
    Task<Product> GetAsync(int id);
}

and even though your service still implements and exposes IProductService you can use ChannelFactory<IProductServiceAsync> to call into it. As long as the method names match the GetFoo/GetFooAsync pattern everything will just work. That's how Add Service Reference in Visual Studio can generate you an async service reference to a synchronous service.

See Calling a synchronous WCF method asynchronously using ChannelFactory for a more detailed explanation on how this work.

1
  • What if my service has hundreds of methods? Is there an automatic and programmatic way to do so?
    – Boiethios
    Jan 28, 2020 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.