title: Configuring a WCF and StructureMap ServiceHostFactory from code
body: <p>I am creating a custom ServiceHost object and configuring it from code. My service is using InstanceContextMode.Single and ConcurrencyMode.Multiple and is hosted in a windows service.</p>
  
  <p>As stated in a number of blogs/articles <a href="http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/" rel="nofollow">(here)</a>, sharing a StructureMap container across instances requires using a custom InstanceProvider, ServiceBehavior and ServiceHostFactory.</p>
  
  <p>My initialization code looks like this. I do not use a config file.</p>
  
  <pre><code>var baseAddress = ConfigurationManager.AppSettings["BaseAddress"];
  var port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);
  
  Host = new MyServiceHost(typeof(MediaFileServicePrivate), new Uri(string.Format(baseAddress, port)));
  
  var binding = new NetTcpBinding();
  Host.AddServiceEndpoint(typeof(IMediaFileServicePrivate), binding, string.Format(baseAddress, port));
  </code></pre>
  
  <p>How do I tell the service to use my custom service host factory? All the examples I can find configure it from the config file.</p>
  
  <p>Is a ServiceHostFactory only used for IIS/WAS hosted scenarios? If so, how do I use SM for a self-hosted InstanceContextMode.Single service?</p>