WCF net tcp 服务对象有空事件
WCF net tcp service object has null event
我有一个 WCF 服务只用一种方法实现服务契约 'foo'。
[ServiceContract]
public interface IWCFService
{
[OperationContract]
void Foo(MyEntity entity);
}
我正在使用一些添加的属性和一个事件来实现此接口。
internal class MyWCFService : IWCFService
{
public event EventHandler MyEvent;
public string Name { get; set; }
private ServiceHost WCFServiceHost { get; set; }
internal MyWCFService()
{
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
WCFServiceHost = new ServiceHost(typeof(MyWCFService), new Uri[] { new Uri("net.tcp://xxx.xxx.xxx.xxx:61243") });
WCFServiceHost.AddServiceEndpoint(typeof(IWCFService), tcpBinding, "MyWCFService");
}
internal void Start()
{
WCFServiceHost.Open();
}
internal void Stop()
{
this.WCFServiceHost.Close(new TimeSpan(0, 0, 1));
}
void IWCFService.Foo(MyEntity entity)
{
//Business logic...
ThrowEvent(entity);
}
private void ThrowEvent(MyEntity entity)
{
if (this.MyEvent != null) //this.MyEvent is always null even though I'd successfully suscribe a function in another class
{
this.MyEvent(entity);
}
}
}
在另一个 class 同一服务器应用程序 上,我订阅了 'MyEvent'。
public class AnotherClass
{
internal MyWCFService TheService { get; set; } = new MyWCFService();
public void Start()
{
this.TheService.MyEvent += FunctionThatDoesSomethingWithMyEntity;
//Breakpoint here, this.TheService.MyEvent has 1 suscriber
this.TheService.Start();
}
private void FunctionThatDoesSomethingWithMyEntity(MyEntity entity)
{
//More logic...
}
}
问题是,尽管我已经用调试器检查过函数 FunctionThatDoesSomethingWithMyEntity 已成功订阅 MyEvent OtherClass.Start(),当客户端调用WCF服务方法Foo时,MyEvent为null .
此外,我在 Foo 中的断点处停止时注意到以下错误:
error CS1061: 'MyWCFService' does not contain a definition for 'MyWCF' and找不到接受类型 'MyWCFService' 的第一个参数的扩展方法 'MyWCF'(您是否缺少 using 指令或程序集引用?)
我相信每次客户调用 Foo 都会创建一个新的 MyWCFService 对象,因此 MyEvent 为空,对此有解释吗?
事件不能像您期望的那样在 WCF 中工作。This question,但可能会帮助您。基本上你需要使用回调。
编辑:由于您澄清并尝试绑定到事件而不是通过 WCF,而是在 server/WCF 项目的另一个 class 中,您假设每个实例都是正确的创建的 WCF 客户端生成一个新的 class,因此失去了事件绑定。我不能保证它会工作,因为我现在无法测试它,但你可以尝试制作事件 "static" 这样它就不会在每个新的 class 实例中创建。
我有一个 WCF 服务只用一种方法实现服务契约 'foo'。
[ServiceContract]
public interface IWCFService
{
[OperationContract]
void Foo(MyEntity entity);
}
我正在使用一些添加的属性和一个事件来实现此接口。
internal class MyWCFService : IWCFService
{
public event EventHandler MyEvent;
public string Name { get; set; }
private ServiceHost WCFServiceHost { get; set; }
internal MyWCFService()
{
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
WCFServiceHost = new ServiceHost(typeof(MyWCFService), new Uri[] { new Uri("net.tcp://xxx.xxx.xxx.xxx:61243") });
WCFServiceHost.AddServiceEndpoint(typeof(IWCFService), tcpBinding, "MyWCFService");
}
internal void Start()
{
WCFServiceHost.Open();
}
internal void Stop()
{
this.WCFServiceHost.Close(new TimeSpan(0, 0, 1));
}
void IWCFService.Foo(MyEntity entity)
{
//Business logic...
ThrowEvent(entity);
}
private void ThrowEvent(MyEntity entity)
{
if (this.MyEvent != null) //this.MyEvent is always null even though I'd successfully suscribe a function in another class
{
this.MyEvent(entity);
}
}
}
在另一个 class 同一服务器应用程序 上,我订阅了 'MyEvent'。
public class AnotherClass
{
internal MyWCFService TheService { get; set; } = new MyWCFService();
public void Start()
{
this.TheService.MyEvent += FunctionThatDoesSomethingWithMyEntity;
//Breakpoint here, this.TheService.MyEvent has 1 suscriber
this.TheService.Start();
}
private void FunctionThatDoesSomethingWithMyEntity(MyEntity entity)
{
//More logic...
}
}
问题是,尽管我已经用调试器检查过函数 FunctionThatDoesSomethingWithMyEntity 已成功订阅 MyEvent OtherClass.Start(),当客户端调用WCF服务方法Foo时,MyEvent为null .
此外,我在 Foo 中的断点处停止时注意到以下错误:
error CS1061: 'MyWCFService' does not contain a definition for 'MyWCF' and找不到接受类型 'MyWCFService' 的第一个参数的扩展方法 'MyWCF'(您是否缺少 using 指令或程序集引用?)
我相信每次客户调用 Foo 都会创建一个新的 MyWCFService 对象,因此 MyEvent 为空,对此有解释吗?
事件不能像您期望的那样在 WCF 中工作。This question,但可能会帮助您。基本上你需要使用回调。
编辑:由于您澄清并尝试绑定到事件而不是通过 WCF,而是在 server/WCF 项目的另一个 class 中,您假设每个实例都是正确的创建的 WCF 客户端生成一个新的 class,因此失去了事件绑定。我不能保证它会工作,因为我现在无法测试它,但你可以尝试制作事件 "static" 这样它就不会在每个新的 class 实例中创建。