title: Collection Already Contains Address with scheme net.tcp
body: <p>I have a persistent problem, which I have been researching for a couple days now.  I am working on transitioning a WCF service to use a dynamic port, a need has finally come into scope for this.  I am most of the way there; however, I am getting the error: </p>
  
  <blockquote>
    <p>System.ArgumentException:  This collection already contains an address with scheme net.tcp.</p>
  </blockquote>
  
  <p>I have searched online for an answer, and have only found solutions to if the scheme was http.  I will provide some code, to aid those who desire to help me and others with this problem.  </p>
  
  <p>I have a services.development.config file. We have the app.config file separated for specific reasons within the organization.</p>
  
  <pre><code>&lt;service name="MLITS.Pulse.Pulse" behaviorConfiguration="PulseCSBehavior"&gt;
      &lt;host&gt;
        &lt;baseAddresses&gt;
          &lt;add baseAddress="net.tcp://localhost:14613/PulseService"/&gt;
        &lt;/baseAddresses&gt;
      &lt;/host&gt;
      &lt;endpoint name="NetTcpEndPoint"
                address=""
                binding="netTcpBinding"
                contract="MLITS.Pulse.IPulse" /&gt;
      &lt;endpoint name="NetTcpMetadataPoint"
                address="mex"
                binding="mexTcpBinding"
                contract="IMetadataExchange" /&gt;
  </code></pre>
  
  <p>We also have the endpoint address in a client.development.config file.</p>
  
  <pre><code>&lt;endpoint address="net.tcp://localhost:14613/PulseService" binding="netTcpBinding"
  bindingConfiguration="NetTcpEndPoint" contract="PulseService.IPulse" name="NetTcpEndPoint" /&gt;
  </code></pre>
  
  <p>Now it is my understanding with the method I'm using, I am able to keep the specific port number specified here, and later change it.  Now the way I'm changing the port number is adding the session id to the base port number (which would be the 14613).  The following code is the method that does just that.</p>
  
  <pre><code>public static void DynamicAddress()
  {
      int sessionIdentification = 0;
      int portNumber = 0;
      int newPort = 0;
      string uriString = string.Empty;
  
      sessionIdentification = Process.GetCurrentProcess().SessionId;
      portNumber = 14613;
      newPort = portNumber + sessionIdentification;
  
      uriString = "net.tcp://localhost:" + newPort + "/PulseService";
  
      //seperate the port from the rest of the service
      //add the session id to the port
      //put the address back together and return it
  
      Uri uri = new Uri(uriString);
  
      ServiceHost objServiceHost = new ServiceHost(typeof(Pulse), uri);
  }
  </code></pre>
  
  <p>The error appears when we try and process the ServiceHost line.  My question is:  How do I resolve this, for it to function correctly?</p>
  
  <p>Keep in mind, I have tried commenting out the base addresses in the services.development.config file, as well as the endpoint address in the client.development.config file.  When doing this I have ran into other issues that were because I've commented the address out.</p>