Building Blocks for a WCF Service Web Site - Hosting a Service in IIS
(Page 4 of 4 )
How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services over HTTP protocol, the Windows Activation Service (WAS) can
support others such as TCP and named pipes, and self-hosting can support many protocols and includes several deployment options such as console or Windows Forms applications and Windows services. Selecting a hosting environment has nothing to do with service implementation, but everything to do with service deployment and overall system design.
This lab will show you how to host an existing service type as part of a web site hosted in IIS. In the process, I'll also be illustrating other extended concepts such as:
- The WCF Service web site template
- Message-based activation
- Additional metadata behavior settings
- Exporting service descriptions
- Consuming service description documents to generate client code
As always, after the lab I'll describe some of these features in greater detail.
Lab: Creating an IIS Host and Browsing Metadata
For this lab, you will work with an existing solution that contains a completed service library and shell client application. Using Visual Studio templates, you'll create a new IIS web site project that contains a service and modify it to host a preexisting service. To consume the service, you'll generate a client proxy from static service documentation exported using SvcUtil.
Creating a WCF Service web site
The first thing to do is create a WCF-enabled web site using the WCF Service template, which is new to WCF. When services are added to a web site, the supplied sample service is accompanied by a .svc file, the web server endpoint.
- Open the startup solution for the lab, located at <YourLearningWCFPath>\Labs\ Chapter1\IISHostedService\IISHostedService.sln. This solution contains a copy of theHelloIndigoproject from earlier labs and a shell client application.
- You are going to create a new web site to host the service. Go to Solution Explorer, right-click the solution node and select Add -> New Web Site. Select the WCF Service template and make sure the location type for the new web site is HTTP (see Figure 1-25). Set the location value to http://localhost/IISHostedService.
When Visual Studio creates a new HTTP web site, a virtual application is created in IIS pointing to a directory beneath c:\inetpub\wwwroot (or wherever your Default Web Site is pointing). In Figure 1-25, the path to theIISHostedServiceproject might be c:\ inetpub\wwwroot\IISHostedService.
- The WCF Service template generates a new web site with a default service implementation. You can delete the service implementation since you will be hosting an existing service. Go to Solution Explorer and expand the App_Code folder for the web site (see Figure 1-26). There you'll see the file Service.cs. Delete it from the project.
- Go to the web site project and add a reference to theHelloIndigoproject, which contains the service you're about to host.
You now can modify the web endpoint for the service so that it is associated with the correct service type. Open Service.svc in the code window and modify the@ServiceHostdirective to associate the web endpoint with the service typeHelloIndigo.HelloIndigoService, as shown here:
<%@ ServiceHost Service="HelloIndigo.HelloIndigoService" %>
Now, when a request arrives to Service.svc, the service model will activate a newServiceHostinstance associated with theHelloIndigoServicetype.
The WCF Service template also generated configuration settings for the host, but these settings are based on the service supplied by the template. You must modify these settings to reflect the correct service contract and service type.
Open the web.config file and find the<service>section. Change the name attribute of the<service>section toHelloIndigo.HelloIndigoServiceand change the contract attribute of the<endpoint>section toHelloIndigo.IHelloIndigoService. While you're at it, change the binding tobasicHttpBindinginstead ofwsHttpBinding. The result is shown here:
<service type="HelloIndigo.HelloIndigoService"
behaviorConfiguration="returnFaults">
<endpoint contract="HelloIndigo.IHelloIndigoService"
binding="basicHttpBinding"/>
</service>
You now have a web site that will expose an endpoint to reachHelloIndigoService. Before generating the client proxy, I'll show you some useful metadata features.
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 1 of the book Learning WCF A Hands-on Guide, written by Michele Leroux Bustamante (O'Reilly, 2007; ISBN: 0596101627). Check it out today at your favorite bookstore. Buy this book now.
|
|