In this walkthrough I’ll show you how to use the AppFabric-enabled WCF Data Service (C#) template for Visual Studio 2010 to enhance your WCF Data Services by providing

  • Monitoring events and errors to the AppFabric Data Store
  • Eliminating the need to use the .svc extension in your URI

Requirements

Step 1 – Create a Web Application

  1. Start Visual Studio 2010
  2. Select File / New Project
  3. Choose a Web Application Template – for this example I used Empty Web Application

Step 2 – Add an ADO.NET Entity Data Model

  1. For my database I’m using the AdventureWorksLT sample database for SQL Server 2008R2
  2. Right click on your project and select Add / New Item…
  3. Select the ADO.NET Entity Data Model template
  4. Name the model AdventureWorks.edmx
  5. Select Generate From Database
  6. Connect to AdventureWorksLT and use the default settings
  7. Select all the tables by checking the Tables checkbox

Step 3 – Add an AppFabric-enabled WCF Data Service

  1. Right click on your project and select Add / New Item…
  2. Select Online Templates
  3. In the search box type AppFabric
  4. Select the AppFabric-enabled WCF Data Service (C#) template
  5. Name it AdventureWorks.svc 

    Add New Item AppFabric-enabled WCF Data Service C#
  6. Click Install to install the template on your machine

Step 4 – Modify the template code

We have added TODO tasks in the template to guide you through tasks you need to do to make your service ready

  1. Replace [[class name]] with AdventureWorksLTEntities
  2. Modify the SetEntitySetAccessRule code as shown for all entities (“*”)
  3. If you want to use a different route you can set that as well
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{            
    // Enable read only access to all entities
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

    RouteTable.Routes.Add(
        new ServiceRoute("AdventureWorks", new DataServiceHostFactory(), typeof(AdventureWorks)));
}

Step 5 – Modify the web.config

  1. In AdventureWorks.svc.cs expand #region Sample Config with End-To-End Monitoring enabled"
  2. Select the commented XML and uncomment it (Ctrl+K, Ctrl+U)
  3. Copy it to the clipboard
  4. Comment it again (Ctrl+K, Ctrl+C)
  5. Open web.config
  6. Paste the XML inside the <configuration> tag
  </connectionStrings>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <diagnostics etwProviderId="830b12d1-bb5b-4887-aa3f-ab508fd4c8ba">
      <endToEndTracing propagateActivity="true" messageFlowTracing="true" />
    </diagnostics>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <etwTracking profileName="EndToEndMonitoring Tracking Profile" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="UrlRoutingModule"/>
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add
      name="UrlRoutingHandler"
      preCondition="integratedMode"
      verb="*" path="UrlRouting.axd"
      type="System.Web.HttpForbiddenHandler, System.Web, 
                      Version=2.0.0.0, Culture=neutral, 
                      PublicKeyToken=b03f5f7f11d50a3a"/>
    </handlers>
  </system.webServer>
</configuration>

Step 6 – Run the Service in 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 I’m going to modify the project to host with the local IIS server.  Note: This requires you to run Visual Studio as Administrator

  1. 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
  2. Right click on the WebApplication and select properties
  3. Go to the Web tab
  4. Check Use Local IIS Web Server and click Create Virtual Directory
  5. Press Ctrl+Shift+S to save your project settings (Debugging will not save them)
  6. Open AdventureWorks.svc.cs
  7. Press F5 to Debug
  8. The Service will open in the debugger and show the service document 
    Service working
  9. Remove the .svc extension from the URI and try it again.  It will work without the .svc extension in the URI. 

    If this isn’t working for you see System.Web.Routing RouteTable not working with IIS?
    If you want to disable the URI with the .svc extension (and eliminate the .svc file) see this post
  10. If you see an error in the event log (Login failed for user 'IIS APPPOOL\DefaultAppPool'. Reason: Failed to open the explicitly specified database. [CLIENT: <local machine>]) You need to grant permission to IIS Users to access the database
USE [AdventureWorksLT]
GO
CREATE USER [IIS USERS] FOR LOGIN [BUILTIN\IIS_IUSRS]
GO
USE [AdventureWorksLT]
GO
EXEC sp_addrolemember N'db_datareader', N'IIS USERS'
GO

Step 7 – View Events in AppFabric

  1. Start IIS Manager
  2. Open the AppFabric Dashboard
  3. Click one of the links for WCF
  4. Right click on an event and select View All Related Events to see detailed events

AppFabric Dashboard

 

Tracked Events