Click here to Skip to main content
13,002,133 members (80,761 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 24 Jun 2008

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.


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,
                   UnityConfigurationSection section
                     = (UnityConfigurationSection)config.GetSection("unity");


                   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));
   public class ServiceA : IServiceAContract {

       public ServiceA() {

       private IServiceBContract serviceB;
       private IServiceC serviceC;

       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");

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

       private IServiceC serviceC;

       public ServiceB(IServiceC serviceC) {
           Console.WriteLine("Build up Service B ");
           this.serviceC = serviceC;
           Console.WriteLine("Service B constructor calling to serviceC.OperationG");
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.


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 | :)

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Dmitri Nesteruk30-Jun-09 23:52
memberDmitri Nesteruk30-Jun-09 23:52 
QuestionSource code? Pin
seesharper24-Jun-08 8:53
memberseesharper24-Jun-08 8:53 
AnswerRe: Source code? Pin
Emre Coskun24-Jun-08 9:37
memberEmre Coskun24-Jun-08 9:37 
GeneralRemove the line numbers please Pin
leppie24-Jun-08 2:54
memberleppie24-Jun-08 2:54 

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.

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