A duplex
Contract defines two logical sets of operations: A set that the Service
exposes for the Client to call and a set that the Client exposes for
the Service to call. The programming model for defining a duplex
Contract is to split each set in a separate type (each type must be a
class or an interface) and annotate the contract that represents the
service's operations with ServiceContractAttribute, referencing the
contract that defines the client (or callback) operations. In addition,
ContractDescription contains a reference to each of the types thereby
grouping them into one duplex Contract.
Similar to Bindings, each Contract has a Name and Namespace that uniquely identify it in the Service's metadata.
Each Contract also has a collection of ContractBehaviors that are modules that modify or extend the contract's behavior.
Duplex Communication |
- Sender and Receiver can exchange multiple messages.
- Sender and Receiver establishes a socket for to communication.
- A duplex service contract is a message exchange pattern which both endpoints can send messages to the other independently.
- It gives Rich Communication[One-Way Communication ].
- It uses dual channel Binding.eg: WsdualBinding.
- Decalre the Callback interfece.
- Add callbackcontract property of ServiceContractAttribute to indicate the callback interface
- CallOperationcontext.Current.GetcallbackChannel to get the callback interface.
- Fire callback interface methods in free your time.
The following is an example of a duplex contract.
Steps for developing duplex contract:
- Start new website -->wcf service.
- Delete the default wcf service and add new wcf service.In interface file create service contract with methods to run at server and an additional interface which is a callbackinterface for this service wcf provides optio in servicecontract and other areas to specify perticular communication.
- Implement the main inteface according to requirement.
- Goto Config file and change wsHttpBinding to wsDualBinding because this binding only provides the Duplex style of communication.
using System;
using
System.Collections.Generic;
using System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using System.Text;
[ServiceContract(SessionMode=SessionMode.Required,CallbackContract
=typeof(ICourierTrackcallback))]
public interface ICourierTrack
{
[OperationContract]
void
PlaceOrder(int value);
}
//callback
interface
public interface ICourierTrackcallback
{
[OperationContract(IsOneWay=true)]
void
Result(string info);
}
Write following code in CourierTrack.cs
using System;
using System.Collections.Generic;
using System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using System.Text;
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class CourierTrack
: ICourierTrack
{
public ICourierTrackcallback callback
{
get
{
return OperationContext.Current.GetCallbackChannel<ICourierTrackcallback>();
}
}
public void PlaceOrder(int
value)
{
callback.Result("Special Order Accepted by Manager");
System.Threading.Thread.Sleep(7000);
callback.Result("Order Dispatched by dispatch team-ref- a23sd-");
System.Threading.Thread.Sleep(7000);
callback.Result("Order in Air-ref-accecing-"+value.ToString());
System.Threading.Thread.Sleep(4000);
callback.Result("Order recd by local client- Thanks U");
}
}
Write following code in CourierTrack.svc
<%@ ServiceHost Language="C#" Debug="true" Service="CourierTrack" CodeBehind="~/App_Code/CourierTrack.cs" %>
Write following code in webconfig file
<system.serviceModel><services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="wsDualHttpBinding" contract="IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service behaviorConfiguration="CourierTrackBehavior" name="CourierTrack">
<endpoint address="" binding="wsDualHttpBinding" contract="ICourierTrack">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="CourierTrackBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
- To Consume this service Goto Duplex Client Program.
No comments:
Post a Comment