[英]Windows Service hosted WCF: Callback to client from Windows service
I got wcf services hosted by a windows service. 我有Windows服务托管的WCF服务。 The windows service listens for usb drives (removal and insertion. Now I want to inform the client about it. Windows服务侦听USB驱动器(卸下和插入。现在我想通知客户端。
I have tried to call a static method in the wcf service from the windows service first where I then call the Callback method via 我试过先从Windows服务在wcf服务中调用静态方法,然后再通过调用Callback方法。
OperationContext.Current.GetCallbackChannel<ICallback>()
But OperationContext.Current is always null. 但是OperationContext.Current始终为null。 Seems I'm in the wrong thread/context. 似乎我在错误的线程/上下文中。
Tried to declare a static event in the wcf service then, registered it in the wcf and called a static method from the windows service in the wcf service that fires the event then: 试图在wcf服务中声明一个静态事件,然后在wcf中注册它,并从wcf服务中的Windows服务中调用一个静态方法,然后触发该事件:
//WCF Service
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WCFService : IService
{
public static event EventHandler<EventArgs> StatusChanged;
public Service()
{
StatusChanged += OnStatusChanged;
}
private void OnStatusChanged(object sender, EventArgs eventArgs)
{
// still not in the correct thread here?
// OperationContext.Current is null
OperationContext.Current.GetCallbackChannel<ILocalLicenceBackendServiceCallback>().ServiceStateChanged();
}
public static void ChangeStatus()
{
if (StatusChanged != null)
StatusChanged(null,EventArgs.Empty);
}
}
//Windows Service
public partial class WindowsService : ServiceBase
{
private void OnStatusChanged()
{
WCFService.ChangeStatus();
}
}
..still not working. ..还是行不通。 So how can I do that, passing information from the windows service to the client with the wcf callbacks. 因此,如何通过wcf回调将信息从Windows服务传递到客户端。
Okay, what I did now is calling an "InitCallback" Function from the Client and saving an ICallBack object into a field. 好的,我现在要做的是从客户端调用“ InitCallback”函数并将ICallBack对象保存到字段中。 Reusing that callback object from the windows service to be able to communiate from the windows service through the wcf service back to the client. 重用Windows服务中的该回调对象,以便能够通过wcf服务从Windows服务与客户端进行通信。
for that to work the wcf service has to run as singelton of course. 为此,wcf服务必须作为singelton运行。 Scalability is not a concern..so I'm fine. 可伸缩性不是问题。.所以我很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.