ありがとう、それは便利なコード(+1)でした。
これはちょっと面倒ですが、いくつかのバグ(大文字と小文字を区別する必要のないチェックなど)があり、必要のないUI機能があり、たくさんのコードを繰り返します。
私はそれから実際の発見メカニズムを取り、それを書き直して、ほとんどそれを働かせました(接続していますが、いくつかのフィッシングが必要です)。 mainメソッドが使用する
まず、いくつかの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がこれを意図的に複雑にしたのは知っている" –
多分それは不公平だ。 MSは意図的に変更することができるすべての設定を可能にしましたが、基本設定にはWCFの本とプログラミングの経験が必要です。 – Keith