I have a need to do some real-time reporting on the functionality of a WCF service. The service is self-hosted in a windows app, and my requirement is to report \"live\" to
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
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);
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.