In duplex contracts when client initiates an operation the server service provides a reference callback Uri back to the client. So the client initiates a call using the proxy class and when serverfinishes its work it notifies the client using the callback channel. This is called as duplexmessaging in WCF. If you remember in the previous question, we had no way to know when theserver finished its task.
Figure: - Duplex Service code
Let us try to understand the duplex concept by doing a small sample. The code snippet is asshown in the above figure. We will extend the previous sample, which was shown in the firstquestion only that now we will provide the notification to the client once the doHugeTask iscompleted.
The first step is to change the service class. Above is the code snippet of the service class. Again,we have numbered them so that we can understand how to implement it practically line by line.So below is the explanation number by number:-
1 - In the service contract attribute we need to specify the callback contract attribute. ThisCallback Contract attribute will have the interface, which will define the callback.
2 - This is the interface which client will call. In order that it should be asynchronous, we havedefined the one-way attribute on the doHugeTask method.
3 -This is the most important interface. As it forms the bridge of communication between serverand client. The server will call the client using Completed method. Client needs to provideimplementation for the completed method at the client side and server will call that method onceit completes the doHugeTask method.
4 and 5 - In this we implement the Interface Duplex interface and provide implementation fordoHugeTask () method.
6 - This is an important step. The OperationContext.Current.GetCallBackChannel will be used tomake callback to the client.
7 - We will expose the service using HTTP protocol. In this sample, because we want to doduplex communication we need to use wsDualHttpBinding rather than just using simple HttpBinding. This new binding configuration is then assigned to the end on which the service ishosted.
This completes the server side explanation for duplex.
Figure: Duplex Client side code
Above is the code snippet for client side. So let us explain the above code with numbers.
1- We implement the completed method. This is the callback method, which the server will callwhen doHugeTask is completed.
2- In this step we create object of the Instance Context class. Instance Context represents contextinformation of a service. The main use of Instance Context is to handle incoming messages. Inshort, proxy is used to send messages to server and Instance Context is used to accept incomingmessages.
3 -In this step we pass the Instance Context object to the constructor of the proxy. This is needed,as the server will use the same channel to communicate to the client.
4 - In this section two windows are shown. The top windows is the servers output and the belowwindows is of the client. You can see in the below window that server as made a callback to theclient.
You can also buy our book .NET interview question from flipkart by click on
See the following video on WCF One way contract.
Click and get to see more on WCF Interview questions series.
Also visit author’s blog for more WCF interview questions
write a comment