C# Net TCP 绑定:无法识别 URI 前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17886007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:44:21  来源:igfitidea点击:

Net TCP binding: The URI prefix is not recognized

c#.netwcfwcf-bindingnettcpbinding

提问by Nullius

This really is bugging me for a couple of hours. I created the simplest WCF service using a TCP binding.

这真的困扰了我几个小时。我使用 TCP 绑定创建了最简单的 WCF 服务。

namespace WcfTcpService
{
    public class TestTcpService : ITestTcpService
    {
        public string Hello(string name)
        {
            return "Hello, " + name + "!";
        }
    }
}

namespace WcfTcpService
{
    [ServiceContract]
    public interface ITestTcpService
    {
        [OperationContract]
        string Hello(string name);
    }
}

Web.config file has the following section:

Web.config 文件包含以下部分:

  <system.serviceModel>
    <services>
      <service name="WcfTcpService.TestTcpService" behaviorConfiguration="TestServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:808/WcfTcpService.TestTcpService" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="WcfTcpService.ITestTcpService"></endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"></endpoint>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="tcpBinding" portSharingEnabled="true">
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--<protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
      <add binding="netTcpBinding" scheme="net.tcp" />
    </protocolMapping>-->    
    <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />-->
  </system.serviceModel>

This service is hosted in IIS:

此服务托管在 IIS 中:

detailed settings

详细设置

Now, when trying to add a reference to net.tcp://localhost:808/WcfTcpService.TestTcpServicefrom a client application, I keep receiving the error:

现在,当尝试net.tcp://localhost:808/WcfTcpService.TestTcpService从客户端应用程序添加引用时,我不断收到错误消息:

The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost/WcfTcpService.TestTcpService'.
The message could not be dispatched because the service at the endpoint address 'net.tcp://localhost/WcfTcpService.TestTcpService' is unavailable for the protocol of the address.
If the service is defined in the current solution, try building the solution and adding the service reference again.

The net.tcp service is running, I receive the same error with WcfTestClient.exe. and I can succesfully run http://localhost/WcfTcpService/TestTcpService.svc.

net.tcp 服务正在运行,我收到与 WcfTestClient.exe 相同的错误。我可以成功运行http://localhost/WcfTcpService/TestTcpService.svc

I've searched google but nothing came up.

我已经搜索过谷歌,但没有任何结果。

Thanks!

谢谢!

EDIT:

编辑:

The bindings screen of the 'Default Web site' looks like this btw:

“默认网站”的绑定屏幕看起来像这样顺便说一句:

bindings

绑定

采纳答案by empi

When you create service that uses netTcpBinding and you want to Add service reference in Visual Studio you should use http address (httpGetEnabled) not actual tcp address the service listens on. So the solution was to set localhost/WcfTcpService/TestTcpService.svc as an url in Add service reference dialog.

当您创建使用 netTcpBinding 的服务并且想要在 Visual Studio 中添加服务引用时,您应该使用 http 地址 (httpGetEnabled) 而不是服务侦听的实际 tcp 地址。因此,解决方案是在 Add service reference 对话框中将 localhost/WcfTcpService/TestTcpService.svc 设置为 url。

回答by AR.Alimardan

I had same problem and I changed web.config as below:

我有同样的问题,我改变了 web.config 如下:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
      <baseAddressPrefixFilters>
        <add prefix="net.tcp://YourBaseUrl"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>