For some testing code, I would like to be able to host the WCF service in just a few lines. I decided that I would write a simple hosting class:
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 (); } }
This can be used as follows:
using (var host = new WcfHost<ImplementationClass, ContractClass> ()) {
Is there something wrong with this approach? Is there a flaw in the code (especially about recycling)?
source share