none
Adding WCF service reference using net.tcp fails: The requested address is not valid in its context RRS feed

  • Question

  • Dear, I have followed instructions on a microsoft  article titled (How to: Host WCF in a Windows Service Using TCP) to create and install WCF windows service. It all worked well locally.

    Then I wanted to move it to a server, I changed base address to point to server's IP, and installed windows service on server, but when I tried to add reference to that service from my client, I got this error:

    A TCP error (10049: The requested address is not valid in its context) occurred while listening on IP Endpoint=192.168.x.x:808



    Can you help me please ?
    Saturday, November 30, 2019 1:03 PM

Answers

  • Hi,
    My friend, there are many factors that result in these kinds of problems. Please try to check the below.
    1. Adding service reference is valid only when we have added the MEX metadata service endpoint.

    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
      <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata />


    Or programming style.

    using (ServiceHost sh = new ServiceHost(typeof(TestService), uri)) { sh.AddServiceEndpoint(typeof(ITestService), binding, ""); ServiceMetadataBehavior smb; smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null) { smb = new ServiceMetadataBehavior() { }; sh.Description.Behaviors.Add(smb); } Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();

    sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex"); sh.Opened += delegate { Console.WriteLine("service is ready"); }; sh.Closed += delegate { Console.WriteLine("service is closed"); }; sh.Open(); Console.ReadLine(); sh.Close(); }



    2. We have to ensure the WCF service over TCP works properly when we consume it on the client-side. can you access the service start page properly in the browser?
    3. Please ensure the windows service relates to NETTCP works properly.

    Net.Tcp listener Adapter
    Net.Tcp Port Sharing service.

    Feel free to let me know if the problem still exists.
    Updated.

    According to the question owner, we need to replaced server's IP in the service base address with its DNS name. after that it works.


    Monday, December 2, 2019 2:37 AM
    Moderator

All replies

  • Hi,
    My friend, there are many factors that result in these kinds of problems. Please try to check the below.
    1. Adding service reference is valid only when we have added the MEX metadata service endpoint.

    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
      <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata />


    Or programming style.

    using (ServiceHost sh = new ServiceHost(typeof(TestService), uri)) { sh.AddServiceEndpoint(typeof(ITestService), binding, ""); ServiceMetadataBehavior smb; smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null) { smb = new ServiceMetadataBehavior() { }; sh.Description.Behaviors.Add(smb); } Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();

    sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex"); sh.Opened += delegate { Console.WriteLine("service is ready"); }; sh.Closed += delegate { Console.WriteLine("service is closed"); }; sh.Open(); Console.ReadLine(); sh.Close(); }



    2. We have to ensure the WCF service over TCP works properly when we consume it on the client-side. can you access the service start page properly in the browser?
    3. Please ensure the windows service relates to NETTCP works properly.

    Net.Tcp listener Adapter
    Net.Tcp Port Sharing service.

    Feel free to let me know if the problem still exists.
    Updated.

    According to the question owner, we need to replaced server's IP in the service base address with its DNS name. after that it works.


    Monday, December 2, 2019 2:37 AM
    Moderator
  • Thanks a lot. I did all of that, but it didn't work until I replaced server's IP in the service base address with its DNS name. Now everything is fine. I marked your comment as an answer, but please add the "DNS" point to it so it can be a more complete answer for others to benefit from.

    Monday, December 2, 2019 7:18 AM