views:

122

answers:

1

Hi all

I'm implementing an alert type system within my company LAN using WCF callbacks. It has a subscribe mechanism etc. I've used this tutorial as a starting point but I changed the binding to NetTcpBinding instead of wsDualHttpBinding and I'm self hosting in a Windows service.

That's working quite nicely but I have a question. Most of my clients do not need callback. They are various desktop applications that only need to send a one way alert to the server which will be passed on to those clients running the callback enabled "Notify" application and subscribed to that type of alert.

I might be concerned about nothing here but since my WCF service implements callback, all the clients need to implement a callback object whether they need callback or not. It would seem like a more tidy solution if the one way clients communicated with a service that does not do callback.

So ... I created another endpoint without callback in my WCF Service. It just has a simple one way method call. That works but my problem is that I can't quite figure out how to pass the received message to the callback enabled service.

My Windows Service has something like this:

        internal static ServiceHost myNotifyHost;
    internal static ServiceHost mySendingHost;

    protected override void OnStart(string[] args)
    {
        // service with callback
        myNotifyHost = new ServiceHost(typeof(NotifyService));
        myNotifyHost.Open();

        // service without callback
        mySendingHost = new ServiceHost(typeof(SendingService));
        mySendingHost.Open();
    }

In my SendingService method that is called by the sendonly client, I thought I'd be able to do this:

    var notify = (NotifyService)WindowsService.myNotifyHost.SingletonInstance;

    notify.SendMessage("Message text");

SendMessage() sends the callback messages out to subscribed clients. Unfortunately, myNotifyHost.SingletonInstance is always null even when there is a client connected and waiting for callback. I guess I'm misunderstanding what that property means. NortifyService has these attributes

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]

Is there a way that I can communicate between the two services? Should I give up and this and just stick to the one service and just live with implementing the meaningless callback class in those clients that don't need it. At this point it's not a big deal. It's more to do with my understanding of WCF.

Thanks

+1  A: 

Try this,

  public class NotifyService
    {
        public static NotifyService DefaultInstace;

        public NotifyService()
        {
            DefaultInstace = this;


        }
     ///.....SNIP......      
   }
unclepaul84
Thanks. That works just fine. I actually thought of that earlier but somehow I never tried it because I had some weird idea that you couldn't access "this" inside a constructor.
tetranz