自己ホスト型サービスでは、App.configに指定されているエンドポイント(存在する場合)、またはApp.configが空の場合はコードに指定されたデフォルトのエンドポイントを使用したいと思います。これどうやってするの?サービスの既定のWCFエンドポイントをハードコードする方法
を編集します。明確にするため、これはServiceHostを使用してサーバー(サービス)側にあります。
自己ホスト型サービスでは、App.configに指定されているエンドポイント(存在する場合)、またはApp.configが空の場合はコードに指定されたデフォルトのエンドポイントを使用したいと思います。これどうやってするの?サービスの既定のWCFエンドポイントをハードコードする方法
を編集します。明確にするため、これはServiceHostを使用してサーバー(サービス)側にあります。
片方向はtry
で、最初にconfigファイルからロードし、エンドポイントをcatch
にハードコードすることです。 EG:
MyServiceClient client = null;
try
{
client = new MyServiceClient();
}
catch (InvalidOperationException)
{
EndpointAddress defaultAddress = new EndpointAddress(...);
Binding defaultBinding = new Binding(...);
client = new MyServiceClient(defaultBinding, defaultAddress);
}
あなたは以下のように構成セクションを取得することができます。
var clientSection = System.Configuration.ConfigurationManager.GetSection("system.serviceModel/client");
値がnullであるかclientSection.Endpoints
がゼロの要素が含まれている場合は、それが定義されていない場合。
試してみてください...テストされていませんが、あなたのために働く必要があります。一致する契約を持つエンドポイントの構成をチェックします。あなたは名前で一致させたり、異なる情報を返すなど、あなたの状況に合ったものに変更することができます。一致するものが見つからない場合、ロジックを配置してデフォルトのエンドポイントを作成できます。
public List<EndpointAddress> GetEndpointAddresses(Type t)
{
string contractName = t.FullName;
List<EndpointAddress> endpointAddresses = new List<EndpointAddress>();
ServicesSection servicesSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
foreach (ServiceElement service in servicesSection.Services)
{
foreach (ServiceEndpointElement endpoint in service.Endpoints)
{
if (string.Compare(endpoint.Contract, contractName) == 0)
{
endpointAddresses.Add(new EndpointAddress(endpoint.Address));
}
}
}
if (endpointAddresses.Count == 0)
{
//TODO: Add logic to determine default
endpointAddresses.Add(new EndpointAddress("Your default here"));
}
return endpointAddresses;
}
app.configファイルを使用せずにスタンドアロンサービスクライアントを実装する場合、同じ種類の問題に直面しました。最後に私はそれを並べ替えることができました。以下のコードサンプルに従ってください。それは正常に動作しており、私はそれをテストしました。
BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_ITaskService";
binding.CloseTimeout = TimeSpan.FromMinutes(1);
binding.OpenTimeout = TimeSpan.FromMinutes(1);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.SendTimeout = TimeSpan.FromMinutes(1);
binding.AllowCookies = false;
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferSize = 65536;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 65536;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = "";
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
Uri endPointAddress = new Uri("http://www.kapanbipan.com/TaskService.svc");
ChannelFactory<taskmgr.TaskService.ITaskServiceChannel> factory = new ChannelFactory<ITaskServiceChannel>(binding, endPointAddress.ToString());
taskmgr.TaskService.ITaskServiceChannel client = factory.CreateChannel();
ありがとうございますが、私はサーバー側のコードを探していました。クライアントでもそれをやってくれるかもしれません:) – Qwertie
自己ホスティングサービス(ServiceHostを使用している場合)の場合、同じ方法が有効です。 –