Selecting the ways of hosting is dependant upon the transport protocol and the feature you expect in the hosting environment, There are many more features will be needed to make the service a robust host. Few important features like Health Monitoring, Process Recycling,Process Activation and Idle Time Management are highly recommended to host the service.I will not detail out the feature here but i will brief out.
Process Recycling - If the IIS finds the service is not healthy enough due to the memory leak or other factors ,it will restart the worker process and avoiding the entire system to get into the problem
Process Activation - Worker process or other processes will be triggered only the request arrives. Unlike SelfHosting ,the host process should always be started up and listening for incoming requests. There is always a big debate on Which hosting will be better for your service? of -course, it's an interesting to discuss.But i don't want to hijack the topic, we will discuss this in the series of my articles in hosting.
Service Host
Service Host is a part of System.ServiceModel Namespace. ServiceHost instance associates one or more services to be accessible at run- time. When you are trying to do hosting in a console application(self-hosting), then you need to use the class Service Host. If you are trying to host your service in WAS or IIS, WAS or IIS will use the instance ServiceHost on behalf of you. ServiceHost will be initialized with the parameters ServiceType,Service EndPoints,Base Address and Behaviour and creates a service description that is a part of System.ServiceModel. The ServiceDescription is responsible for creating the description of the service that includes the types of service,endpoint and behaviors, and uses that description to create the channel dispatcher objects and WSDL document so that the client can access the exposed services. The following diagram shows the process of service host.
The diagram above tells exactly what I have explained you in the sub-topic Service Host.
Self Hosting
The simplest way to host your services is self-hosting that can suffice using the Winforms, Console applications and windows services. The Developer must simply instantiate the Service Host class that is derived from system.servicemodel. Service Host will be opened when it will be instantiated with the parameters service type,contract type and endpoints.Once it is opened it will allocate the work process to receive the incoming messages from the client and serve it. Ok, let's move on to discuss the steps required for the Self Hosting.
Step 1
In the First step, you can find out that New Host Project will be created. For the Demonstration purpose ,i have selected the console application in windows templates. you can also select the Host project as Windows Form application or WPF application. The following snapshot provides a clear picture on first step.
Step 2
In the second step, you can find out that ServiceHost is instantiated with the service type. In our Demonstration ,you can find that the ServiceType parameter(Implementation) is passed to the ServiceHost class. This call will create the service to communicate in the communication protocol mentioned(wsHttpBinding) with the DataContract ILibrary to host in the address mentioned in the BaseAddress
You can also find out that Event handlers have been assigned to the service host class and it will be fired if the ServiceHost is opened or closed appropriately. Please ensure that servicetype(implementation) declaration is happened since it mentioned in the Host App.config file, You can also do it programmatically.
Here with I have attached the SourceCode as well,you can have a look at that. Self Hosting is very simple and you don't require too many steps to host it. Yes, That's it, am going to end this article, thought of giving a detailed explanation on WAS Hosting and IIS Hosting in my next article.
Summary
Service Host is a part of System.ServiceModel .This class will be instantiated by the developer if he needs to do SelfHosting. This class will be instantiated by IIS if the service is hosted in the way of IIS or WAS.