Share via


AppFabric WCF HTTP Service Template

Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. Windows Server AppFabric provides tools for managing and monitoring your web services and workflows. WCF HTTP Service is a service that can be accessed by a simple HTTP POST/GET request and can return responses in plain XML and in JSON formats.

The AppFabric WCF HTTP Service template brings these two products together providing the following features:

  • Monitor the calls to your service across multiple servers with AppFabric Monitoring
  • Eliminate the .svc extension from the URI by using routing
  • Create custom Event Tracing for Windows (ETW) events that will be logged by AppFabric Monitoring
  • Provide a simple HTML page for invoking the service with or without the .svc extension

Setup

To build and test an AppFabric WCF HTTP Service you will need the following:

  1. Visual Studio 2010 / .NET Framework 4
  2. IIS 7
  3. Windows Server AppFabric
  4. HTTP Redirection (more info)

Walkthrough

  • Add a new project using the ASP.NET Empty Web Application Template.
  • Add a new item to your project using the AppFabric WCF Service template named SampleService.svc
  • Open the SampleService.svc.cs file and replace the SayHello method with the following code:
    1: public string SayHello(string name)
    2: {
    3:     // Output a warning if name is empty
    4:     if (string.IsNullOrWhiteSpace(name))
    5:         AppFabricEventProvider.WriteWarningEvent(
    6:             "SayHello",
    7:             "Warning - name is empty");
    8:     else
    9:         AppFabricEventProvider.WriteInformationEvent(
   10:             "SayHello",
   11:             "Saying Hello to user {0}",
   12:             name);
   13:  
   14:     return "Hello " + name;
   15: }
  • Build the solution

Enable Monitoring

  • Open web.config
  • Enable the EndToEndMonitoring Tracking profile for your web application
    1: <microsoft.applicationServer>
    2:   <monitoring>
    3:     <default enabled="true" connectionStringName="ApplicationServerMonitoringConnectionString" monitoringLevel="EndToEndMonitoring" />
    4:   </monitoring>
    5: </microsoft.applicationServer>
    6:  
    7: <system.serviceModel>
    8:   <diagnostics etwProviderId="830b12d1-bb5b-4887-aa3f-ab508fd4c8ba">
    9:     <endToEndTracing propagateActivity="true" messageFlowTracing="true" />
   10:   </diagnostics>
   11:   <behaviors>
   12:     <serviceBehaviors>
   13:       <behavior>
   14:         <etwTracking profileName="EndToEndMonitoring Tracking Profile" />
   15:         <serviceMetadata httpGetEnabled="true" />
   16:       </behavior>
   17:     </serviceBehaviors>
   18:   </behaviors>
   19: </system.serviceModel>

Enable Routing

    1: <system.serviceModel>
    2:   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    3: </system.serviceModel>
  • Enable the UrlRouting module.
    1: <system.webServer>
    2:   <modules runAllManagedModulesForAllRequests="true">
    3:     <remove name="UrlRoutingModule"/>
    4:     <add name="UrlRoutingModule" 
    5:          type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    6:   </modules>
    7:   <handlers>
    8:     <add name="UrlRoutingHandler"
    9:          preCondition="integratedMode"
   10:          verb="*" 
   11:          path="UrlRouting.axd"
   12:          type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
   13:   </handlers>
   14: </system.webServer>

exclamation

You Must Enable HTTP Redirection

The UrlRoutingHandler will not work if the HTTP Redirection feature of IIS is not installed (more info)

  • Add global.asax
  • Add the following namespace directives
    1: using System.Web.Routing;
    2: using System.ServiceModel.Activation;
  • Add code to the Application_Start method to register the route.
    1: protected void Application_Start(object sender, EventArgs e)
    2: {
    3:     RouteTable.Routes.Add(new ServiceRoute(
    4:         "SampleService", /* Replace SampleService with your service name */
    5:         new WebServiceHostFactory(), 
    6:         typeof(SampleService))); /* Replace SampleService with your service name */
    7: }

Verify with Development Server

  • In the Solution Explorer window, right click on the generated HTM file for your service. For example SampleService.svc.htm and select View in Browser.
  • The ASP.NET Development Server will start and your page will load in the browser.
  • The test page contains a text box and two links. One for your service with the .svc extension and one without the .svc extension which requires routing.
  • Enter your name in the text box, and click on the links to verify that your service works with and without routing.

exclamation

If you are using Internet Explorer 9, you might see the following error message: “The XML page cannot be displayed”

To view the XML response of the service, open the View menu and select Source.

Verify with IIS

To see the events in Windows Server AppFabric you need to deploy the Web project to IIS or modify your project to host the solution in the local IIS Server.  For this example you will modify the project to host with the local IIS server. 

exclamation

Run Visual Studio as Administrator

If you are not running Visual Studio as Administrator, exit and restart Visual Studio as Administrator and reload your project.  For more information see Using Visual Studio with IIS 7.

  • Right click on the Web Application project and select properties
  • Go to the Web tab
  • Check Use Local IIS Web Server and click Create Virtual Directory
  • Save your project settings (Debugging will not save them)
  • In the Solution Explorer window, right click on the generated HTM file for your service. For example SampleService.svc.htm and select View in Browser. The address should now be that of the IIS (“https://localhost/applicationName/”) and not of the ASP.NET Development Server (“https://localhost:port/”).
  • The test page contains a text box and two links. One for your service with the .svc extension and one without the .svc extension which requires routing.
  • Enter your name in the text box, and click on the links to verify that your service works with and without routing.

Verify AppFabric Monitoring

  • Open IIS Manager (from command line: %systemroot%\system32\inetsrv\InetMgr.exe)
  • Navigate to your web application (In the Connections pane open ComputerName à Sites à Default Web Site à ApplicationName)
  • Double Click on the AppFabric Dashboard to open it
  • Look at the WCF Call History you should see some successful calls to your service. It groups calls by URI so calls without the .SVC extension look like a different service.

WCFCallHistory

  • Switch back to your browser, leave the text box empty (clear it if necessary) and click the link for service with the .svc extension and the link for the service without the .svc extension. This will generate a warning event.
  • To see the monitoring for these activities switch back to the IIS Manager and refresh the AppFabric Dashboard.
  • Click on the link in the WCF Call History for SampleService.svc and you will see events for the completed calls.
  • To see specific events, right click on an entry for the SayHello operation and select View All Related Events.
  • In the list of related events you will see the user defined event named SayHello. The payload of this event contains the message logged by the operation.

AFMonitoring