Blog‎ > ‎

Host a WCF Web Service on IIS 7 (Windows 7)

posted Mar 16, 2011, 9:33 PM by Jeremy Walker   [ updated Jul 16, 2011, 3:25 PM ]
There is no perfect way to host a Web Service. Each implementation is unique to its environment and requirements. This guide assumes you are running Windows 7 x64 Professional, have .Net Framework 4 Client Profile AND Extended [Full Installation], with Visual Studio 2010 [Express] and your WCF Web Service is built using .Net Framework 4. It also assumes you already have the appropriate Interfaces and Class implementations compiled in a class library (DLL). This guide only scratches the surface of WCF Web Services, search the Microsoft Developer Network (MSDN) for more articles on the subject.

Install/Configure IIS 7
  1. These Windows Features are required, ensure they are turned ON:
    (You may enable any other features of IIS if you wish)
    • Internet Information Services
      • Web Management Tools
        • IIS 6 Management Compatibility
          • [X] IIS 6 Scripting Tools
        • [X] IIS Management Console
      • World Wide Web Services
        • Application Developement Features
          • [X] .NET Extensibility
          • [X] ASP.NET
          • [X] ISAPI Extensions
          • [X] ISAPI Filters
        • Common Http Features
          • [X] Static Content
        • Security
          • [X] Windows Authentication
      • Microsoft .NET Framework 3.5.1
        • [X] Windows Communication Foundation Http Activation
  2. Enable .NET Framework 4 as the default framework for IIS, run these commands:
    • "%WINDIR%\Microsoft.Net\Framework\v4.0.30319\aspnet_regiis" –i –enable
    • "%WINDIR%\Microsoft.Net\Framework\v4.0.30319\ServiceModelReg.exe" -r
Install the Web Service
  1. Create a folder where you want to store the Web Service.
    • Make sure the appropriate privileges are assigned. A default IIS setup uses the 'IUSR' account for file access, so give the 'IIS_IUSRS' group modify privileges to the folder and its sub-folders. (Note that the IUSR doesn't show up in any lists of users, but its definitely there.)
  2. Add an Application to the appropriate Website in IIS 7 and point it to the new folder. Set the 'alias' to anything you want, this will be the postfix of the URL.
  3. Add a folder named 'bin' inside the new folder.
  4. Copy the class library (DLL) to the new 'bin' folder.
  5. Create a '.svc' file. Name it whatever you want, it will be the file you request from a web browser or client application.
    • Add '<%@ServiceHost language=c# Debug="true" Service="[Full class name]"%>' to the top of the '.svc' file. [Full class name] is the fully qualified name of the class in the compiled DLL file, i.e. 'Microsoft.Samples.MyWCFClass'.
.Net Framework 4.0 instructs IIS to create a default endpoint for HTTP. However it does not have a default endpoint for publishing metadata.

Publish Metadata (WSDL Contract)
  1. Define the <endpoint> in the web.config file.
        <services>
          <service name="[Full class name]" behaviorConfiguration="[Behavior name]">
            <endpoint address="" binding="wsHttpBinding" contract="[Full class path to contract Interface]" />
          </service>
        </services>
  2. Define a <behavior>, add a <serviceMetadata> tag with 'http[s]GetEnabled' set to true, and bind it to the <endpoint>.
    ...
        <behaviors>
          <serviceBehaviors>
            <behavior name="[Give this a name]">
              <serviceMetadata http[s]GetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    ...
    <service name="[Full class name]" behaviorConfiguration="[Behavior name]">
    ...
    A lot of this XML can be configured differently. Hopefully my examples provide at least an idea.
This will publish metadata about the service in WSDL format at 'http[s]://[URI to .svc file]?wsdl'. Tools like svcutil.exe can read this metadata to produce Interfaces (also known as a contract) for implementing into a server or client.

Test it!
    To test, open a web browser of your choice and go to the URL of the new '.svc' file. For example, if I added the Application to the Default Website, named it 'MyTestWebService', and named the '.svc' file 'service.svc' then I would enter 'http://localhost/MyTestWebService/service.svc'. It should display a page without error messages.