1

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?

Nebojsa Veron
  • 1,545
  • 3
  • 19
  • 36

2 Answers2

1

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.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • After I try to start the second service on the same port, I get this error box: http://screencast.com/t/Vlakb26XbuQr. And this is the trace log exception: http://screencast.com/t/tJ5Gvmy4Dgm7. – Nebojsa Veron Aug 26 '11 at 10:52
  • Could you add the error messages as text, preferably to your question, please? This way those of us, who are suffering from overzealous corporate proxy-filtering, can see them. – Christian.K Aug 26 '11 at 10:55
  • My base addres is the same, but the endpoint address is not, take a look: http://screencast.com/t/HVd91WoThQU (edit in original post). As soon as I change the port of the second service to 7779, it works perfectly. – Nebojsa Veron Aug 29 '11 at 09:28
  • The image at http://screencast.com/t/rutAOcQYvEZ is (a) showing something else than what your question shows, is (b) apparently not the complete `system.serviceModel` configuration section (although I think that shouldn't matter in that case it is "bad style") and (c) is using (again) different ports and service names than the ones you cite in your question. It is really hard to help with things changing all the time and not being fully and consistently shown. I would suggest you provide full (at best self contained) example, including full configuration, error messages, etc. in your question. – Christian.K Aug 29 '11 at 09:38
0

Even though there is an accepted answer, I'll just throw it out there that I had a similar problem when defining the services programatically. I create the service the following way:

_serviceHost = new ServiceHost(_cmdService, new Uri(_serviceAddress));

ServiceEndpoint endPoint =
    _serviceHost.AddServiceEndpoint(typeof(IHttpCmdService), _binding, _endpointAddress);
endPoint.Behaviors.Add(new WebHttpBehavior());

Amazingly, when I try to do this multiple times with the same _serviceAddress and different _endpointAddress values, it fails. But if I put the whole address into the _serviceAddress it succeeds. Both methods end up with the same final service URL.

So, knowing this I'm wondering if you put create your services with different baseAddress values, will it successfully start, so it would look like this:

<services>
  <service name="Service1">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:7778/first/"/>
      </baseAddresses>
    </host>
    <endpoint
      address=""
      binding="webHttpBinding"
      contract="IService1"/>
  </service>
  <service name="Service2">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:7778/second/"/>
      </baseAddresses>
    </host>
    <endpoint
      address=""
      binding="webHttpBinding"
      contract="IService2"/>
  </service>
</services>
Gyuri
  • 4,548
  • 4
  • 34
  • 44