Home > Azure, C#, WinRT > Http Message Handlers with the new version of Azure Mobile Services client for Windows Store apps

Http Message Handlers with the new version of Azure Mobile Services client for Windows Store apps

16/05/2013

Some times ago in this blog we talked about how to use Service filters with Azure Mobile Services client for Windows Store apps. With the new version of the client (0.3.3 RC at the time of writing), the Service Filters mechanism has been replaced by Http Message Handlers, that are part of HttpClient. So, now we can use a standard class instead of a specific one.

Converting a service filter to a message handler is straightforward. The Trace filter that has been shown in these pages becomes the following:

public class TraceHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        Debug.WriteLine("MOBILE SERVICE REQUEST:");
        Debug.WriteLine("Uri: " + request.RequestUri);
        Debug.WriteLine("Method: " + request.Method);

        if (request.Content != null)
        {
            var content = await request.Content.ReadAsStringAsync();
            Debug.WriteLineIf(!string.IsNullOrWhiteSpace(content),
                "Content: " + content);
        }

        Debug.WriteLine("======");

        // Sends the actual request to the Mobile Service.
        var response = await base.SendAsync(request, cancellationToken);
        return response;
    }
}

In order to add this handler to the execution pipeline, we need to use a new overload of the MobileServiceClient constructor:

public static MobileServiceClient MobileService = new MobileServiceClient(
    "mobile_service_url",
    "mobile_service_application_key", 
    new TraceHandler()
);

The third parameter of the constructor accepts a param array of HttpMessageHandler, so we can easily add as many handlers as we want.

Categories: Azure, C#, WinRT
  1. 19/06/2013 at 17:14

    Superb stuff here on wordpress.com, man. I actually like what you
    have accrued right here, definitely like what you’re saying and the way through which you profess it. You’ve taken the effort to
    make it simultaneously entertaining and intelligent.
    I cannot wait to know much more from you. A helpful blog without a doubt.

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: