The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8090/'

396 Views Asked by At

I have gone through many answers on Stackoverflow before posting my question. None of the answers seemed to resolve my issue. I have created a simple WCF service and have hosted it in Console application using TCP binding.

My Host App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <system.serviceModel>
     <bindings>
      <netTcpBinding>
        <binding closeTimeout="00:01:00" openTimeout="00:01:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647" transferMode="Buffered">
          <security mode="None">
            <message clientCredentialType="None"/>
            <transport clientCredentialType="None"
                       protectionLevel="None" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="mexbehavior" name="EmailService.PAEmail">
        <endpoint address="MailService" binding="netTcpBinding" contract="EmailService.IPAEmail" />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8090/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>      
        <behavior name="mexbehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

My contracts:

[ServiceContract]
    public interface IPAEmail
    {
        [OperationContract]         
        void SendMail();
    }

 public class PAEmail : IPAEmail
    {
       public void SendMail()
        {
           
        }
    }

My host:

static void Main(string[] args)
        {
            using (ServiceHost obj = new ServiceHost(typeof(EmailService.PAEmail)))
            {
                obj.Open();
                Console.WriteLine("Host Started at {0}", DateTime.Now.ToString());
            }

            Console.ReadLine();
        }

The host is running in my pc. From my client, if i try to add service reference, it gives me this error:

The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8090/'. Could not connect to net.tcp://localhost:8090/. The connection attempt lasted for a time span of 00:00:04.0092984. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8090. No connection could be made because the target machine actively refused it 127.0.0.1:8090 If the service is defined in the current solution, try building the solution and adding the service reference again.

What am I missing here?

1

There are 1 best solutions below

1
Ding Peng On BEST ANSWER

This is because you used using and released the servicehost after creating the servicehost.You can put Console.ReadLine() in using so that ServiceHost is not released immediately:

static void Main(string[] args)
        {
            using (ServiceHost obj = new ServiceHost(typeof(PAEmail)))
            {
                obj.Open();
                Console.WriteLine("Host Started at {0}", DateTime.Now.ToString());
                Console.ReadLine();
            }
            
        }

You can also choose not to use using:

static void Main(string[] args)
            {
                ServiceHost obj = new ServiceHost(typeof(PAEmail));
                obj.Open();
                Console.WriteLine("Host Started at {0}", DateTime.Now.ToString());
                Console.ReadLine();
                obj.Close();
            }

Feel free to let me know if the problem persists.