views:

291

answers:

1

I have a service that is hosted in WAS. I am trying to inject this service with dependencies, but have been having trouble finding where to do this. In a WCF service hosted in IIS you can use the application_onstart event to instantiate the castle container, but this isn't available in my scenario. So, I am trying to create a custom host factory like so:

public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            container = new WindsorContainer();
            container.Register(Component.For<IMyDependency>().ImplementedBy<MyDependency>());
            DefaultServiceHostFactory.RegisterContainer(container.Kernel);

            var service = container.Resolve(constructorString);
            ServiceHost serviceHost = new ServiceHost(service, baseAddresses);

            return serviceHost;
        }

This works fine with singleton WCF services, but how do you get this to work with per-call? With non singleton services it is expecting a type to be passed to the ServiceHost constructor rather than an actual service. However, if I do this then when the service spins up it looks for a parameterless constructor only, not the DI version.

Any ideas on how to get this all working?

Thanks

+1  A: 

WCF Facility will do that (and a lot more) for you.

Krzysztof Koźmic
Thanks, I have got WCF Facility working with WCF services activated in IIS before, but not WAS. In the past I simply hooked the service up to the Wcf factory then added the container in the global.asax. However, I cannot hook up the container in global.asax with WAS, so how would you put this all together?
Jon Archway
The answer is to hook into AppInitialize()Thanks
Jon Archway
cool, glad you made it work. If you have any questions regarding Castle, it's best to ask on the discussion list: http://groups.google.com/group/castle-project-users
Krzysztof Koźmic