Can I access the same instance of a Service object without using Channel?

37 views Asked by At

I am using InstanceContextMode.Single in the sevice behaviour plus my service also implements the singleton pattern. Such as:

 private static readonly object padlock = new object();
 public static MySingletonService Instance
      {
          get
          {
              lock (padlock)
              {


                  if (_event == null)
                  {
                      _event = new MySingletonService();
                  }
                  return _event;
              }
          }
      }

Now, the reason that I did this was I wanted to access the same instance of the service regardless of whether the call comes from a channel or someone simply calls it like a simple class(code snippets given for both scenarios).

var channelFactory = new ChannelFactory<MySingletonService>(myBinding, myEndpoint);

             channelFactory.Open();
            using (channelFactory)
             {
            var channelclient = channelFactory.CreateChannel();
            channelclient.MethodCall(this);
               }

Also,

MySingletonService.Instance.MethodCall(this);

But, I get two differenct objects (verified by checking object hashcode) in both cases and not one object.

Is there any other way to achieve this?

0

There are 0 answers