Is this a correct way to host a WCF service?

Posted by mafutrct on Stack Overflow See other posts from Stack Overflow or by mafutrct
Published on 2010-04-12T09:50:48Z Indexed on 2010/04/12 9:53 UTC
Read the original article Hit count: 361

Filed under:
|
|

For some testing code, I'd like to be able to host a WCF service in only a few lines. I figured I'd write a simple hosting class:

public class WcfHost : 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 (); } }

That can be used like this:

using (var host = new WcfHost<ImplementationClass, ContractClass> ()) {

Is there anything wrong with this approach? Is there a flaw in the code (esp. about the disposing)?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about c#