Net TCP绑定:未识别URI前缀

8
这个问题困扰我已经几个小时了。我使用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文件具有以下部分:

  <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>

这个服务是托管在IIS中的:

详细设置

现在,当我尝试从客户端应用程序添加对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.

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

我已经在谷歌上搜索过,但没有找到相关结果。

谢谢!

注:

'Default Web site' 的绑定屏幕如下所示:

bindings


1
当您选择“添加服务引用”时,是否遇到此错误?请在此对话框中尝试使用地址http://localhost/WcfTcpService/TestTcpService.svc。 - empi
检查IIS/IIS Express中的绑定。请参考此问题:https://dev59.com/k3A75IYBdhLWcg3wo6rx - nakchak
@empi 你一定在开玩笑吧...那个方法居然有效。如果你把它作为解决方案发布,我会接受的。谢谢! - Nullius
@nakchak 你可能太晚看到我发布问题后的编辑了,但是绑定是正确的。无论如何还是谢谢! - Nullius
@Nullius添加了答案,以便其他人能够轻松找到解决方案。 - empi
2个回答

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

1
为了完整起见:实际上是 net.tcp://localhost/WcfTcpService/TestTcpService.svc 解决了问题。 - Nullius

0

我曾经遇到过同样的问题,我将web.config更改如下:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接