System.InvalidOperationException: '绑定实例已经被关联到监听

Posted

技术标签:

【中文标题】System.InvalidOperationException: \'绑定实例已经被关联到监听【英文标题】:System.InvalidOperationException: 'A binding instance has already been associated to listenSystem.InvalidOperationException: '绑定实例已经被关联到监听 【发布时间】:2021-07-09 05:03:11 【问题描述】:

我刚刚为一个项目启动了 WCF,它显示了以下异常,但是我发现了一些与此问题相关的问题,但我无法在我的代码中找到,我将代码放在这里,请查看并提出建议我的代码的最佳解决方案。

    <system.serviceModel>
    <services>
      <service name="CompanyService.CompanyService" behaviorConfiguration="mexBehaviour">
        <endpoint address="CompanyService" binding="basicHttpBinding" contract="CompanyService.ICompanyPublicService"></endpoint>
        <endpoint address="CompanyService" binding="netHttpBinding" contract="CompanyService.ICompanyConfidentialService"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
            <add baseAddress="net.tcp://localhost:8091/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

下面是错误异常:

'A binding instance has already been associated to listen URI 'http://localhost:8080/CompanyService'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config. '

【问题讨论】:

检查这个question。您似乎遇到了同样的问题。 【参考方案1】:

需要修改端点地址的名称:

    <services>
      <service name="CompanyService.CompanyService" behaviorConfiguration="mexBehaviour">
        <endpoint address="CompanyService01" binding="basicHttpBinding" contract="CompanyService.ICompanyPublicService"></endpoint>
        <endpoint address="CompanyService02" binding="netHttpBinding" contract="CompanyService.ICompanyConfidentialService"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
            <add baseAddress="net.tcp://localhost:8091/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

【讨论】:

以上是关于System.InvalidOperationException: '绑定实例已经被关联到监听的主要内容,如果未能解决你的问题,请参考以下文章

视图中出现 InvalidOperationException.Collection 被修改错误