How to expose an already instantiated service object in WCF

Iam trying to achieve something similar to CAO in .NET Remoting using WCF. Is there a way i can publish an already instantiated Service object

I know i can expose a Singleton object like follows

m_host = new ServiceHost(myService)

m_host.AddServiceEndpoint(typeof(IService), binding, baseAddress);

m_host.Open();

How can i do the same for a non-singleton Any leads is much appreciated.




Answer this question

How to expose an already instantiated service object in WCF

  • Michael Ruck

     

    Michael,

    Basically, iam trying to achive a Server side Factory model where I have a Singleton Service that provides a list of available  services (lets say MyService). Each client need to get an instance of its own MyService and before a clients request i need to initialize some state on the MyService. This is pretty much what iam trying to achive.

    Any leads is much appreciated.



  • Mike W1

    This is a list of possible extensions:

    http://blogs.thinktecture.com/cweyer/archive/2006/04/27/414527.aspx

    I think you should look into the IServiceBehavior interface.


  • toypaj

    T.Ramesh wrote:
    Iam trying to achieve something similar to CAO in .NET Remoting using WCF. Is there a way i can publish an already instantiated Service object

    I'm not sure I understood what you're trying to do.

    If you want to pass an already-instantiated singleton, you can pass the instance to the ServiceHost's constructor just like the example you posted above.

    If you don't want a singleton, why do you want to instantiate it in advance Each session will have its own instance, so you don't need to instantiate it yourself but let the ServiceHost instantiate it when needed. Instead of passing myService to the constructor, give it typeof(MyServiceClass) and let WCF dynamically create the instances.


  • JenLS

    Also, I have made sure

    [ServiceContract(Session=true)] and

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

    for my Service object. Now all want is to instantiate this object and set properties and publish it so that each client has its own service object instance. Much like CAO in .NET Remoting....



  • KCSmith

    Anver,

    >>If you don't want a singleton, why do you want to instantiate it in advance

    Let's say i need to set some properties before my first client request on the non-singleton object. WCF would use the default contructor to instantiate my object. correct Basically, iam trying to achive a Factory model on the server side where clients can contact a Singleton Factory service and get a list of available services then they can request for a specific service. All this is done fine.

    The piece iam missing is a way to set properties on the client requested specific service. Any leads..



  • phanikirankoneru

    Hmmm.. I hope I understand what you're saying here.

    You want to have each client session have it's own instance of the server object.

    You want to set properties on this instance before the call actually takes place.

    I just wonder if you could use the IInstanceProvider to do this. Before you actually grab an object out of the pool you could set whatever properties in the GetInstance method.

    Thanks,

    Scott



  • bobberino1

    I think you have some things a bit mixed up here. You can publish an object at an endpoint with a binding. That is the case you already have an exmpla for. The service need not be a "singleton". You can have as many of the same service on different ports/addresses as you like. The other case is that a port will act as a multi-session server and the per-session instances created when the client connects to the port your service is on. WCF is about exchanging messages between ports/endpoints, so I am not sure there is any other case that makes sense. If you want to describe what you are trying to acheive in more detail rather than describing the method you thought would solve it, you might get more productive input.

  • KJO43

    Thanks Scott, i think you have pointed me in the correct direction. IInstanceProvider seems to have the answer.

    <From SDK docs>

    IInstanceProvider has two methods, GetInstance and ReleaseInstance. These methods are typically implemented in order to create service objects using a non-default constructor or to initialize or dispose of some state related to the lifetime of the objectIInstanceProvider has two methods, GetInstance and ReleaseInstance. These methods are typically implemented in order to create service objects using a non-default constructor or to initialize or dispose of some state related to the lifetime of the object

    <From SDK docs>

    Thanks,

    T.Ramesh.



  • How to expose an already instantiated service object in WCF