Monitoring WCF Data Services with Windows Server AppFabric

WCF Data Services provides a very simple model driven way to expose data using REST and the OData format.  As I was preparing my talk for Tech-Ed on Implementing REST in .NET I was asking myself if there would be any value in using Windows Server AppFabric with WCF Data Services.  After all, AppFabric has monitoring which can capture tracking information across the server farm storing it in a monitoring database.  Typically we show Workflow Services using this capability because they provide such a rich stream of tracking events. However, it is quite possible to use AppFabric to monitor data services.  In this post I’ll take you through step by step how you can do it.

Note: To make this post more readable – click on the images to see them full size. The team also has a sample on this here

Step 1 – Host your site in IIS

To benefit from Windows Server AppFabric your web application must be hosted in IIS.  To use Visual Studio with IIS you must run it as administrator.  Then go into the project properties of your web application and set it to run in the local IIS server.

image

Step 2 – Browse to your service and see the monitoring in action

In my RESTWeb demo application I’ve added some links to the Default.htm page that will exercise the service.  If you run the app and hit a few pages you will see that AppFabric captures information about your service being invoked. 

To see it

  1. Open IIS Manager
  2. Select the RESTWeb website by expanding the Default Web site
  3. Double click on AppFabric Dashboard
  4. Check the WCF Call History section

image

Here you can see that there were 4 calls to the WCF Data Service named Conference.svc.  If you click on the hyperlink for Conference.svc you will get the detail on those 4 calls.

image

This is good, but because we have a WCF Data Service all the requests look the same in the tracking data.  We can see that somebody invoked the service but we don’t know what things they were querying in the model or if they were doing a GET, POST, PUT etc. 

Step 3 – Instrument your service to emit custom tracking records

In my RESTWeb web application, I’ve added some sample code I got from the AppFabric guys (with some minor modifications from me) that shows how you can emit a custom tracking record that will be displayed in AppFabric.  By overriding the DataService.OnStartProcessingRequest method, I can now emit a tracking record with details of the request.

     public class Conference : DataService<ConferenceDataEntities>
    {
        // Sample class allows you to emit custom tracking records
        static WCFUserEventProvider eventProvider = new WCFUserEventProvider();

        protected override void OnStartProcessingRequest(ProcessRequestArgs args)
        {
            string text = string.Format("Processing HTTP {0} request for URI {1}", 
                args.OperationContext.RequestMethod, args.RequestUri);

            // Push the event through ETW to AppFabric
            eventProvider.WriteInformationEvent("Conference Request", text);
        }

Step 4 - Enable End-To-End Tracking

To make this work you have to turn on end-to-end tracking in AppFabric.  To do this

  1. Right click on your web app and select Manage WF and WCF Services
    image
  2. Select the Monitoring Tab
  3. Set the monitoring level to End-To-End
    SNAGHTML42dfda5

 

Step 5 – Verify Behavior

  1. Run a few more queries through your data service. In my RESTweb example just click through a few links on the home page.
    image
  2. Open (or refresh) AppFabric Dashboard as you did before.
  3. Click the link for calls to Conference.svc
  4. Right click on the top event and select View Related Events
    image
  5. Now you will see a number of related events (related because of end-to-end tracking) – the tracking record payload will contain the message we created in the code.
    SNAGHTML43491b9[4]

Summary

The attached sample code is what I showed at my Tech-Ed Session.  Hope you find it helpful.

RESTWeb.zip