WCF InvalidOperationException:绑定实例已与侦听URI相关联

Shi*_*ada 9 wcf

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

我在使用ServiceContract NameSpace和Name时遇到了问题.当我运行代码时,我发现了一个波纹管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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 的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>
    
    Run Code Online (Sandbox Code Playgroud)

谢谢.

Sir*_*lly 21

两个端点(基本端口和mex端口)不能位于同一地址.为其中一个(或两个)添加一些特定地址.

例如:

<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
Run Code Online (Sandbox Code Playgroud)


tom*_*ern 5

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

<endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
Run Code Online (Sandbox Code Playgroud)

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