繁体   English   中英

Windows Service托管的WCF:从Windows服务回调到客户端

[英]Windows Service hosted WCF: Callback to client from Windows service

我有Windows服务托管的WCF服务。 Windows服务侦听USB驱动器(卸下和插入。现在我想通知客户端。

我试过先从Windows服务在wcf服务中调用静态方法,然后再通过调用Callback方法。

OperationContext.Current.GetCallbackChannel<ICallback>() 

但是OperationContext.Current始终为null。 似乎我在错误的线程/上下文中。

试图在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();
    }

}

..还是行不通。 因此,如何通过wcf回调将信息从Windows服务传递到客户端。

好的,我现在要做的是从客户端调用“ InitCallback”函数并将ICallBack对象保存到字段中。 重用Windows服务中的该回调对象,以便能够通过wcf服务从Windows服务与客户端进行通信。

为此,wcf服务必须作为singelton运行。 可伸缩性不是问题。.所以我很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM