WCF: What is a ServiceHost?

Andreas Grech picture Andreas Grech · May 3, 2009 · Viewed 45.1k times · Source

As I'm currently learning to use WCF Services, I am constantly encountering tutorials on the internet which mention using a ServiceHost when using a WCF Service.

What exactly is this ServiceHost ?


In my current project I am using a WCF Service and having a reference to it from my app and whenever I want to consume it from my app I just instantiate its ServiceClient like such:

new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), customBinding, endpointAddress);

And then access my web methods (OperationContracts) from that instance (obviously opening it before consuming the method and closing it afterwards with Open and Close)

My WCF service is host in my IIS and I just access the .svc from my app to instantiate the ServiceClient.

So why and where is ServiceHost used?

Answer

Andy White picture Andy White · May 3, 2009

A ServiceHost basically provides you everything you need to host a WCF service in a non-IIS or WAS setting. A common place for a ServiceHost would be in a console app or Windows service. See the example code from MSDN for how to setup a ServiceHost in a console app.