【问题标题】:WCF InvalidOperationException: A binding instance has already been associated to listen URIWCF InvalidOperationException:绑定实例已关联到侦听 URI
【发布时间】:2012-07-15 09:45:54
【问题描述】:

我是 WCF 的初学者,正在学习 Essential WCF。

我在使用 ServiceContract 名称空间和名称时遇到问题。 当我运行代码时,我捕捉到了下面的 InvalidOperationException。但是我没看清楚。

绑定实例已关联到侦听 URI 'http://localhost:8080/NamespaceChange01'。如果两个端点想要共享同一个 ListenUri,它们也必须共享同一个绑定对象实例。两个冲突的端点要么在 AddServiceEndpoint() 调用中指定,要么在配置文件中指定,要么在 AddServiceEndpoint() 和 config 的组合中指定。

有人知道如何避免 InvalidOperationException 吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace NamespaceChange01
{

    [ServiceContract(Name = "MyServiceName", Namespace = "http://ServiceNamespace")]
    public interface IBurgerMaster
    {
        [return: MessageParameter(Name = "myOutput")]
        [OperationContract(Name = "OperationName", Action = "OperationAction", ReplyAction = "ReplyActionName")]
        double GetStockPrice(string ticker);
    }

    [ServiceBehavior(Namespace = "http://MyService")]
    public class BurgerMaster : IBurgerMaster
    {

        public double GetStockPrice(string ticker)
        {
            return 100.99;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(BurgerMaster));
            host.Open();
            Console.ReadLine();
            host.Close();
        }
    }
}
  • app.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="NamespaceChange01.BurgerMaster" behaviorConfiguration="mexServiceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/NamespaceChange01"/>
              </baseAddresses>
            </host>
            <endpoint name="basic" binding="basicHttpBinding" contract="NamespaceChange01.IBurgerMaster"/>
            <endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mexServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    

谢谢。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    我知道这是一个老问题,但我最近遇到了一个非常相似的问题,谷歌找到了我,而且我有一个不同的潜在解决方案:

    由于相关服务的 service 标记完全缺失,并且在 IIS 中为其设置了 HTTP 和 HTTPS 绑定,我遇到了所描述的完全相同的错误消息。

    当然,添加service 标签可以解决这个问题。有趣的是,删除 IIS 中的 HTTPS 绑定使 HTTP 版本可用(尽管这显然是不可取的)。

    【讨论】:

      【解决方案2】:

      两个端点(basic 和 mex)不能在同一个地址上。为其中一个(或两个)添加一些特定地址。

      例如:

      <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      

      【讨论】:

        【解决方案3】:

        您的元数据端点缺少 address 属性:

        <endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
        

        没有它,WCF 认为您希望将 mex 端点托管在同一地址。

        【讨论】:

        • 客户端应用程序使用 mexHttpsBinding 和服务器使用 wsHttpsBinding ,我也收到同样的错误“客户端和服务绑定不匹配”
        【解决方案4】:

        在创建您的服务类时,您为什么要像代码中所说的那样使用 ServiceContract 属性对其进行标记?

        [ServiceBehavior(Namespace = "http://MyService")]
        public class BurgerMaster : IBurgerMaster
        

        请删除那个并重试。

        【讨论】:

        • -1 这是不正确的 - OP 的代码完全有效。是配置不正确
        猜你喜欢
        • 2012-11-18
        • 2019-12-02
        • 2021-07-09
        • 1970-01-01
        • 2011-06-09
        • 2011-09-24
        • 2013-12-18
        • 2015-12-15
        • 2018-12-15
        相关资源
        最近更新 更多