订阅WCF服务中的事件 [英] Subscribe to events within a WCF service
问题描述
我需要对WCF服务的功能进行一些实时的报告。该服务是在Windows应用程序中自我托管的,我的要求是在客户端调用某些方法时向主机应用程序报告live。
我的初始这个任务的想法是在服务代码中发布一个NotifyNow事件,并在我的通话应用程序中订阅该事件,但这似乎是不可能的。在我的服务代码(实现,而不是接口)中,我尝试添加以下
public delegate void MessageEventHandler(string message);
public event MessageEventHandler outputMessage;
void SendMessage(string message)
{
if(null!= outputMessage)
{
outputMessage(message);
}
}
,只要我需要通知SendMessage方法主机应用程序的一些动作。 (这是基于我在winforms应用程序中记住的这种形式间通信,我的记忆可能让我在这里...)
当我尝试连接我的主机中的事件处理程序,但是,我似乎无法弄清楚如何附加到事件...我的主机代码(简而言之)
service = new ServiceHost(typeof(MyService));
service.outputMessage + = new MyService.MessageEventHandler(frm2_outputMessage);
//以上行不行!
service.Open();
(包裹在try / catch中)
任何人都可以帮助,告诉我如何使这种方式工作,或者指点一下更好的方式。
TIA
我今天早上做了一些更多的研究,并提出了比已经概述的更简单的解决方案。信息的主要来源是此页面,但它在这里总结。
1)在服务类中,添加以下(或类似的)代码..
public static event EventHandler< CustomEventArgs> CustomEvent;
public void SendData(int value)
{
if(CustomEvent!= null)
CustomEvent(null,new CustomEventArgs());
}
重要的是让事件静止,否则你将没有抓住它的机会。
2)定义一个CustomEventArgs类,例如...
public class CustomEventArgs:EventArgs
{
public string Message;
public string CallingMethod;
}
3)在您拥有的服务的每种方法中添加调用代码兴趣...
public string MyServiceMethod()
{
SendData(6);
}
4)正常实例化ServiceHost,并挂钩事件。 p>
ServiceHost host = new ServiceHost(typeof(Service1));
Service1.CustomEvent + = new EventHandler< CustomEventArgs>(Service1_CustomEvent);
host.Open();
5)处理从那里向主机冒泡的事件消息。
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 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
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.
这篇关于订阅WCF服务中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!