Windows Communication Foundation (WCF) based Callbacks.
Callbacks
WCF supports duplex communication between service and client also. In other
words, service can also call a client's function to inform the client during
an event. A callback contract must be defined, and client should implement
that contract interface.
public interface IResult
{
[OperationContract]
void Result (int result);
}
Client needs to inform the WCF that it supports the callback. This is done
when creating the ChannelFactory.
class Client : IResult
{
public void Result (int result)
{
Console.WriteLine("Result = {0}", result);
}
public Client ()
{
NetTcpBinding netTcpBinding = new NetTcpBinding();
Uri serviceUri = new Uri("http://localhost:9090/AdderService");
IChannelFactory channelFactory = new DuplexChannelFactory(
this, netTcpBinding);
IAdder adder = channelFactory.CreateChannel(serviceUri);
}
}
On the service side, the service gets the callback channel using the OperationContext's GetCallbackChannel method.
IResult resultCallback = OperationContext.Current.GetCallbackChannel();
resultCallback.Result(5);
If you found this post helpful, please "Kick" it so others can find it too: