Search code examples
wcfrestserviceporttcpportsharing

WCF Service Port Sharing problem


I am trying to host several WCF REST services on the same port. I started Net.Tcp Port Sharing Service and this is my app.config file:

<?xml version="1.0"?>
<configuration>

  <system.serviceModel>    
    <services>  
      <service name="MyService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/services/"/>
          </baseAddresses>
        </host>

        <endpoint
          address="test"
          binding="webHttpBinding"
          contract="IMyService"/>
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <bindings>
      <netTcpBinding>
        <binding name="PortSharingEnabled" portSharingEnabled="true">
        </binding>
      </netTcpBinding>
    </bindings>

  </system.serviceModel>

</configuration>

I still cannot host two services on the same port.

When I try to run the second service, I get the following error: http://screencast.com/t/Vlakb26XbuQr. "The Service service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs".

Trace log (http://screencast.com/t/tJ5Gvmy4Dgm7) says: "HTTP could not register URL http://+:7778/MyServiceName/. Another application has already registered this URL with HTTP.SYS."

EDIT:

<services>
  <service name="Service1">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:7778/"/>
      </baseAddresses>
    </host>
    <endpoint
      address="first"
      binding="webHttpBinding"
      contract="IService1"/>
  </service>
  <service name="Service2">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:7778/"/>
      </baseAddresses>
    </host>
    <endpoint
      address="second"
      binding="webHttpBinding"
      contract="IService2"/>
  </service>
</services>

I suppose something is missing to enable port sharing?


Solution

  • What you are attempting to do makes no sense. "Port-Sharing" can happen in two ways:

    • Http: This works (sort of) out of the box on Windows, since HTTP is handled by the HTTP.SYS Kernel-Level driver. There is nothing particular you need to do.

    • Net.Tcp Port Sharing. This requires the steps outlined here.

    However your (only) endpoint is configured to use the HTTP-Binding, not the Net.Tcp-Binding (which in turn makes sense if you want to use a REST-style service), so Net.Tcp Port sharing is not applicable here.

    Without more information on what exactly you want to do, what error you are seeing, it is hard to help.

    EDIT

    Still confusing. From your app.config it looks as if you are using port 80 for your endpoints, however the error message you cite says port 8080. Whatever, the following is applicable anyway.

    From the error messages your provided, it looks as if you're trying to register/start the service twice with the exact same endpoint URI. This will not work. You can reuse the hostname:port part, or even parts of the path, but the complete URI must be unique.

    For example, you could use the following endpoint URIs:

    Note that this is the same for Net.Tcp port sharing - you can share the port, but not the complete (unique) endpoint URI. Hence the name name "port" sharing. It is not a transparent load balancing mechanism or such.