我正在寻找一种干净的方式让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();
}
这是一个好的解决方案吗?
有没有更清洁的方法来做到这一点?