views:

458

answers:

3

I have a need to do some real-time rpoerting on the functionality of a WCF service. The service is self-hosted in a windows app, and my requirement is to report "live" to the host app when certain methods are called by the client.

My initial thought on the task was to publish a "NotifyNow" event in the service code, and subscribe to the event in my calling app, but this doesn't seem to be possible. Within my service code (implementation, not the interface) I have tried adding the following

public delegate void MessageEventHandler(string message);
public event MessageEventHandler outputMessage;

void SendMessage(string message)
{
    if (null != outputMessage)
    {
        outputMessage(message);
    }
}

and call the SendMessage method whenever I need to notify the host app of some action. (This is based on what I remember of this kind of inter-form communication in a winforms app, and my memory may have let me down here...)

When I try to hook up the event handler in my host, though, I can't seem to figure out how to attach to the events... My hosting code is (in a nutshell)

service = new ServiceHost(typeof(MyService));
service.outputMessage += new MyService.MessageEventHandler(frm2_outputMessage);
  // the above line does not work!
service.Open();

(wrapped in a try/catch).

Can anyone help, either by telling me how to get this approach to work or by pointing me at a better way.

TIA

+1  A: 

You seem to be instantiating a default ServiceHost class:

service = new ServiceHost(typeof(MyService));
              ***********
service.outputMessage += new MyService.MessageEventHandler(frm2_outputMessage);
  // the above line does not work!

and I highly doubt that would have a "outputMessage" property for an event handler.

Shouldn't you be instantiating your own custom service host, something like this:

class MyCustomServiceHost : ServiceHost
{
  ...... your custom stuff here ......
}

service = new MyCustomServiceHost(typeof(MyService));
              *******************
service.outputMessage += new MyService.MessageEventHandler(frm2_outputMessage);

??

Marc

marc_s
This looks like a good solution, but I have to confess to not really knowing how I would go about it... I'm with you all the way until ...your custom stuff here... :( I suspect the issue I'm going to face is still around actually hooking into the main event handling code in the service
ZombieSheep
After getting home and turning my mind off for a while, I see exactly what you are saying - I'm looking at the ServiceHost, not the instance of the service... I need to look at it again tomorrow and figure out what I need to code in the custom ServiceHost to pass the event through. Thanks
ZombieSheep
+2  A: 

The service variable is an instance of ServiceHost not your service implementation. Try something like:

MyService myService = new MyService();
myService.outputMessage += new MyService.MessageEventHandler(frm2_outputMessage);
host = new ServiceHost(myService);
Maurice
Upvoted, although I didn't go down this route - I had some issues this afternoon that meant I couldn't spend the required time on it. :)
ZombieSheep
A: 

I did some more research this morning, and turned up a much simpler solution than has been outlined already. The main source of information was this page, but it is summarised here.

1) in the service class, add the following (or similar) code..

public static event EventHandler<CustomEventArgs> CustomEvent;

public void SendData(int value) 
{    
    if (CustomEvent != null)
        CustomEvent(null, new CustomEventArgs());
}

The important bit is to make the event static, otherwise you'll have no chance of catching it.

2) Define a CustomEventArgs class, for example...

public class CustomEventArgs : EventArgs
{
    public string Message;
    public string CallingMethod;                        
}

3) Add calling code in each method of the service in which you have an interest...

public string MyServiceMethod()
{
    SendData(6);
}

4) Instantiate the ServiceHost as normal, and hook into the event.

ServiceHost host = new ServiceHost(typeof(Service1));
Service1.CustomEvent += new EventHandler<CustomEventArgs>(Service1_CustomEvent);
host.Open();

5) Handle the event messages that bubble up to the host from there.

ZombieSheep