WCF 错误 : A registration already exists for URI

标签 wcf

下面是我的 WCF 服务配置。我使用 2 ServiceHost 来托管 2 种服务类型。它们使用相同的基地址,但它们的端点使用不同的相对地址。

但是我收到了这个错误,为什么?

服务无法启动。 System.InvalidOperationException: 'http://earth:1111/' 的 ChannelDispatcher 与契约(Contract) '"IHttpGetHelpPageAndMetadataContract"' 无法打开其 IChannelListener。 ---> System.InvalidOperationException: URI 'http://earth:1111/' 的注册已经存在。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="Distributed.Troubleshooting.System.IIS.IISServiceType">
        <endpoint address="iis" binding="basicHttpBinding" name="iis"
          contract="Distributed.Troubleshooting.System.IIS.IISServiceContract" />
        <endpoint address="iismex" binding="mexHttpBinding" bindingConfiguration=""
          name="iismex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://Earth:1111/" />
          </baseAddresses>
        </host>
      </service>
      <service behaviorConfiguration="serviceBehavior" name="Distributed.Troubleshooting.System.SQL.SQLServiceType">
        <endpoint address="sql" binding="basicHttpBinding" name="sql"
          contract="Distributed.Troubleshooting.System.SQL.SQLServiceContract" />
        <endpoint address="sqlmex" binding="mexHttpBinding" bindingConfiguration=""
          name="sqlmex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://Earth:1111/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

一些更荒谬的发现:

我将我的配置更改为:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="Distributed.Troubleshooting.System.IIS.IISServiceType">
        <endpoint address="http://Earth:1111/iis" binding="basicHttpBinding" name="iis"
          contract="Distributed.Troubleshooting.System.IIS.IISServiceContract" />
        <endpoint address="http://Earth:1111/iismex" binding="mexHttpBinding" bindingConfiguration=""
          name="iismex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://Earth:1111/iis" />
          </baseAddresses>
        </host>
      </service>
      <service behaviorConfiguration="serviceBehavior" name="Distributed.Troubleshooting.System.SQL.SQLServiceType">
        <endpoint address="http://Earth:1111/sql" binding="basicHttpBinding" name="sql"
          contract="Distributed.Troubleshooting.System.SQL.SQLServiceContract" />
        <endpoint address="http://Earth:1111/sqlmex" binding="mexHttpBinding" bindingConfiguration=""
          name="sqlmex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://Earth:1111/sql" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

然后我发现我可以在 Visual Studio 中使用“添加服务引用”,地址如下:
  • http://Earth:1111/iis
  • http://Earth:1111/iismex
  • http://Earth:1111/sql
  • http://Earth:1111/sqlmex
  • 最佳答案

    可以使用相同的基地址托管多个服务。设置 HttpHelpPageEnabledHttpsHelpPageEnabled 的属性全部 ServiceDebugBehavior转至 false ,那么它应该可以工作。

    关注 :默认情况下,总是有一个 ServiceDebugBehavior即使没有明确指定,也会在主机的行为描述集合中注册(我仅使用程序配置和 ServiceHost 类尝试了它,而不是通过 XML 配置)。所以,你应该添加一个明确的 ServiceDebugBehavior并设置上述属性。 IncludeExceptionDetailInFaults属性可以是true .

    关于WCF 错误 : A registration already exists for URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8328552/

    相关文章:

    c# - 为什么 WCF 在遇到 302 响应时无法调用 SOAP 服务?

    wpf - 此 WCF 错误 : "Custom tool warning: Cannot import wsdl:portType" 是什么意思

    asp.net - 如何拦截对 WCF .svc 服务中方法的所有调用?

    asp.net-mvc - 在 .NET 中实现基于 REST 的 Web 服务的最佳方法是什么?

    asp.net - WCF REST 服务模板 40 和 JSON 格式的 Entity Framework 返回空响应

    c# - 尝试 open() sql server 时出错

    c# - WCF 需要哪种类型的安全性

    wpf - 在 MVVM 中调用 WCF 服务?

    c# - 如何将 asmx 服务转换为 WCF 服务

    c# - 无法通过 WCF 访问类