Inversion of Control with WCF and Unity (Part II)

This post is based on the first Inversion of Control with WCF and Unity.

In the last post we have seen how we can use Inversion of Control with WCF. The sample cover only self-hosted applications scenario. To use the same solution in ASP.NET applications, hosted by IIS, we must develop another component: the ServiceHostFactory.

The UnityServiceHostFactory looks like this:

  1: public class UnityServiceHostFactory : ServiceHostFactory
  2: {
  3:     protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
  4:     {
  5:         IUnityContainer unityContainer = null;
  6:         var configurationSection = 
  7:             (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
  8: 
  9:         string configurationName = GetConfigurationName(serviceType);
 10:         var unityContainerElement = 
 11:             configurationSection.Containers[configurationName];
 12:         if (unityContainerElement == null)
 13:         {
 14:             unityContainerElement = 
 15:                 configurationSection.Containers.Default;
 16:         }
 17: 
 18:         if (unityContainerElement != null)
 19:         {
 20:             unityContainer = new UnityContainer();
 21:             unityContainerElement.Configure(unityContainer);
 22:         }
 23: 
 24:         return new UnityServiceHost(unityContainer, serviceType, baseAddresses);
 25:     }
 26: 
 27:     private string GetConfigurationName(Type serviceType)
 28:     {
 29:         ServiceBehaviorAttribute serviceBehaviorAttribute = serviceType.GetCustomAttributes(true)
 30:             .OfType<ServiceBehaviorAttribute>()
 31:             .FirstOrDefault();
 32: 
 33:         return (serviceBehaviorAttribute != null &&
 34:             !string.IsNullOrEmpty(serviceBehaviorAttribute.ConfigurationName))
 35:                    ? serviceBehaviorAttribute.ConfigurationName
 36:                    : serviceType.FullName;
 37:     }
 38: }

The CreateServiceHost method is invoked by the ServiceHostingEnvironment to create the ServiceHost. In this method we also create the instance of the UnityContainer and then configure it based on the config file (the web.config). The implemented solution try to get the specific container whose name matches the ConfigurationName indicated in the ServiceBehavior behavior. If not specified, uses the full name of the type of service. However if the specific container is not found, the unityContainer is configured with the default container. Finally, creates the instance of the ServiceHost and assign the unityContainer instance.

Now, in the .svc file we can specify the UnityServiceHostFactory:

  1: <%@ ServiceHost Language="C#" Debug="true" 
  2: Service="DotNetSide.WCF.IoC.Service.BookService, DotNetSide.WCF.IoC.Service"
  3: Factory="DotNetSide.WCF.IoC.Unity.UnityServiceHostFactory, DotNetSide.WCF.IoC.Unity" %>

Finally we can run our application.

bye [:)]

1 Comment

  • Thanks, nice post - I am wondering if I can use Unity to solve the issue of having over 1000 entity (DbSet) classes. I need to be able to call these entities remotely, as the web app is here, and the db there - do you know how I could leverage the multiple classes using just one service and Injection attributes ?

Comments have been disabled for this content.