Home > Azure, C#, Mobile Services, WinRT > Adding filters to monitor Mobile Services activities in a Windows Store app

Adding filters to monitor Mobile Services activities in a Windows Store app


The Azure Mobile Services SDK for Windows Store apps provides a mechanism with which we can add filters to the execution pipeline, to process all the requests and responses from the service. In this way, for example, we can use create a filter to log the OData requests that are sent and the JSON objects that are included in the body of Insert/Update calls.

Suppose that we have downloaded the Windows Azure Mobile Services Quickstart app for Windows Store. To add a Service Filter to it, we need to create a class that implemenents the IServiceFilter interface:

public class TraceFilter : IServiceFilter
    public IAsyncOperation<IServiceFilterResponse> Handle(IServiceFilterRequest request, 
        IServiceFilterContinuation continuation)
        Debug.WriteLine("MOBILE SERVICE REQUEST:");
        Debug.WriteLine("Uri: " + request.Uri);
        Debug.WriteLine("Method: " + request.Method);

            "Content: " + request.Content);


        // Sends the actual request to the Mobile Service.
        var response = continuation.Handle(request);
        return response;

Within the Handle method, we have access to an object that represents the request that will be sent to the Mobile Service. In this example, we trace only a subset of the available data, but we have many other information, such as the Headers collection, the Content Type, ecc.

After logging, we call the continuation.Handle method, that actually sends the request. Note that it takes the request object as parameter, so by modifing it we can alter what is sent to the Mobile Service.

To add this filter to the execution pipeline, we must call the WithFilter method on the MobileServiceClient object that is declared in App.xaml.cs:

public static MobileServiceClient MobileService = new MobileServiceClient(
    .WithFilter(new TraceFilter());

The WithFilter method creates a new MobileServiceClient that automatically invokes the Handle method of the specified filter for every Mobile Service request.

Now we can run the app. Let’s save a new TodoItem and mark it as complete. In the Output window, we’ll see something like this:

Uri: https://service_name.azure-mobile.net/tables/TodoItem?$filter=(complete eq false)
Method: GET
Uri: https://service_name.azure-mobile.net/tables/TodoItem
Method: POST
Content: {"text":"New Item","complete":false}
Uri: https://service_name.azure-mobile.net/tables/TodoItem/3
Method: PATCH
Content: {"text":"Azione","complete":true,"id":3}

Note in particular that, at line 2, the Uri includes the OData filter the corresponds to the LINQ query used in the RefreshTodoItems method of MainPage.xaml.cs. The ability so see the actual filters can be very useful for debugging purpose.

Categories: Azure, C#, Mobile Services, WinRT
  1. 07/06/2013 at 15:12

    Wow that was odd. I just wrote an really long comment but
    after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that
    over again. Regardless, just wanted to say fantastic blog!

  1. 16/05/2013 at 09:28
Comments are closed.
%d bloggers like this: