Loading of container configuration in IIS

Jul 31, 2012 at 6:22 AM

Hi,

Is it possible to load the container configuration for the WCF services hosted in IIS? I have defined unity configuration in web.config file but there seems to be no place holder for ..

using (IUnityContainer container = new UnityContainer())
{
     container.LoadConfiguration();
}

 Regards,

Vikas Kapoor

Jul 31, 2012 at 7:05 AM

Yes, you can use Unity for IoC with WCF and IIS.  There are quite a few articles out there.  You can create a container and load the configuration in each service but this is a tightly coupled solution which is not generally recommended (but it will still work).  An alternative would be hook into WCF extensibility points to create custom IInstanceProvider, behavior, and ServiceHost/ServiceHostFactory.

These postings go into more depth: 

There is also a project Unity.WCF available via NuGet that could help as well.  Read more about it here: http://www.devtrends.co.uk/blog/introducing-unity.wcf-providing-easy-ioc-integration-for-your-wcf-services.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Aug 8, 2012 at 8:29 AM

Hi,

Thanks for providing related links. Using the Unity.WCF project I created a concrete implementation of UnityServiceHostFactory but I was not able to inject property depedency into the WCF service using the custom factory. Following is the code -

public class UnityIocServiceHostFactory : UnityServiceHostFactory
    {
        protected override void ConfigureContainer(IUnityContainer container)
        {
          // this works but I am not able to inject depedency
          container.RegisterType(typeof(ICustomerService), typeof(CustomerService));

          // this does not work. 
          //container.RegisterType(typeof(ICustomerService), typeof(CustomerService),new InjectionProperty("Controller", new ResolvedParameter<ICustomer>("CustomerController")));

          // this also does not work 
          // container.RegisterType<ICustomerService, CustomerService>(new InjectionProperty("Controller", new ResolvedParameter<ICustomer>("CustomerController")));

          // Loading the configuration not successfuly either after including namespace Microsoft.Practices.Unity.Configuration
            //container.LoadConfiguration();
        }
    }
 
 
 Ideally I wanted to achieve configuration like the following using the custom factory - 
    <container>
      <register name="MemoryAdapter" type="IBusinessAdapter" mapTo="InMemoryAdapter"></register>
      
      <register name="SqlAdapter" type="IBusinessAdapter" mapTo="SqlAdapter" />
      
      <register type="ICustomer" mapTo="CustomerController" name="CustomerController">
        <property name="BusinessAdapter" dependencyName="MemoryAdapter" dependencyType="IBusinessAdapter"></property>
      </register>

      <register type="ICustomerService" mapTo="CustomerService" >
        <property name="Controller" dependencyName="CustomerController" dependencyType="ICustomer"/>
      </register>
      
    </container>
Kindly suggest, if needed I can share the sample code. 
Regards,
Vikas Kapoor
Aug 8, 2012 at 9:00 AM

I am sorry. Following code in the custom factory seems to be working. I will verify again and if problem persist I will let you know.

container.LoadConfiguration();