42

I want to know is there way of Event Handling in WCF. I came across Callbacks in WCF, but i want to do Event Handling in WCF.

My requirement is like i want to raise event to particular clients not to all the clients using Event Handling in WCF and i also want to maintain session.

I have seen Publisher/Subscriber model in WCF which deals with Callback , but this model publish to all the clients who have subscribed but i want to publish only to selected clients.

I think that can be done using Events in WCF.

Client side :

public class Callbacks : IServiceCallback
{
    public void CallToMyClient(string name)
    {
        this.CallToMyClient(name);  

    }
}

protected void Page_Load(object sender, EventArgs e)
{
    Callbacks callback = new Callbacks();            
    ServiceClient client = new ServiceClient(new InstanceContext(callback));        

    client.SubscribeClient();
    client.DoSomeWork(); 
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bokambo
  • 4,204
  • 27
  • 79
  • 130

4 Answers4

75

There is no Event in WCF to notify it's client but there is a callback channel, the purpose of the callback channel is same as event though the working principle is totally different in both cases. To notify a particular client what you could do is store callback channel of that client while subscribing to somewhere, (I prefer Dictionary in this case). Later you can pick the instance and invoke your callback method over that channel, doing so only one client will get notified.

UPDATE

If you are interested here is the code:

public interface IClientCallback
{
    //Your callback method
    [OperationContract(IsOneWay = true)]
    void CallToMyClient(string name);
}
[ServiceContract(CallbackContract = typeof(IClientCallback))]
public interface ITestService
{

    [OperationContract(IsOneWay = true)]
    void SubscribeClient();
    [OperationContract(IsOneWay = true)]
    void DoSomeWork();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class ServiceImplementation : ITestService
{
    private static readonly List<IClientCallback> CallbackChannels = new List<IClientCallback>();

    /// <summary>
    /// client should call this method before being notified to some event
    /// </summary>
    public void SubscribeClient()
    {
        var channel = OperationContext.Current.GetCallbackChannel<IClientCallback>();
        if (!CallbackChannels.Contains(channel)) //if CallbackChannels not contain current one.
        {
            CallbackChannels.Add(channel);
        }
    }

    public void DoSomeWork()
    {
        //Here write your code to do some actual work
        //After you done with your work notify client
        //here you are calling only the first client that is registered
        IClientCallback callbackChannel = CallbackChannels[0];
        callbackChannel.CallToMyClient("You are the only one receving this message");
    }
}
crypted
  • 10,118
  • 3
  • 39
  • 52
5

WCF Duple Operation and UI Threads By jeff.barnes

Perhaps this can help you.

The WCF doesn't support event handler. Callback channel is the way for it

Thang Do
  • 316
  • 2
  • 16
  • 1
    This is a fun example that is easy to build and run (as long as you remember to run the BeerService as Administrator). I used the upgrade wizard to load it into VS 2010 (with no problems). – Andrew Jens Jun 29 '13 at 02:52
1

If you are using WCF for RPC(as apposed to web service or rest) you can use .Net Remoting to perfrom event invocation cross process.

Dmitry
  • 1,513
  • 1
  • 15
  • 33
  • 5
    John .net remoting is not deprecated. Its legacy and still has valuable use cases, especially if RPC event invocation is a requirement! Please educate your self prior to down-voting: http://stackoverflow.com/questions/1294494/is-net-remoting-really-deprecated – Dmitry Feb 20 '13 at 20:52
  • Please note MS Tech lead's responsible own words regarding the framework: "Remoting is part of the .Net Framework and as such it isn't going away... For in-process, cross-appdomain communication Remoting is the CLR's native way of communicating. – Dmitry Feb 20 '13 at 20:54
  • 1
    The words of the unnamed "MS Tech lead", on some unspecified date, do not change the fact that, for almost all requirements, WCF is the replacement for .NET remoting. Even in terms of the request of the OP, it is not _events_ which are required - it is _notification_. A WCF callback contract achieves this without the need to resort to Remoting. And BTW, I said "deprecated", not "obsolete". – John Saunders Feb 20 '13 at 21:02
  • The name is Clemens Vasters, just read the answers in the link – Dmitry Feb 20 '13 at 21:13
  • I'm familiar with Mr. Vasters. What was the date of the remark? Do you have a link? – John Saunders Feb 20 '13 at 21:42
-2

You cannot use events. You can use callbacks to simulate events.

John Saunders
  • 160,644
  • 26
  • 247
  • 397