39

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?

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360

2 Answers2

33

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.

Stacked
  • 6,892
  • 7
  • 57
  • 73
Andy White
  • 86,444
  • 48
  • 176
  • 211
  • 18
    Almost correct - even IIS and WAS will use a ServiceHost to host your WCF Service - they just create it and tear it down automagically behind your back. – marc_s May 03 '09 at 08:15
  • 4
    Yeah, I was just trying to describe when you'd see a ServiceHost in your own code. The IIS/WAS ServiceHost is not usually accessed directly by the developer. – Andy White May 03 '09 at 09:43
  • Yeah, that's right - the ServiceHost is almost invisible in the IIS/WAS scenario :) – marc_s May 03 '09 at 12:21
  • 16
    I love that shitty page by MS "provides a host for services" wtf. Thanks for nothing Microsoft. I learned nothing on that page. – PositiveGuy Oct 31 '13 at 03:35
  • 1
    You have to look toward the bottom of the MS page for the Remarks and Examples sections, which provide a better explanation than just "Provides a host for services." – kevinpo Jun 06 '14 at 13:22
29

Your service implementation is just a .NET class - you need to have a runtime environment for it, so it can be executed somehow. That's what the ServiceHost is for - it will load your service class, set up the endpoints and channel listeners and all that stuff, and thus give your service class an "ecosystem" to live and operate in.

You can either instantiate a ServiceHost class yourself in a console app, a Windows service, or even a Winforms app, and thus make your WCF service class available to the outside world - or you can delegate that work to IIS or WAS. Even IIS or WAS will use a ServiceHost to host your WCF service - they just do it automagically behind the scenes, and "on demand" - whenever a request for your WCF service comes in.

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    would you use that in TDD tests where you want to test calls such as sending in specific headers or data to the http request (WebOperationContext) to wcf service methods? – PositiveGuy Oct 31 '13 at 03:35