Click here to Skip to main content
15,885,061 members
Articles / Programming Languages / C#
Article

WCF Unity

Rate me:
Please Sign up or sign in to vote.
2.38/5 (8 votes)
24 Jun 2008CPOL1 min read 50.1K   718   14   4
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)


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
Dmitri Nеstеruk30-Jun-09 23:52
Dmitri Nеstеruk30-Jun-09 23:52 
QuestionSource code? Pin
seesharper24-Jun-08 8:53
seesharper24-Jun-08 8:53 
AnswerRe: Source code? Pin
Emre Coskun24-Jun-08 9:37
Emre Coskun24-Jun-08 9:37 
GeneralRemove the line numbers please Pin
leppie24-Jun-08 2:54
leppie24-Jun-08 2:54 
They are not even correct (notably 1st line).

Thanks Smile | :)

xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)

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

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