C# WCF相对端点地址在所有基址上公开,无论使用何种绑定

C# WCF相对端点地址在所有基址上公开,无论使用何种绑定,c#,.net,wcf,C#,.net,Wcf,我有这个自助服务。在app.config中,我定义了两个基本地址。一个用于http,一个用于net.tcp 一个契约通过两个端点公开,一个具有basicHttpBinding,另一个具有netTcpBinding 现在,奇怪的是两个端点在两个基地址都可用。如果使用wcfclient.exe应用程序连接到任一端点,则会显示两个端点。Ie basicHttpBinding通过net.tcp和其他方式 为什么会这样?我能做些什么吗 该配置文件可供参考 <configuration> <

我有这个自助服务。在app.config中,我定义了两个基本地址。一个用于http,一个用于net.tcp

一个契约通过两个端点公开,一个具有basicHttpBinding,另一个具有netTcpBinding

现在,奇怪的是两个端点在两个基地址都可用。如果使用wcfclient.exe应用程序连接到任一端点,则会显示两个端点。Ie basicHttpBinding通过net.tcp和其他方式

为什么会这样?我能做些什么吗

该配置文件可供参考

<configuration>
<system.serviceModel>
<services>
  <service name="FileServer.BookService" behaviorConfiguration="serviceMetadata">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost"/>
        <add baseAddress="net.tcp://localhost"/>
      </baseAddresses>
    </host>
    <endpoint address="BookService"
              binding="netTcpBinding"
              contract="FileServer.IBookService">
    </endpoint>
    <endpoint address="BookService/mex"
              binding="mexTcpBinding"
              contract="IMetadataExchange">
    </endpoint>
    <endpoint address="BookService"
      binding="basicHttpBinding"
      contract="FileServer.IBookService">
    </endpoint>
    <endpoint address="BookService/mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange">
    </endpoint>
    <endpoint address="basic"
              binding ="basicHttpBinding"
              contract="FileServer.ITest">
    </endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceMetadata">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

WcfTestClient应用程序将从服务下载元数据信息,而服务本身有三个端点-因此客户端显示的是三个端点,而不是两个服务。即使您通过两个不同的位置公开元数据,元数据也不是相对于端点或基址,而是相对于服务主机本身

如果希望从HTTP地址获取元数据的客户端仅获取HTTP端点(TCP也是如此),则可以使用两台主机,如下例所示

public class Post_09851985_ee54_4627_9af7_6a9505c2067f
  {
    [DataContract]
    public class Person
    {
      [DataMember]
      public string name;
      [DataMember]
      public string address;
    }
    [ServiceContract]
    public interface ITest
    {
      [OperationContract]
      Person Echo(Person person);
    }
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Service : ITest
    {
      public Person Echo(Person person)
      {
        return person;
      }
    }
    public static void SingleHost()
    {
      string baseAddressHttp = "http://" + Environment.MachineName + ":8000/Service";
      string baseAddressTcp = "net.tcp://" + Environment.MachineName + ":8008/Service";
      ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddressHttp), new Uri(baseAddressTcp));
      host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
      host.AddServiceEndpoint(typeof(ITest), new NetTcpBinding(SecurityMode.None), "tcp");

      host.Description.Behaviors.Add(new ServiceMetadataBehavior());
      host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
      host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

      host.Open();
      Console.WriteLine("Host opened");

      Console.WriteLine("Press ENTER to close");
      Console.ReadLine();

      host.Close();
    }
    public static void TwoHosts()
    {
      string baseAddressHttp = "http://" + Environment.MachineName + ":8000/Service";
      string baseAddressTcp = "net.tcp://" + Environment.MachineName + ":8008/Service";
      ServiceHost httpHost = new ServiceHost(typeof(Service), new Uri(baseAddressHttp));
      ServiceHost tcpHost = new ServiceHost(typeof(Service), new Uri(baseAddressTcp));
      httpHost.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
      tcpHost.AddServiceEndpoint(typeof(ITest), new NetTcpBinding(SecurityMode.None), "tcp");

      httpHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
      tcpHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
      httpHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
      tcpHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

      httpHost.Open();
      tcpHost.Open();
      Console.WriteLine("Host opened");

      Console.WriteLine("Press ENTER to close");
      Console.ReadLine();

      httpHost.Close();
      tcpHost.Close();
    }
    public static void Test()
    {
      TwoHosts();
    }
  }