0
votes

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.

1
You can only callback when inside a service operation that has originally been a client invocation to your service (OperationContext.Current is only non-null, well, inside a service operation). Your OnStatusChanged event handler does not execute in the context of a WCF service operation. Have you researched other "publish subscribe wcf" articles on SO already? Lots of info there. - Christian.K

1 Answers

0
votes

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.