WCFクライアントの場合、IServiceProxyFactory
資格情報を設定するためのインターフェイスがあります。
public interface IServiceProxyFactory<T>
{
T GetServiceProxy();
}
public class ServiceProxy1 : IServiceProxyFactory<ServiceClient1>
{
public ServiceClient1 GetServiceProxy()
{
var client = new ServiceClient1();
// set credentials here
return client;
}
}
public class ServiceProxy2 : IServiceProxyFactory<ServiceClient2> {
// ...
}
質問からWCFクライアントの「使用」ブロックの問題に対する最善の回避策は何ですか?、そして私は次のようにヘルパーを作成しました:
public static class Service<TProxy, TClient>
where TProxy : IServiceProxyFactory<TClient>, new()
where TClient : ICommunicationObject
{
public static IServiceProxyFactory<TClient> proxy = new TProxy();
public static void Use(Action<TClient> codeBlock)
{
TClient client = default(TClient);
bool success = false;
try
{
client = proxy.GetServiceProxy();
codeBlock(client);
((ICommunicationObject)client).Close();
success = true;
}
finally
{
if (!success)
{
((ICommunicationObject)client).Abort();
}
}
}
}
そして、私はヘルパーを次のように使用します。
Service<ServiceProxy1, ServiceClient1>.Use(svc => svc.Method());
質問:
TClient
またはTProxy
(更新された)タイプを削除して、次を使用して呼び出すことができる方法はありますか?Service<ServiceProxy1>.Use(svc => svc.Method());
または(更新)
Service<ServiceClient1>.Use(svc => svc.Method());
ICommunicationObject
とに使用するよりも良い方法はClose()
ありAbort()
ますか?