Hosting for the Web
Arguably one of the best improvements in WCF is the feature for hosting services on the Web. Prior to .NET Framework 3.5, you had to provide configuration or write code to host services. This was true even if you hosted your services within IIS. This became tedious for those hosting services on the Web. There are many capabilities offered by WCF to host services, but only a limited configuration was used by Web developers building services. For example, you would not expect an AJAX-based application to support multiple bindings, use message-level security, or require transactions. To simplify hosting of services, WCF introduced a feature called Configuration Free Hosting. This allows developers to host services without providing configuration or writing any code. The infrastructure for this feature was always a part of the hosting model within WCF. We will examine two ways to use this feature.
There is a new class available in the System.ServiceModel.Web namespace called the WebScriptServiceHost class. This class allows for self-hosting of services using the WebHttpBinding binding and the WebScriptEnablingBehavior endpoint behavior. The advantage to using this class over the ServiceHost class is that you do not have to provide any binding or behaviors to host a service.
Another class in the System.ServiceModel.Activation namespace, called the WebScriptServiceHostFactory class, is meant to be used with IIS hosting and .svc files. It allows for hosting of services in IIS using the WebHttpBinding binding and the WebScriptEnablingBehavior endpoint behavior without the need for configuration. Listing 13.15 shows an example of an .svc file that uses the WebScriptServiceHostFactory class. This is the same .svc file used to host the WallpaperService service shown in Listing 13.12. The advantage of this approach is that the configuration information shown in Listing 13.13 is no longer required to host the service.
Listing 13.15. WallpaperService.svc (Configuration Free)
<%@ ServiceHost Factory= "System.ServiceModel.Activation.WebScriptServiceHostFactory" Language="C#" Debug="true" Service="EssentialWCF.WallpaperService" CodeBehind="~/App_Code/WallpaperService.cs" %>