title: Using Unity Dependency Injection with WCF services
body: <p>I have the following after doing some research on other questions:</p>
  
  <p><strong>MyServiceHost:</strong></p>
  
  <pre><code>public class MyServiceHost : ServiceHost
  {
      public MyServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses)
          : base(serviceType, baseAddresses)
      {
          if (container == null)
          {
              throw new ArgumentNullException("container");
          }
          foreach (var cd in this.ImplementedContracts.Values)
          {
              cd.Behaviors.Add(new DependencyInjectionInstanceProvider(container));
          }
      }
  }
  </code></pre>
  
  <p><strong>DependencyInjectionInstanceProvider:</strong></p>
  
  <pre><code>public class DependencyInjectionInstanceProvider : IInstanceProvider, IContractBehavior 
  {     
      private readonly IUnityContainer container;      
      public DependencyInjectionInstanceProvider(IUnityContainer container)     
      {         
          if (container == null)         
          {             
              throw new ArgumentNullException("container");         
          }          
  
          this.container = container;     
  
      }      
  
      #region IInstanceProvider Members      
  
      public object GetInstance(InstanceContext instanceContext, Message message)     
      {         
          return this.GetInstance(instanceContext);     
      }      
  
      public object GetInstance(InstanceContext instanceContext)     
      {         
          var serviceType = instanceContext.Host.Description.ServiceType;         
          return this.container.Resolve(serviceType);     
      }      
  
      public void ReleaseInstance(InstanceContext instanceContext, object instance)    
      {        
          this.container.Teardown(instance);     
      }      
  
      #endregion      
  
      #region IContractBehavior Members      
  
      public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)     
      {     
      }      
  
      public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)     
      {     
      }      
  
      public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)    
      {         
          dispatchRuntime.InstanceProvider = this;     
      }      
  
      public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)     
      {     
      }     
      #endregion 
  
  } 
  </code></pre>
  
  <p><strong>MyServiceHostFactory:</strong></p>
  
  <pre><code>    public class MyServiceHostFactory : ServiceHostFactory
  {
      private readonly IUnityContainer container;     
      protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
      { 
          return new MyServiceHost(this.container, serviceType, baseAddresses); 
      }
  }
  </code></pre>
  
  <p><strong>Email Service with an attempted Constructor Injection:</strong></p>
  
  <pre><code>public class EmailValidator : IEmailValidator
  {
      private IFakeDAL fakeDAL;
  
      public EmailValidator(IFakeDAL fakeDAL)
      {
          this.fakeDAL = fakeDAL;
      }
  
      public bool ValidateAddress(string emailAddress)
      {
          Console.WriteLine("Validating: {0}", emailAddress);
  
          string pattern = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$";
          return Regex.IsMatch(emailAddress, pattern);
      }
  }
  </code></pre>
  
  <p><strong>My Console Host to start the Service:</strong></p>
  
  <pre><code>static void Main(string[] args)
      {
          Type serviceType = typeof(EmailValidator);
          Uri serviceUri = new Uri("http://localhost:8080/");
  
          MyServiceHostFactory shf = new MyServiceHostFactory();
          ServiceHost host = shf.CreateServiceHost(serviceType, serviceUri);
          //ServiceHost host = new ServiceHost(serviceType, serviceUri);
          host.Open();
  </code></pre>
  
  <p>My problem resides in the console host logic.  The CreateServiceHost call has a syntax error due to the first argument expecting a Constructor string and not a Type.  Which I don't understand since it does accept a Type parameter.  In addition to that I don't understand where I should be mapping IFakeDAL to a concrete class.  Can I do that in an app.config file or should I register that somewhere else?</p>