Collection Already Contains Address with scheme net.tcp

寵の児 提交于 2019-12-13 04:57:28

问题


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:

System.ArgumentException: This collection already contains an address with scheme net.tcp.

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.

I have a services.development.config file. We have the app.config file separated for specific reasons within the organization.

<service name="MLITS.Pulse.Pulse" behaviorConfiguration="PulseCSBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:14613/PulseService"/>
      </baseAddresses>
    </host>
    <endpoint name="NetTcpEndPoint"
              address=""
              binding="netTcpBinding"
              contract="MLITS.Pulse.IPulse" />
    <endpoint name="NetTcpMetadataPoint"
              address="mex"
              binding="mexTcpBinding"
              contract="IMetadataExchange" />

We also have the endpoint address in a client.development.config file.

<endpoint address="net.tcp://localhost:14613/PulseService" binding="netTcpBinding"
bindingConfiguration="NetTcpEndPoint" contract="PulseService.IPulse" name="NetTcpEndPoint" />

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.

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);
}

The error appears when we try and process the ServiceHost line. My question is: How do I resolve this, for it to function correctly?

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.


回答1:


I have resolved the issue I was having in regards to this question. Instead of trying to assign the service host with another URI, I had to first clear the Endpoints and then add my new service endpoint. The only changes I made were to the DynamicPort method. Code Follows:

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";

    Uri uri = new Uri(uriString);

    ServiceHost objServiceHost = new ServiceHost(typeof(Pulse));
    objServiceHost.Description.Endpoints.Clear();
    objServiceHost.AddServiceEndpoint(typeof(Pulse), new NetTcpBinding(), uri);
}


来源:https://stackoverflow.com/questions/31431175/collection-already-contains-address-with-scheme-net-tcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!