Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WCF service that I've created, and when a client calls one of the service methods, I want to post an event from the service host to the app that instantiated it (NOT to the client app), and have the app that handle those events. I tried this:

C#
public class MyService : IMyService
{
    // this is a standard custom event
    public event MyHostEventHandler MyHostEvent = delegate { };

    //--------------------------------------------------------------------------------
    public void SendStatusMessage(string msg)
    {
        MyHostEvent(this, new MyHostEventArgs(msg, DateTime.Now));
    }

    //--------------------------------------------------------------------------------
    public void SendStatusMessage(string msg, DateTime datetime)
    {
        MyHostEvent(this, new MyHostEventArgs(msg, datetime));
    }
}


But when I create the service host object, I don't see my event in intellisense. Am I going about this the wrong way? Am I missing a step? Is there some WCF bult-in mechanism for doing this? Is there a metatag I can use to enable this?

My next thing to try is specifying delegates to call back to a static class that can post the event instead.
Posted
Updated 23-Feb-11 4:03am
v7

John,

For this to work, your service has to be a singleton service. Otherwise you have no control over the instance of the service class that's being used at any time.

And once you have a singleton service, you can use the SingletonInstance property (you'll need to cast it to your service type) and then access the event (and hook a handler).
 
Share this answer
 
Comments
#realJSOP 25-Jan-11 11:08am    
Thanks - I looked on google for almost half an hour and couldn't find out what to do. I figured it was an attribute thing.
While Nish's answer steered me in the right direction, the more precise solution was to add the following attribute to the service declaration:

C#
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900