WCF

WCF Examples

WCF

WCF Projects

WCF Project

adplus-dvertising
Defining and configuring a callback contract
Previous Home Next

We provide the ServiceContract attribute with the type of the callback contract and the definition of the callback contract for define a service contract with a callback contract

Example:

interface ISomeCallbackContract
{
   [OperationContract]
   void OnCallback( );
}
[ServiceContract(CallbackContract = typeof(ISomeCallbackContract))]
interface IMyContract
{
   [OperationContract]
   void operation1( );
}
Duplex proxy

When we use such a service endpoint whose contract defines a callback contract, then the client use a proxy that will set up the bidirectional communication and pass the callback endpoint reference to the service. The proxy the client uses must derive from the specialized proxy class DuplexClientBase<T>.

Example of DuplexClientBase<T>:

public interface IDuplexContextChannel : IContextChannel
{
   InstanceContext CbInstance
   {get;set;}
   //More members
}
public abstract class DuplexClientBase<T> : 
ClientBase<T> where T : class
{
   protected DuplexClientBase(InstanceContext callbackContext);
   protected DuplexClientBase(InstanceContext callbackContext,
                              string EpName);
   protected DuplexClientBase(InstanceContext callbackContext,
                              Binding binding,
                              EndpointAddress remoteAddress);
   protected DuplexClientBase(object CbInstance);
   protected DuplexClientBase(object CbInstance,
                              string endpointConfigurationName);
   protected DuplexClientBase(object CbInstance,Binding binding,
                              EndpointAddress remoteAddress);

   public IDuplexContextChannel InnerDuplexChannel
   {get;}
   //More members
}
Tool-generated duplex proxy

When we use use SvcUtil or Visual Studio 2005 to create a proxy class targeting a service with a callback contract then the tools will create a class that derives from DuplexClientBase<T>.

partial class MyContractClient : DuplexClientBase<IMyContract>,IMyContract
{
public MyContractClient(InstanceContext callbackContext) :
	base(callbackContext)
{}
public MyContractClient(InstanceContext callbackContext,string EpName) :
 base(callbackContext,EpName)
{}
public MyContractClient(InstanceContext callbackContext,Binding binding,
EndpointAddress remoteAddress) :
 base(callbackContext,binding,remoteAddress)
{}
//More constructors
public void operation( )
{
Channel.operation( );
}
}

Client implementing the callback contract

class MyClient : IMyContractCallback,IDisposable
{
   MyContractClient mypro;

   public void CallService( )
   {
      InstanceContext cn = new Instancecn(this);
      mypro = new MyContractClient(cn);
      mypro.operation( );
   }
   public void OnCallback( )
   {...}

   public void Dispose( )
   {
      mypro.Close( );
   }
}
Previous Home Next