Customizing IIS ServiceHost via ServiceHostFactory
When hosting in an IIS server, the WCF service and endpoints configuration are, by default, loaded from the Web.config
file. So, developers do not need to explicitly create and initiate the ServiceHost like we do in a self-hosting scenario. Sometimes, however, we still need to customize the service or endpoints through code. This recipe will use a custom metadata configuration case to demonstrate how to programmatically adjust service behaviors in an IIS-hosting scenario.
How to do it...
Here we will go through the steps of applying a customized ServiceMetadataBehavior
into an IIS-hosted WCF service:
Create the WCF service in the hosting web application.
We can simply create a new WCF service by adding a new WCF Service item in the hosting web application. The resulting WCF service is represented by a
.svc
file and an optional code-behind file (theCalcService.svc
andCalcService.svc.cs
files shown in the following screenshot).Define a custom...