title: How to Host a WCF Service in a Managed Windows Service?
body: <p>I have 3 project in my solution.</p>
  
  <ul>
  <li>Test Client         => for add reference and access via tcp ip</li>
  <li>WcfServiceLibruary1 => for executing my methots</li>
  <li>WindowsService1     => for installing&amp;running as windows service(Account:Network Service , StartType:Automatic)</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/8Ax5v.png" alt="enter image description here"></p>
  
  <p>I used all same codes on msdn sample</p>
  
  <pre><code>http://msdn.microsoft.com/en-us/library/ff649818.aspx
  </code></pre>
  
  <p>I use a wcf service that has 2 methots.I want to use this  wcf service in managed windows service.I added a windows service to my solution and set references stuff.</p>
  
  <p>I use this address referance on my wcf - app.config:</p>
  
  <pre><code>net.tcp://localhost:2023/Service1
  </code></pre>
  
  <p><em>NOW PROBLEM IS:</em></p>
  
  <p>I success to add reference to my test client project using</p>
  
  <p><code>net.tcp://localhost:2023/Service1</code>:</p>
  
  <p>But this referance address is not be used on install as windows service !!!
  When i install it as windows service,i cant access this address,
  And i got this error: <code>No connection could be made because the target machine actively refused it</code> </p>
  
  <p>WcfServiceLibruary app.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
    &lt;configuration&gt;
      &lt;system.web&gt;
      &lt;compilation debug="true"/&gt;
    &lt;/system.web&gt;
    &lt;!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WcfServiceLibrary1.Service1"&gt;
          &lt;endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1"&gt;
            &lt;identity&gt;
              &lt;dns value="localhost"/&gt;
            &lt;/identity&gt;
          &lt;/endpoint&gt;
          &lt;endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="net.tcp://localhost:2023/Service1"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=""&gt;
            &lt;serviceMetadata httpGetEnabled="false"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  
  &lt;startup&gt;&lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&gt;&lt;/startup&gt;&lt;/configuration&gt;
  </code></pre>
  
  <p>My WindowsService :</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      if (myServiceHost != null)
      {
          myServiceHost.Close();
      }
      myServiceHost = new ServiceHost(typeof(Service1));
      myServiceHost.Open();
   }
  </code></pre>
  
  <p>Everything works well when i start on visualstudio service host:
  <img src="https://i.stack.imgur.com/KiMO8.png" alt="enter image description here"></p>