这真的困扰了我几个小时。我使用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
我搜索过谷歌,但什么也没找到。
谢谢!
编辑:
“默认网站”的“绑定”屏幕如下所示:

最佳答案

当您创建使用nettcpbinding的服务并希望在visual studio中添加服务引用时,应该使用http地址(httpgetenabled),而不是该服务侦听的实际tcp地址。因此,解决方案是在“添加服务引用”对话框中将localhost/wcftcpservice/testtcpservice.svc设置为url。

10-07 19:00
查看更多