Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Can you explain duplex contracts in WCF
Can you explain duplex contracts in WCF?
✍: Guest
In duplex contracts when client initiates an operation the server service provides a reference
call back Uri back to the client. So the client initiates a call using the proxy class and
when server finishes its work it notifies the client using the callback channel. This is
called as duplex messaging in WCF. If you remember in the previous question we had no
way to know when the server finished its task.
Figure 15.19:- Duplex Service code
Let’s try to understand the duplex concept by doing a small sample. The code snippet is as
shown in the above figure. We will extend the previous sample which was shown in the
first question only that now we will provide the notification to the client once the
doHugeTask is completed.
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 callbackcontract attribute.
This CallBackContract 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
have defined the one way attribute on the doHugeTask method.
3-- This is the most important interface. As it forms the bridge of communication between
server and client. The server will call the client using Completed method. Client needs to
provide implementation for the completed method at the client side and server will call
that method once it completes the doHugeTask method.
4 and 5 -- In this we implement the InterfaceDuplex interface and provide implementation
for doHugeTask () method.
6 -- This is an important step. The OperationContext.Current.GetCallBackChannel will
be used to make callback to the client.
7 - We will expose the service using HTTP protocol. In this sample because we want to
do duplex communication we need to use wsDualHttpBinding rather than just using
simple HttpBinding. This new binding configuration is then assigned to the end point on
which the service is hosted.
This completes the server side explanation for duplex.
Figure: - 15.20 Duplex Client side code
Above is the code snippet for client side. So let’s explain the above code with numbers.
1-- We implement the completed method. This is the callback method which the server
will call when doHugeTask is completed.
2-- In this step we create object of the InstanceContext class. InstanceContext represents
context information of a service. The main use of InstanceContext is to handle incoming
messages. In short proxy is used to send messages to server and InstanceContext is used
to accept incoming messages.
3 -- In this step we pass the InstanceContext 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 below windows is of the client. You can see in the below window that server as made
a callback to the client.
2007-11-04, 8306👍, 0💬
Popular Posts:
What will be printed as the resultof the operation below: int x; int modifyvalue() { return(x+=10); ...
What Happens If the UPDATE Subquery Returns Multiple Rows? - Oracle DBA FAQ - Understanding SQL DML ...
What will be printed as the resultof the operation below: int x; int modifyvalue() { return(x+=10); ...
How can we know the number of days between two given dates in PHP? Simple arithmetic: <?php $...
1. The basics first, please define the web in simple language? How is it connected with internet? Wh...