Previous | Home | Next |
The callback mechanism provides higher-level protocol for managing the connection between the service and the callback endpoint
Exapmle of Explicit callback connection management
[ServiceContract(CallbackContract = typeof(IMyContractCallback))] interface IMyContract { [OperationContract] void operation( ); [OperationContract] void Connectoperation( ); [OperationContract] void DisConnectoperation( ); } interface IMyContractCallback { [OperationContract] void OnCallback( ); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] class MyService : IMyContract { static List<IMyContractCallback> m_Callbacks = new List <IMyContractCallback>( ); public void Connectoperation( ) { IMyContractCallback callback = OperationContext.Current. GetCallbackChannel<IMyContractCallback>( ); if(m_Callbacks.Contains(callback) == false) { m_Callbacks.Add(callback); } } public void DisConnectoperation( ) { IMyContractCallback callback = OperationContext.Current. GetCallbackChannel<IMyContractCallback>( ); if(m_Callbacks.Contains(callback) == true) { m_Callbacks.Remove(callback); } else { throw new InvalidOperationException("Cannot find callback"); } } public static void CallClients( ) { Action<IMyContractCallback> invoke = delegate (IMyContractCallback callback) { callback.OnCallback( ); }; m_Callbacks.ForEach(invoke); } public void operation( ) {...} }
WCF also provides DuplexChannelFactory<T>, which can be used for setting up duplex proxies programmatically.
DuplexChannelFactory<T> is used as base class, ChannelFactory<T>, except its constructors expect either a callback instance or a callback context.
public class DuplexChannelFactory<T> : ChannelFactory<T> { public DuplexChannelFactory(object callback); public DuplexChannelFactory(object callback,string EPName); public DuplexChannelFactory(InstanceContext context,string EPName); public T CreateChannel(InstanceContext context); public static T CreateChannel(object callback,string EPName); public static T CreateChannel(InstanceContext context,string EPName); public static T CreateChannel(object callback,Binding binding, EndpointAddress EPAddress); public static T CreateChannel(InstanceContext context,Binding binding, EndpointAddress EPAddress); //More members }
Previous | Home | Next |