简体   繁体   English

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

[英]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.

相关问题 Windows Service托管的WCF终结点存在问题 - Problems with WCF endpoints hosted from Windows Service 从Windows服务中调用Forms身份验证站点中托管的WCF服务 - Calling WCF service hosted in Forms Authentication site from a Windows Service 使用从外部解决方案托管在Windows服务中的WCF服务 - Consuming a WCF Service that is hosted in a Windows Service from outside solution 如何从 IIS7 中托管的另一个 WCF 服务评估托管 WCF 服务的 Windows 服务? - How to assess a Windows Service hosted WCF service from another WCF service hosted in IIS7? 从另一个WCF服务(自行托管)使用一个WCF服务(托管在Windows Service上)时出错 - Error while consuming one WCF Service (hosted on Windows Service) from another WCF Service (self hosted) 使用XML注释生成WCF(Windows服务托管)客户端代理类? - Generate WCF (windows service hosted) client proxy class with XML comments? Windows服务中托管的WCF服务-不起作用 - WCF service hosted in Windows service - not working 如何调试由 Windows 服务托管的 WCF 服务? - How to debug a WCF service that hosted by Windows Service? 托管在Windows服务中的WCF服务 - WCF Service Hosted in a Managed Windows Service Windows Service中托管的WCF服务停止挂起 - WCF Service hosted in Windows Service hangs on stop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM