chollida
chollida

Reputation: 7894

Hooking wcf service creation

We have a WCF service that has multiple clients connecting to it.

Our service is setup so that its a per instance service. The service needs to have access to another instance object to do its work. The required instance is not a wcf service and I'd rather not make the required instance a singleton.

If the service was an object that I created then I'd just pass it the instance object it needs to interact with. But since this is a wcf service its created by wcf.

How can I hook the service's creation to pass it some data/interfaces to use or how can I get a pointer to a service after its been created so I can pass it the required instance.

[ServiceContract]    
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void DoSomethingCool();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
class MyService : IMyService 
{
    private IHelper helper;
    void DoSomethingCool()
    {
        // How do I pass an instance of helper to MyService so I can use it here???
        helper.HelperMethod();
    }
}

Upvotes: 1

Views: 136

Answers (2)

chollida
chollida

Reputation: 7894

Ok, what I ended up doing was to make a singleton ServiceGlue object as an intermediary instead of making my helper class a singleton.

The service and the helper both self register with an intermediary that is a singleton and the singleton passes the instances back and forth.

Before my service is setup I'd instantiate the singleton and register the helper so that each service can get the helper object.

this makes my code look like:

public class ServiceGlue
{
    private static ServiceGlue instance = null;
    public static ServiceGlue Instance
    {
        get
        {
            if (instance == null)
                instance = new ServiceGlue();
             return instance;
         }
     }

    public Helper helper { get; set; }
}


[ServiceContract]    
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void DoSomethingCool();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode =     ConcurrencyMode.Multiple)]
class MyService : IMyService 
{
    private IHelper helper;
    public MyService()
    {
       // use an intermidiary singleton to join the objects
       helper = ServiceGlue.Instance.Helper();
    }

    void DoSomethingCool()
    {
       // How do I pass an instance of helper to MyService so I can use it here???
       helper.HelperMethod();
    }
}

Upvotes: 0

gleng
gleng

Reputation: 6304

As Tim S. suggested, you should read into dependency injection (From his comment, link can be found here http://msdn.microsoft.com/en-us/library/vstudio/hh273093%28v=vs.100%29.aspx) . A poor mans dependency injection can be used like:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
class MyService : IMyService 
{
    private IHelper _helper;

    public MyService() : this(new Helper())
    {

    }

    public MyService(IHelper helper)
    {
       _helper = helper;
    }

    void DoSomethingCool()
    {            
        helper.HelperMethod();
    }
}

If you want a specific implementation of this, you will need to get an IoC (Inversion of Control) container, to resolve this dependency. There are tons of this available, but in particular, I use Castle Windsor.

Upvotes: 1

Related Questions