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.
OperationContext.Currentis only non-null, well, inside a service operation). YourOnStatusChangedevent 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