views:

815

answers:

3

Hi there,

I have a created a simple Custom ServiceHost that inherits from ServiceHost and overrides the method InitializeRuntime.

How do i change my app.config / web.config to use the custom service host so my overriden InitializeRunTime is executes..

I see attributes in the config file like behaviorConfiguration etc but nothing obvious where i can force it to use my Custom ServiceHost

My ServiceHost is simple :-

public class UnityServiceHost : ServiceHost
{
    protected override void InitializeRuntime()
    {

        //DO MY UNITY INJECTION HERE 
        //Console.WriteLine("");
        base.InitializeRuntime();
    }


}

Any help really appreciated.

Thanks

A: 

If you're hosting the service yourself (as opposed to using IIS or WAS), you should simply create a UnityServiceHost instead of a regular ServiceHost.

If you are using IIS or WAS, write a ServiceHostFactory

SLaks
Actually i will be using IIS as the host container but for TDD i want to be able to used TDD directly on the WCF Service Class but of course I when TDD calls the service something needs to injects the objects otherswise some methods are going to fail..This is easy with Global.asax to create on SessionStart and inject values but i wanted a general solutions so that doesn't matter what the host is IIS, TDD or what ever the values will be injected
mark smith
A: 

You need to create a custom ServiceHostFactory and use that to create your UnityServiceHost. You specify the ServiceHostFactory to use in the SVC file. See the MSDN docs for the SVC syntax.

Maurice
thanks for the reply, but what about TDD, I wanted to test the WCF Service Class rather than the hosted SVC... I only what to inject objects that i am going to use inside the wcf service - i don't want to inject any Services etc
mark smith
+1  A: 

Oran Dennison describes how to do this using Spring.NET: http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html

In summary, you'll use WCF's "behavior injection" to supply an instance of the service created by your DI container.

1) Create custom IInstanceProvider implementation with the GetInstance method returning the service object created by your container:

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return _container.Resolve(_serviceType);
    }

2) Implement a custom IServiceBehaviour that adds your custom IInstanceProvider to each endpoint configuration.

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
        {
            ChannelDispatcher cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = 
                        new YourCustomInstanceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }

3) In your custom service host, override OnOpening and add your custom service behavior

    protected override void OnOpening()
    {
        this.Description.Behaviors.Add(new CustomServiceBehavior());
        base.OnOpening();
    }

Note that you may have to pass down your UnityContainer instance through to the IInstanceProvider so that it can do the resolving.

Andy J