C# .NetTcpBinding 2 services at same port 5000

72 Views Asked by At

I am hosting two services using NetTcpBinding on same port 5000 like below. In my service app.config I have like below

<system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfiguration"
                 closeTimeout="00:10:00"
                 openTimeout="00:10:00"
                 receiveTimeout="00:10:00"
                 sendTimeout="00:10:00"
                  maxBufferSize="2147483647" 
                 maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                         maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                         maxNameTableCharCount="2147483647" />
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="netTcpServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
    <!--First Service [net.tcp://localhost:5000/MyService/FirstService] -->
      <service behaviorConfiguration="netTcpServiceBehavior" name="FirstserviceLib">
        <endpoint address="" binding="netTcpBinding" contract="IFirstService"
           bindingConfiguration="netTcpBindingConfiguration" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:5000/MyService/FirstService" />
          </baseAddresses>
        </host>
      </service>

      <!--Second Service [net.tcp://localhost:5000/MyService/SecondService] -->
      <service behaviorConfiguration="netTcpServiceBehavior" name="SecondserviceLib">
        <endpoint address="" binding="netTcpBinding" contract="ISecondService"
           bindingConfiguration="netTcpBindingConfiguration" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:5000/MyService/SecondService" />
          </baseAddresses>
        </host>
      </service>
    </services>
</system.serviceModel>

I am using console application to self-host like below

static void Main(string[] args)
{
    ServiceHost firstHost =  new ServiceHost(typeof(IFirstService));
    firstHost.Open();
    ServiceHost secondHost =  new ServiceHost(typeof(ISecondService));
    secondHost.Open();
    Console.WriteLine("Services Hosted");
}

When I run my console application I get Service Hosted message. I feel my services are running.

On client side, I have below in my app.config

 <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfiguration"
                 closeTimeout="00:10:00"
                 openTimeout="00:10:00"
                 receiveTimeout="00:10:00"
                 sendTimeout="00:10:00"
                  maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                         maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                         maxNameTableCharCount="2147483647" />
        </binding>
      </netTcpBinding>
    </bindings>

    <client>
      <endpoint address="net.tcp://localhost:5000/MyService/SecondService"
                binding="netTcpBinding"
                contract="ISecondService"
                name="NetTcpBinding_SecondService"
                bindingConfiguration="netTcpBindingConfiguration" />
    </client>
  </system.serviceModel>

I am calling a method in SecondService like below

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);

ConfigurationChannelFactory<ISecondService> chn =
  new ConfigurationChannelFactory<ISecondService>(
  "NetTcpBinding_SecondService",
  config,
  new EndpointAddress("net.tcp://localhost:5000/MyService/SecondService"));
ISecondService facade = chn.CreateChannel();
string fullName = facade.GetName();

I am getting exception like below

System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at net.tcp://localhost:5000/MyService/SecondService that could accept the message. This is often caused by an incorrect address or SOAP action

Since I am working on Windows 10 I enabled and running below service

enter image description here

Note: If I host only 1 service everything is good. But when I add SecondService it is not working. Please let me know what mistake I am making.

I can test IFirstService but not ISecondService

UPDATE 1:

I opened command prompt and went to C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools

I gave svcutil net.tcp://localhost:5000/MyService/SecondService

I do not get any error message like There was no endpoint listening at blah blah...

but when I give svcutil net.tcp://localhost:5000/MyService/FirstService I do get error message like There was no endpoint listening at blah blah...

It seems my service itself is not hosted properly.

It works fine now.

In my consolehost app.config I made sure there are no spaces between <service> in <services> section.

I removed all references to FirstService in Main method in Program.cs of my console host and also in App.config.

I ran my consolehost exe and now ISecondService is hosted

I ran svcutil net.tcp://localhost:5000/MyService/SecondService and now I do not see endpoint not listening error message.

On client side, I was able to execute below line which threw exception in GetName()

ISecondService facade = chn.CreateChannel();
string fullName = facade.GetName();

I fixed my code and re-executed it and this time it was fine.

Now I re-added references to FirstService in Console Host app.config and in Main() of Program.cs

I ran consolehost exe to host both services and now both below did not show me no endpoint message

svcutil net.tcp://localhost:5000/MyService/FirstService

svcutil net.tcp://localhost:5000/MyService/SecondService

Now everything works fine.

0

There are 0 best solutions below