25

I have a WCF service that I call from a windows service.

The WCF service runs a SSIS package, and that package can take a while to complete and I don't want my windows service to have to wait around for it to finish.

How can I make my WCF service call asynchronous? (or is it asynchronous by default?)

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

4 Answers4

27

All your needs will be satisfied in the following articles from MSDN:

Implementing an Async Service Operation

Calling WCF Service Async

Designing Service Contracts

Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
  • If I write down down [OperationContractAttribute(AsyncPattern=true)] , then implementation work as async if not mentioned then same code run as Sync ? Am I right ? – Ajay Sharma Aug 24 '16 at 06:45
13

On Visual Studio 2010, on the Add Service Reference > click Advanced button > check the Generate Asynchronous Operations checkbox.

After doing so, the Async operations will be added and be available for your use.

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
Bahamut
  • 1,929
  • 8
  • 29
  • 51
11

Service side:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    bool DoWork(int i);
}

Client side:

[ServiceContract(Name = nameof(IMyService))]
public interface IMyServiceClient : IMyService
{
    [OperationContract]
    Task<bool> DoWorkAsync(int i);
}
Stas BZ
  • 1,184
  • 1
  • 17
  • 36
  • 2
    A lot of answers to this question are from before 2012. This is the modern way to do it. – Greg Jan 21 '18 at 14:35
  • Exactly what I was looking for. This is a neat solution for "shared interfaces" and older project, that aren't async from the start :) Please note, that you have to change the configuration name aswell, in case you want to use the shared interface Name in your app.config file :) – Jannik Jan 18 '19 at 07:35
  • I am finding difficulty in finding a simple complete example of WCF Rest service async calls. – Ziggler Feb 15 '19 at 19:09
  • 1
    @Ziggler see https://marcinjuraszek.com/2018/01/async-wcf-with-channelfactory.html – mBardos Apr 04 '22 at 10:38
2

the WCF Proxy inside of your client (Windows Service?) needs to be specified at creation that you wish to have Asynchronous operations available.

You can modify an existing WCF Proxy by right clicking on it and choosing 'Configure Service Reference' From here you just need to check the tick box next to 'Generate asynchronous operations'

Ok so that is the WCF Proxy side of things taken care of. Now you need to use APM (Asynchronous Programming Model) with the Proxy inside of your client.

Andrew Harry
  • 13,773
  • 18
  • 67
  • 102