C# 侦听从windows服务中运行的应用程序引发的事件
我正在尝试侦听在windows服务下运行的应用程序引发的事件。下面是示例代码,以演示我所追求的 单独的类库代码(classlibrary1.dll) 托管Windows服务(windowsservice1exe) 客户端应用程序(consoleapp1.exe) 如您所见,我已经尝试为发布者/订阅者子类化C# 侦听从windows服务中运行的应用程序引发的事件,c#,events,windows-services,C#,Events,Windows Services,我正在尝试侦听在windows服务下运行的应用程序引发的事件。下面是示例代码,以演示我所追求的 单独的类库代码(classlibrary1.dll) 托管Windows服务(windowsservice1exe) 客户端应用程序(consoleapp1.exe) 如您所见,我已经尝试为发布者/订阅者子类化MarshalByRefObject,但它不起作用。我的意思是,我可以在事件日志中看到事件正在引发,但我的客户端应用程序从未收到通知(或)通知从未到达 请让我知道如何做到这一点 由于我的服务和客
MarshalByRefObject
,但它不起作用。我的意思是,我可以在事件日志中看到事件正在引发,但我的客户端应用程序从未收到通知(或)通知从未到达
请让我知道如何做到这一点
由于我的服务和客户端都在同一台机器上运行,我相信我不需要使用Remoting
。对吧?
namespace ClassLibrary1
{
public class Class1 : MarshalByRefObject
{
public event TestServiceDel TestEvent;
TestEventArgs args1 = new TestEventArgs
{
Data = "Hello Buddy ... How Is Life!!!!!"
};
public void RaiseEvent()
{
if (!EventLog.SourceExists("TestService"))
EventLog.CreateEventSource("TestService", "Application");
EventLog.WriteEntry("TestService","Before raising event ...");
if (TestEvent != null)
TestEvent(this, args1);
EventLog.WriteEntry("TestService","After raising event ...");
}
}
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Threading.Thread.Sleep(1000);
ClassLibrary1.Class1 cs = new ClassLibrary1.Class1();
cs.RaiseEvent();
}
class Program : MarshalByRefObject
{
static void Main(string[] args)
{
Class1 cs = new Class1();
cs.TestEvent += Cs_TestEvent;
Console.ReadLine();
}
private static void Cs_TestEvent(object sender, TestEventArgs args)
{
EventLog.WriteEntry("TestService",args.Data, EventLogEntryType.Information);
Console.WriteLine(args.Data);
}