私の現在の製品コードでは、msdnのドキュメントによると、クライアントを作成する方法は次のとおりです。
using (WebChannelFactory<IServiceInterface> cf
= new WebChannelFactory<IServiceInterface>("http://service.url"))
{
IServiceInterface client = cf.CreateChannel();
client.CallTheMethod();
}
私がこのインターフェースを持っているとすると:
public interface IServiceInterface
{
void CallTheMethod();
}
ただし、WebChannelFactoryによって作成されたオブジェクトクライアントもIDisposableを実装していることに気付きました。だから私もこのオブジェクトを処分したいと思います。私は他の方法を見つけられませんでした:
using (WebChannelFactory<IServiceInterface> cf
= new WebChannelFactory<IServiceInterface>("http://service.url"))
using(IDisposable client = (IDisposable)cf.CreateChannel())
{
((IServiceInterface)client).CallTheMethod();
}
これは醜いです。それで :
- 本当に処分する必要がありますか?つまり、ファクトリを破棄したときに破棄される可能性があります(ファクトリが、作成したすべてのオブジェクトへの参照を保持している場合)?
- はいの場合、より良い方法がありますか?