title: Trying to add IoC support to my WCF Services
body: <p>I am trying to add IoC support to my REST WCF service (Windows Server 2008). I am new to this and am following the instructions provided in the below video:</p>
  
  <p><a href="http://www.dimecasts.net/Content/WatchEpisode/150" rel="noreferrer">http://www.dimecasts.net/Content/WatchEpisode/150</a></p>
  
  <p>The video walks through a number of classes that help me get StructureMap's IoC up and running while exposing WCF endpoints. I have posted all of the code at the end of this post.</p>
  
  <p>When I run my code, the custom class StructureMapServiceHost throws an error @ the StructureMapServiceHost(Type serviceType, params Uri[] baseAddress ) method:</p>
  
  <pre><code>   public class StructureMapServiceHost : ServiceHost
  {
      public StructureMapServiceHost() {}
  
      public StructureMapServiceHost(Type serviceType, params Uri[] baseAddress)
          : base(serviceType, baseAddress)
      {
  
      }
  
      protected override void OnOpening()
      {
          Description.Behaviors.Add( new IoCServiceBehavior());
          base.OnOpening();
      }
  }
  </code></pre>
  
  <p>I am being told that:</p>
  
  <p><strong>The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.</strong></p>
  
  <p>This is true, it doesn't. But the video example didn't have one either. Below is my service:</p>
  
  <pre><code>[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
      public class UserService : IUserService
      {
          public UserService(IUserRepository specification)
          {
              Specification = specification;
          }
  
          public List&lt;User&gt; GetAllUsers()
          {
              return Specification.GetAllUsers();
          }
  
          public User GetUser(string userId)
          {
              return Specification.GetUserById(new Guid(userId));
          }
  
          private List&lt;User&gt; SearchForUsers(string searchString)
          {
              return Specification.SearchUsers(searchString);
          }
  
          public IUserRepository Specification { get; set; }
  
      }
  
  
  
  public class IoCServiceBehavior : IServiceBehavior
      {
          public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
          {   
          }
  
          public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, 
              Collection&lt;ServiceEndpoint&gt; endpoints, BindingParameterCollection bindingParameters)
          {
          }
  
          public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
          {
              foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
              {
                  new StructureMapInstanceProvider(serviceDescription.ServiceType);
              }
          }
      }
  
  
  public class StructureMapInstanceProvider : IInstanceProvider
  {
      private readonly Type _serviceType;
  
      public StructureMapInstanceProvider(Type serviceType)
      {
          _serviceType = serviceType;
      }
  
      public object GetInstance(InstanceContext instanceContext)
      {
          return GetInstance(instanceContext, null);
      }
  
      public object GetInstance(InstanceContext instanceContext, Message message)
      {
          var instance = ObjectFactory.GetInstance(_serviceType);
  
          return instance;
      }
  
      public void ReleaseInstance(InstanceContext instanceContext, object instance)
      {
          throw new NotImplementedException();
      }
  }
  
  
  
  
  public class StructureMapServiceHostFactory : ServiceHostFactory
      {
          public StructureMapServiceHostFactory()
          {
              IoCBootstrap.SetupIoc();
          }
  
          protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
          {
              return new StructureMapServiceHost(serviceType, baseAddresses);
          }
      }
  </code></pre>
  
  <p>Any ideas? Thanks.</p>
  
  <p><strong>EDIT <em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>****</strong></p>
  
  <p>From StructureMapServiceHost I removed:</p>
  
  <pre><code>public StructureMapServiceHost(Type serviceType, params Uri[] baseAddress)
              : base(serviceType, baseAddress) { }
  </code></pre>
  
  <p>And added:</p>
  
  <pre><code>public StructureMapServiceHost(Object singletonInstance, params Uri[] baseAddress)
              : base( singletonInstance, baseAddress) { }
  </code></pre>
  
  <p>And then removed the parameter from my UserService constructor. I am not getting the error: </p>
  
  <blockquote>
    <p>The HTML document does not contain Web
    service discovery information.</p>
  </blockquote>