下面是我的 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 中使用“添加服务引用”,地址如下:
最佳答案
可以使用相同的基地址托管多个服务。设置 HttpHelpPageEnabled
和 HttpsHelpPageEnabled
的属性全部 ServiceDebugBehavior
转至 false
,那么它应该可以工作。
关注 :默认情况下,总是有一个 ServiceDebugBehavior
即使没有明确指定,也会在主机的行为描述集合中注册(我仅使用程序配置和 ServiceHost
类尝试了它,而不是通过 XML 配置)。所以,你应该添加一个明确的 ServiceDebugBehavior
并设置上述属性。 IncludeExceptionDetailInFaults
属性可以是true
.
关于WCF 错误 : A registration already exists for URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8328552/