【问题标题】:WCF relative endpoint address exposed over all base addresses no matter binding used无论使用何种绑定,WCF 相对端点地址都暴露在所有基地址上
【发布时间】:2011-07-14 17:10:41
【问题描述】:

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

一个合约通过两个端点公开,一个带有 basicHttpBinding,另一个带有 netTcpBinding。

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

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

供参考的配置。

<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>

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

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

    如果您希望客户端从 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();
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2013-09-14
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多