对于一些测试代码,我希望能够在几行中托管 WCF 服务。我想我会写一个简单的托管类:
public class WcfHost<Implementation, Contract> : IDisposable
where Implementation : class
where Contract : class
{
public readonly string Address = "net.tcp://localhost:8000/";
private ServiceHost _Host;
public WcfHost ()
{
_Host = new ServiceHost (typeof (Implementation));
var binding = new NetTcpBinding ();
var address = new Uri (Address);
_Host.AddServiceEndpoint (
typeof (Contract),
binding,
address);
_Host.Open ();
}
public void Dispose ()
{
((IDisposable) _Host).Dispose ();
}
}
可以这样使用:
using (var host = new WcfHost<ImplementationClass, ContractClass> ()) {
这种方法有什么问题吗?代码中是否存在缺陷(尤其是关于处置)?