謝謝,那是有用的代碼(+1)。
雖然它有點麻煩,但有一些錯誤(例如不應該是區分大小寫的檢查),有一些我不需要的UI功能,並重復了很多代碼。
我已經從中獲得了實際的發現機制,重新編寫了它,並且幾乎可以正常工作(連接,但需要一些修飾)。
首先由主法中使用的一些UTIL功能:
/// <summary>If the url doesn't end with a WSDL query string append it</summary>
static string AddWsdlQueryStringIfMissing(string input)
{
return input.EndsWith("?wsdl", StringComparison.OrdinalIgnoreCase) ?
input : input + "?wsdl";
}
/// <summary>Imports the meta data from the specified location</summary>
static ServiceEndpointCollection GetEndpoints(BindingElement bindingElement, Uri address, MetadataExchangeClientMode mode)
{
CustomBinding binding = new CustomBinding(bindingElement);
MetadataSet metadata = new MetadataExchangeClient(binding).GetMetadata(address, mode);
return new WsdlImporter(metadata).ImportAllEndpoints();
}
然後試圖不同的方式來連接和方法返回終點:
public static ServiceEndpointCollection Discover(string url)
{
Uri address = new Uri(url);
ServiceEndpointCollection endpoints = null;
if (string.Equals(address.Scheme, "http", StringComparison.OrdinalIgnoreCase))
{
var httpBindingElement = new HttpTransportBindingElement();
//Try the HTTP MEX Endpoint
try { endpoints = GetEndpoints(httpBindingElement, address, MetadataExchangeClientMode.MetadataExchange); }
catch { }
//Try over HTTP-GET
if (endpoints == null)
endpoints = GetEndpoints(httpBindingElement,
new Uri(AddWsdlQueryStringIfMissing(url)), MetadataExchangeClientMode.HttpGet);
}
else if (string.Equals(address.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
var httpsBindingElement = new HttpsTransportBindingElement();
//Try the HTTPS MEX Endpoint
try { endpoints = GetEndpoints(httpsBindingElement, address, MetadataExchangeClientMode.MetadataExchange); }
catch { }
//Try over HTTP-GET
if (endpoints == null)
endpoints = GetEndpoints(httpsBindingElement,
new Uri(AddWsdlQueryStringIfMissing(url)), MetadataExchangeClientMode.HttpGet);
}
else if (string.Equals(address.Scheme, "net.tcp", StringComparison.OrdinalIgnoreCase))
endpoints = GetEndpoints(new TcpTransportBindingElement(),
address, MetadataExchangeClientMode.MetadataExchange);
else if (string.Equals(address.Scheme, "net.pipe", StringComparison.OrdinalIgnoreCase))
endpoints = GetEndpoints(new NamedPipeTransportBindingElement(),
address, MetadataExchangeClientMode.MetadataExchange);
return endpoints;
}
「我知道MS故意讓這個過於複雜」[引用需要] – 2008-09-19 06:36:14
也許這是不公平的。 MS有意使它成爲每一個你想改變的設置都可以,但是你需要一個WCF書籍和編程經驗來設置基礎知識。 – Keith 2008-09-19 12:17:08