Click here to Skip to main content
Click here to Skip to main content

WCF Unity

, 24 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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.

 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.

    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)

About the Author

Emre Coskun
Software Developer (Senior) Bank of Kuveyttruk
Turkey Turkey
I m fan Ms Patterns&Practices group Smile | :)

Comments and Discussions

 
GeneralMy vote of 1 PinmemberDmitri Nesteruk30-Jun-09 23:52 
QuestionSource code? Pinmemberseesharper24-Jun-08 8:53 
AnswerRe: Source code? PinmemberEmre Coskun24-Jun-08 9:37 
GeneralRemove the line numbers please Pinmemberleppie24-Jun-08 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 24 Jun 2008
Article Copyright 2008 by Emre Coskun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid