Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

WCF Unity

2.38/5 (8 votes)
24 Jun 2008CPOL1 min read 1   718  
This project aims using wcf, dependency injection and policy injection together.

Introduction

This project represents to how exploit unity container for WCF. Unity and Policy injection application block worked together in this project. So that WCF services make dependency injection and policy injection. I assume that type definitions are different configuration file. So I avoid that the complex of config file, also it occurred the opportunity of using independent in the implement of project.

Using the code

The best place of unity container in WCF can use that service life time controller interface of Instance Provider. First of all, I wrote instance provider to supply for dependency injection.

C#
public class DependencyInjectionInstanceProvider : IInstanceProvider {
   private void InitContainer(string dependencyFile, string containerName) {
       string currentContainerKey = GetDependencyKey(dependencyFile, containerName);
       if (!containers.ContainsKey(currentContainerKey)) {
           lock (syncRoot) {
               if (!containers.ContainsKey(currentContainerKey)) {
                   containers[currentContainerKey] = currentContainer =
                       new UnityContainer();

                   ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                   map.ExeConfigFilename = dependencyFile;
                   System.Configuration.Configuration config
                     = ConfigurationManager.OpenMappedExeConfiguration(map,
                     ConfigurationUserLevel.None);
                   UnityConfigurationSection section
                     = (UnityConfigurationSection)config.GetSection("unity");

                   section.Containers[containerName].Configure(currentContainer);

                   currentContainer.AddNewExtension<PolicyInjectionExtension><policyinjectionextension />();
               }
           }
       } else {
           currentContainer = containers[currentContainerKey];
       }
   }

DependencyInjectionInstanceProvider need two parameters: Dependency file which is config in type definition. Container name is the name in the config file. Each container only one occurs and saves. Finally these objects supply for PolicyInjectionExtansion. PolicyInjectionExtansion is only register under EnterpriseLibrary.PolicyInjection.ObjectBuilder’s namespace’s strategy and policy. Finally it is necessary that the implement dependency injection provider as a new service behavior.

C#
public class DependencyInjectionServiceBehavior : Attribute, IServiceBehavior {
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 DependencyInjectionInstanceProvider(
                       serviceDescription.ServiceType, configFile, containerName);
            }
        }
    }
}

And now, it can use WCF and unity dependency together.

static void Main(string[] args) {
       ServiceHost hostA = null, hostB = null;
       try {
           hostA = new ServiceHost(typeof(ServiceA));
           hostB = new ServiceHost(typeof(ServiceB));
           hostA.Open();
           hostB.Open();
   [ServiceBehavior]
   public class ServiceA : IServiceAContract {

       public ServiceA() {

       }
       private IServiceBContract serviceB;
       private IServiceC serviceC;

       [InjectionConstructor]
       public ServiceA(IServiceBContract serviceB) {
           Console.WriteLine("Build up Service A");
           this.serviceB = serviceB;
           Console.WriteLine("Service A constructor calling to serviceB.OperationD");
           this.serviceB.OperationD("test1", "test2");
       }

       [Dependency]
       public IServiceC ServiceC {
           set {
               serviceC = value;
               Console.WriteLine("IServiceC injected to ServiceA parameter ");
           }
       }
   public class ServiceB : IServiceBContract {
       public ServiceB() {

       }
       private IServiceC serviceC;

       [InjectionConstructor]
       public ServiceB(IServiceC serviceC) {
           Console.WriteLine("Build up Service B ");
           this.serviceC = serviceC;
           Console.WriteLine("Service B constructor calling to serviceC.OperationG");
           this.serviceC.OperationG("test");
           this.serviceC.OperationG("test");
       }
Many popular application architects like smart/web client software factory have their own object builder. But there is no application architect for WCF like SCSF/WCSF. This is an absence.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)