Push Data to the Client using WCF CallBack Service
Posted by: Mahesh Sabnis
in Category Windows Communication Foundation (WCF)
Abstract: In WCF, callback is used to implement PUSH mechanism, so that delayed or long running operation results can be automatically pushed back to the client application. WCF actively supports callback to its client, over the instance context established. In this article, we will explore the same.
Many a times, the requirement demands that the WCF Service pushes data to the application that is consuming it, over the session established between client application and the WCF service. This mechanism can be termed as Callback. WCF actively supports callback to its client, over the instance context established. To understand this scenario, think of it like the WCF service becomes the client and the client becomes the service.
BasicHttpBinding and WSHttpBinding do not support callback. To support callback over HTTP transport, WCF provides WSDualHttpBinding. The WSDualHttpBinding sets up two channel - one for calls from client to service and other from service to client. Whenever you create a Service, it can have only one callback contract. When a proxy is created for the Service with CallBack, the base class of the proxy is the DuplexClientBase<T>. This is used to create channels for the duplex service and this channel is the associated with the callback object.
Enough of theory, let’s create a WCF service, Host and Client callback.
Step 1: Open VS2010 and create a Blank solution, name it as ‘WCF_Callback_Contract’. In this solution, add a new WCF Service application and name it as ‘WCF_CallBack_Service’. Rename IService1.cs to Iservice.cs and Service1.svc to Service.svc.
Step 2: Open IService.cs and add the following ServiceContract, Callback contract interface and a DataContract, as shown below:
There are some important points to be noted here:
- The method in the interface participating in Call-Back must be OneWay, so that there is no immediate response generated from the method. E.g. GetData() method in IService
- The interface acting as Callback must also define OneWay method. E.g. IServiceCallback.
In the above code, since IServiceCallback interface is set as a CallbackContract of the IService service contract, it will automatically serialize through the proxy, to the client application. The client application can then subscribe to this interface, to receive callback from the WCF service.
Step 3: Open Service.svc.cs and implement the IService in the service class as shown below:
Step 4: Open Web.Config file and write the following configuration as shown below. Here we are using WSHttpBinding for Callback over Http transport.
Step 5: Publish and Host the service on IIS.
Step 6: In the solution created in Step 1, add a new WinForm project and name it as ‘Windows_Client_WCF_CallBack’. Add the WCF service reference in it. If you observe the proxy class, the base class for the proxy will be ‘DuplexClientBase<T>’. Along with this, you will also find the IServiceCallback, the callback contract in the proxy.
Step 7: Now this is the most important step. We need to implement the IServiceCallback contract in the client application so that the Callback from the WCF service can be received in the client. Here the client application acts as service and service acts as a client. In the client application, add a new class file and name it as ‘CallBackClass.cs’. Write the following code in it:
The above code implements IServiceCallback interface and implements its SendResult() method. This method displays count of the department in the department array object.
Step 8: Design Form1.cs as shown below using aButton and DataGridView:
Step 9: Open Form1.cs (the code behind) and declare the following object at Form1 class level:
Step 10: Write the following code in the Form Loaded event:
The above code creates an instance of the RequestCallBack class created above in Step 7. Using this object, the context information of the service instance is acquired using InstanceContext class, then this context is passed to the Proxy object so that the WCF service can automatically send the request to the client application.
Step 11: Write the following code in the click event of the Get Data button. This is a very simple piece of code for making call to the WCF service:
Step 12: Run the application and click on the Get Data button. The result will be as shown below:
The dialog-box shows that the value is posted from client to the WCF service. After a second, the result shown below will be displayed indicating the Service has made a callback to the client:
The ‘Response Received 3’ in the callback specifies the number of Department objects in Department array. Click OK on both the dialog-box and the Department data will be displayed in the DataGridView as below:
Conclusion: In WCF, callback is used to implement the PUSH mechanism, so that delayed or long running operation results can be automatically pushed back to the client application.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!