[英]WCF relative endpoint address exposed over all base addresses no matter binding used
我有這項自助服務。 在app.config中,我定義了兩個基地址。 一個用於http,一個用於net.tcp。
一個合同通過兩個端點公開,一個端點帶有basicHttpBinding,另一個端點帶有netTcpBinding。
現在,奇怪的是兩個端點在兩個基址上都可用。 如果我使用wcfclient.exe應用程序連接到任一端點,則兩個端點都將顯示。 即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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.