找不到与终结点的方案 net.tcp 匹配的基址

本文关键字:tcp 方案 结点 找不到 net | 更新日期: 2023-09-27 18:28:06

我有一个 Wcf 服务项目。 我的 web.config 中的system.serviceModel标签是:

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="RCISPNetTcpBinding" openTimeout="00:10:00" sendTimeout="00:10:00">
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign">
            </transport>
            <message clientCredentialType="Windows"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="RCISP.WcfServiceBehaviour" name="RCISP.WcfService.PersonWcfService">
        <endpoint address="PersonServices" binding="netTcpBinding" bindingConfiguration="RCISPNetTcpBinding"
          contract="RCISP.Common.ServiceContract.IPersonService" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:40003/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RCISP.WcfServiceBehaviour">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceAuthorization principalPermissionMode="UseWindowsGroups" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

当我想为服务创建自托管时,我遇到了运行时错误。

    public class ServiceFactory : ServiceHostFactory
    {
       protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
       {
          if (!IsInitialised) InitialiseService();
             return base.CreateServiceHost(serviceType, baseAddresses);
       }
    }

异常消息是:

Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].

为什么?

找不到与终结点的方案 net.tcp 匹配的基址

您说您是自托管服务(在您自己的进程中(,但根据上面的代码,我认为您正在尝试将其托管在 Web 应用程序中。如果是这样,Visual Studio 的 Web 服务器无法托管 net.tcp 端点。需要将项目设置为在 IIS 下运行。请按照此处的说明进行操作。

如果您确实是自托管,则可以完全保留配置,并使用以下代码启动服务:

var host = new ServiceHost(typeof(Service1));
host.Open();

您需要将协议映射添加到配置文件中,以告诉它"net.tcp"协议是什么。在您的<bindings>部分之后,立即添加以下内容:

<protocolMapping>
  <add scheme="net.tcp" binding="netTcpbinding"/>
</protocolMapping>