In this section, we will discuss Web Hosting. Now, in case of Web-Hosting IIS is going to all our hosting. It instantiates and manages the ServiceHost instance automatically. One point to note here that IIS is limited to HTTP. Nevertheless, with the use of WAS (Windows Application Services) we can use other bindings as well. Without, WAS we are using IIS for external web services. In WCF, we used to require .SVC file for browsing point. However, with .NET 4.0 onwards this need has been eliminated. Now, the same thing can be achieved virtually with configuration itself that we will see in a moment.
Now, as far as WAS is concerned, it requires few salient points.
- It needs full-blown IIS not express.
- Also requires a modification in applicationhost.config file, which can be done in cmd prompt.
- Then, need to add the bindings in IIS Manager.
- Last but not the least we need to change config file to host another binding.
Let me go ahead and create the web host project in my Hosting Folder.
This create me the below skeleton project.
After successful creation, here also I have done the same thing what I have done in console application like adding required references and updating the web.config with connection string and installing the Entity Framework there. Now, let me show the config file in its finished form.
Let me explain the config file here. Therefore, inside the service model section there are few things, which we have already discussed. One point to note here, that I have left address unassigned and used http binding. Since, I am going to create the virtual .SVC file which is nothing but my browsing point of Web-Hosting, hence I left address field unassigned. Therefore, the address of this host will be the deployment address itself as shown below in the screen shot.
Now, to the above address, it is going to add the .svc file virtually. Initial days, we used to have a physical .svc file. To add the virtual .svc file, first thing which we need to do here is reference the System.ServiceModel.Activation assembly in the project as shown below in the screen shot.
Then, I have added a section serviceHostingEnvironment outside services tag and inside service model tag. If I would have been using physical .svc file that need to have name to it. However, since I am having virtual .svc file hence I am using relative address here. As I said, already IIS takes care of instantiating ServiceHost for you for each service you are hosting. Now, a class called ServiceHost factory does this. All it does is receive the type of the service it needs to create a host for; instantiate a service host and return the same to IIS. Now, we can inject custom host factory, which inherits from service host factory. Same thing I have declared in the web.config file section. For this I have created, a new class with the name webHostFactory is inherited from ServiceHostFactory. Below is the snippet for the same.
Let me explain the code a bit. Here I have overridden the method CreateServiceHost. It takes two parameter; type of the service and base address. Here, I have done the very basic thing created the instance with these two parameters and just returned the host. With this change in place, when I build and run the app, then it will produce me the below output.
I hope you would have liked this discussion. We will delve further in coming sections. Till then, stay tuned and Happy Coding.
777 total views, 2 views today