简体   繁体   中英

Windows Service hosted WCF: Callback to client from Windows service

I got wcf services hosted by a windows service. The windows service listens for usb drives (removal and insertion. Now I want to inform the client about it.

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

OperationContext.Current.GetCallbackChannel<ICallback>() 

But OperationContext.Current is always 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 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.

Okay, what I did now is calling an "InitCallback" Function from the Client and saving an ICallBack object into a field. 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.

for that to work the wcf service has to run as singelton of course. Scalability is not a concern..so I'm fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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