ChannelFactoryにチャネルを作成させ、使用後にそれらを破棄する機能を備えたクリーンな方法を探しています。
これは私が得たものです:
public class ClientFactory : IClientFactory
{
private const string endpointName = "IMyService";
private readonly ChannelFactory<IMyService> _factory;
public ClientFactory()
{
_factory = new ChannelFactory<IMyService>(endpointName);
}
public Client<IMyService> GetClient()
{
IMyService channel = _factory.CreateChannel();
return new Client<IMyService>(channel);
}
}
public class Client<T> : IDisposable
{
public T Channel { get; private set; }
public Client(T channel)
{
if (channel == null)
throw new ArgumentException("channel");
Channel = channel;
}
public void Dispose()
{
(Channel as IDisposable).Dispose();
}
}
//usage
using (var client = _serviceFactory.GetClient())
{
client.Channel.DoStuff();
}
これは良い解決策ですか?
これを行うためのよりクリーンな方法はありますか?