订阅 WCF 服务中的事件 [英] Subscribe to events within a WCF service

查看:15
本文介绍了订阅 WCF 服务中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对 WCF 服务的功能进行一些实时报告.该服务在 Windows 应用程序中自托管,我的要求是在客户端调用某些方法时向主机应用程序报告实时".

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.

我对该任务的最初想法是在服务代码中发布NotifyNow"事件,并在我的调用应用程序中订阅该事件,但这似乎是不可能的.在我的服务代码(实现,而不是接口)中,我尝试添加以下内容

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);
    }
}

并在我需要通知主机应用某些操作时调用 SendMessage 方法.(这是基于我对winforms应用程序中这种表单间通信的记忆,我的记忆可能让我失望了……)

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();

(包装在 try/catch 中).

(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) 在服务类中,添加以下(或类似的)代码..

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) 定义一个CustomEventArgs类,例如...

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

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

3) 在您感兴趣的服务的每个方法中添加调用代码...

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

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

4) 照常实例化 ServiceHost,并钩入事件.

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) 处理从那里冒泡到主机的事件消息.

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

这篇关于订阅 WCF 服务中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆