Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: What is a ServiceHost?

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?

like image 574
Andreas Grech Avatar asked May 03 '09 03:05

Andreas Grech


People also ask

What is the role of a ServiceHost class?

The ServiceHost class creates a ServiceDescription from the service type and configuration information and then uses that description to create ChannelDispatcher objects for each endpoint in the description.

What is self hosting in WCF?

This is referred to as a self hosting WCF service, the exact meaning of Self Hosted is that it hosts the service in an application that could be a Console Application or Windows Forms and so on. Earlier we saw what a WCF Service is in the . Net environment. We can host a WCF service in IIS and a Windows service also.

What are 3 basic WCF configurations required for hosting a WCF service?

There are three types of hosting environments for WCF services: IIS, WAS, and self-hosting. The term “self-hosting” refers to any application that provides its own code to initialize the hosting environment. This includes console, Windows Forms, WPF, and managed Windows services.

What is the need for the activation or hosting of a WCF service?

WCF uses the listener adapter interface to communicate activation requests that are received over the non-HTTP protocols supported by WCF, such as TCP, named pipes, and Message Queuing.


2 Answers

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.

like image 143
Andy White Avatar answered Oct 02 '22 23:10

Andy White


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

like image 34
marc_s Avatar answered Oct 02 '22 21:10

marc_s