【问题标题】:The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8090/'无法识别 URI 前缀。元数据包含无法解析的引用:'net.tcp://localhost:8090/'
【发布时间】:2020-08-12 11:30:06
【问题描述】:

在发布我的问题之前,我已经在 Stackoverflow 上浏览了许多答案。似乎没有一个答案能解决我的问题。我创建了一个简单的 WCF 服务,并使用 TCP 绑定将其托管在控制台应用程序中。

我的主机 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>

我的合同:

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

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

我的主人:

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();
        }

主机在我的电脑上运行。 从我的客户那里,如果我尝试添加服务引用,它会给我这个错误:

URI 前缀无法识别。 元数据包含无法解析的引用:“net.tcp://localhost:8090/”。 无法连接到 net.tcp://localhost:8090/。连接尝试持续了 00:00:04.0092984 的时间跨度。 TCP 错误代码 10061:无法建立连接,因为目标机器主动拒绝它 127.0.0.1:8090。 无法建立连接,因为目标机器主动拒绝它 127.0.0.1:8090 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

我在这里错过了什么?

【问题讨论】:

    标签: c# wcf nettcpbinding wcfserviceclient


    【解决方案1】:

    这是因为你使用了using,在创建servicehost后释放了servicehost。你可以把Console.ReadLine()放在using中,这样那 ServiceHost 没有立即释放:

    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();
                }
                
            }
    

    您也可以选择不使用使用

    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();
                }
    

    如果问题仍然存在,请随时告诉我。

    【讨论】:

    • 非常感谢!从我的角度来看,这是一个可怕的错误。
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多