How to make a call to my WCF service asynchronous?
-
03-07-2019 - |
Question
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?)
Solution
All your needs will be satisfied in the following articles from MSDN:
OTHER TIPS
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.
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);
}
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.