How to Customize the API variant generated for the added WCF Service Reference



 We must have seen that when we try to add a WCF service reference in a client project two different versions of the API's in the service is generated. One is the Synchronous variant and the other one is the Asynchronous variant of the same method in the Service that we are referring to.

Before going to how we can customize the API generated, let us discuss about the need of Asynchronous service API. 

Why do we need to have Asynchronous API variant? 

Asynchronous Service API's allows us to do CPU intensive operation/operation which takes lots of time such as database query etc. in a separate thread so that the application's Main thread will be freed and the application will not freeze.

Choosing the type of API variant in the Client Side

We can choose to generate the API variant that we require through Service Reference Settings in the client side. For that

  • Open the Service Reference Dialog by clicking on the Reference->Add Service Reference.

Adding Service Reference


  • Once the Service Reference Dialog opens up there is a button for Advanced Settings as shown below:
Advanced settings in Service Reference


  • Click on the Advanced Button then the dialog for the advanced settings comes up where we can choose the variant of the Service APIs that we want to generate as shown below:
API generation mode


As shown in the figure there is a check box we have to check to generate Asynchronous Version of API.

Suggested Reads : Duplex Contract In WCF

Also there are two flavor's of the asynchronous API that can be generated once is the task based and the other one is the asynchronous based.

Task Based API's allows using new way of doing async operation that is using Task and async await pattern. The service method returns a Task on which we can do await.

The other flavor "generate asynchronous operation" generates API's which takes in call backs which gets triggered once the server API completes its operation.

Better is to go with the first one which is "generate task-based operation". However this will be checked by default. 

Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Step By Step Guide to Detect Heap Corruption in Windows Easily

How to dynamically add Properties in C# .NET?