添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
曾经爱过的八宝粥  ·  PHP: ...·  1 月前    · 
星星上的苦咖啡  ·  [292.26]WARNING: The ...·  3 月前    · 
谦逊的针织衫  ·  Python ...·  5 月前    · 

Xamarin and the HttpClient For iOS, Android and Windows

In an earlier post that talked about using dependency injection and registering interfaces for working with Refit across both Prism and MvvmCross I had code that registered an instance of the CustomHttpMessageHandler class which internally used a HttpClientHandler for its InnerHandler. For developers who have spent a bit of time optimising their iOS, Android or Windows application, you’ll have noted that using the HttpClientHandler is generally not deemed to be best practice.  As I’m a big fan of trying to demonstrate best practices, I figured I’d expand on this a little into a post talking about the HttpClient and some of the options you have.

Firstly, a couple of bits of side reading:

  • – Docs on the HttpClient stack
  • – Mono blog post talking about the HttpWebRequest / HttpClient
  • – Jon’s post on Http Performance
  • What you should gather from these articles is that Microsoft is doing their best to set you up for success but not wanting to take any documentation for granted, let’s see what happens when we create a brand new Xamarin.Forms project and spin up an instance of the HttpClient. When creating the project I just picked the Blank Xamarin.Forms template and made sure that all three platforms were included. The code for creating the HttpClient just uses the zero-parameter constructor:

    var client = new HttpClient();

    Let’s run each platform and see what the HttpClient gives us (and at this point I haven’t updated any NuGet packages, framework versions or anything. This is just what VS2019 gives me when I create a new XF project).

    Here we get the managed HttpClientHandler, rather than the newer (and arguably better) WinHttpHandler. Actually I didn’t find a definitive guide on which is better for UWP, although this stackoverflow post does seem to imply the WinHttpHandler would be the preferred choice, particularly if you want to leverage Http/2.

    Android

    Android is using the AndroidClientHandler which is what should give us the most up to date http experience.

    iOS is using the NSUrlSessionHandler which is what should give us the most up to date http experience.

    This all seems good (albeit that you might want to use the WinHttpHandler on UWP) so for a lot of developers they might never run into any issues. If you did want to adjust which handler is used on iOS and Android (again assuming you’re just using the HttpClient with the default constructor) you can do so via the properties dialog for the corresponding platform:

    However, where things come unstuck is if you want to customise some of the http behaviour. In my previous post I demonstrated setting the compression flag but it could have equally been adding an additional header or changing the credentials that are sent as part of each request. In this case, it’s easy enough to use the overload of the HttpClient constructor that takes a HttpMessageHandler and use the managed HttpClientHandler implementation (as I demonstrated). As you’d have seen from the linked articles above, this isn’t ideal as the managed implementation doesn’t leverage the platform specific optimisations.

    The better approach is for my application to register the platform specific handler, which in MvvmCross can be done via the Setup class (which is created by default when using MvxScaffolding):

    public class Setup : MvxFormsWindowsSetup<Core.App, UI.App>
         protected override void InitializeIoC()
             base.InitializeIoC();
            Mvx.IoCProvider.LazyConstructAndRegisterSingleton<HttpMessageHandler, IServiceOptions>(options =>
    return new WinHttpHandler()
                    AutomaticDecompression = options.Compression
    

    Android

    public class Setup : MvxFormsAndroidSetup<Core.App, UI.App>
         protected override void InitializeIoC()
             base.InitializeIoC();
            Mvx.IoCProvider.LazyConstructAndRegisterSingleton<HttpMessageHandler, IServiceOptions>(options =>
                 return new AndroidClientHandler
                     AutomaticDecompression = options.Compression
    
    public class Setup : MvxFormsIosSetup<Core.App, UI.App>
         protected override void InitializeIoC()
             base.InitializeIoC();
            Mvx.IoCProvider.LazyConstructAndRegisterSingleton<HttpMessageHandler, IServiceOptions>(options =>
                 var nsoptions = NSUrlSessionConfiguration.DefaultSessionConfiguration;
                 if (options.Compression == System.Net.DecompressionMethods.None)
                     nsoptions.HttpAdditionalHeaders = new NSDictionary("Accept-Encoding", "identity", new object[] { });
                 var handler = new NSUrlSessionHandler(nsoptions);
                 return handler;
    

    Note: for iOS the NSUrlSessionHandler enabled compression by default, so the code here illustrates how you could disable compression if you wanted by sending the identity Accept-Encoding header.

    In this post I’ve shown you how you can register each of the native platform handlers to optimise the requests made when using the HttpClient. This post should be read in conjunction with my earlier post that registered the other classes necessary to create the HttpClient based on the registered handler. The only other change is that the HttpService constructor should accept an HttpMessageHandler instead of an ICustomHttpMessageHandler.

    public class HttpService : IHttpService
         public HttpService(HttpMessageHandler httpMessageHandler, IServiceOptions options)
             HttpClient = new HttpClient(httpMessageHandler as HttpMessageHandler)
                 BaseAddress = new Uri(options.BaseUrl)
        public HttpClient HttpClient { get; }
    

    Update: It’s worth noting that the WinHttpHandler used in the UWP example isn’t part of the core framework. Instead it is accessible via the System.Net.Http.WinHttpHandler NuGet package. Visual Studio provides a handy way to find and install this package – selecting the WinHttpHandler reference (where there is a build error) and looking at the intellisense options, there is an option to Install the System.Net.Http.WinHttpHandler package.

    Categories Development Tags , , , , , , ,