Xamarin: System.Net vs native (NSURLSession) performance in ServiceStack

I notice a number of Xamarin threads recommend using ModernHttpClient (https://github.com/paulcbetts/ModernHttpClient) which replaces System.Net.Http.HttpClient with an implementation based on native Android and iOS http sessions (50% performance examples have been mentioned). From a brief glance it looks like ServiceStack clients work on a lower level than HttpClient, but do not use the native http session tools on mobile (is this correct?) If so, I’m wondering if any one has experience comparing performance/reliability of ServiceStack mobile clients vs. using a REST configured to use ModernHttpClient as a replacement for HttpClient?

ServiceStack uses .NET’s built-in HttpWebRequest which supports PCL and has a great implementation on Mono.

Hmm–isn’t HttpWebRequest a blocking call on iOS so that multiple requests result in threads piling up? That’s my understanding from Paul Betts rationale for developing the ModernHttpClient (he also cites a 3G problem on iOS):

http://herdingcode.com/herding-code-186-paul-betts-on-three-cross-platform-libraries-splat-modernhttpclient-and-punchclock/

Would you see these as potential performance issues?

I think HttpClient is also PCL friendly now:
http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspxnt

ModernHttpClient can be called from PCL.

It might be neat if Servicestack was HttpClient based so that we could swap in ModernHttpClient on mobile and get the native optimized http stacks if we wanted to?

It might use a threadpool behind the scenes but async HttpWebRequest calls don’t block the Main/UI thread. If this resulted in a real-world issue I would expect Xamarin would just optimize their impl so all their .NET developers/customers benefit.

All benchmarks I’ve seen show that HttpClient is slower than HttpWebRequest nor does it offer the same finer-grained API’s. Regardless, effort isn’t going to be invested to refactor the working Service Clients to use it, potentially introducing breaking changes and removing used features in deployed systems.

Anyone who really wants to use ModernHttpClient now should just be able to use it instead, given ServiceStack Services are just standard HTTP Services. If there’s evidence of real-world value and enough demand for it new clients could be created, in which case you can submit a new feature request for it.

Fair enough. Thanks for your quick feedback!

Hi we’ve just released a new ServiceClient based on Microsoft’s new HttpClient that’s now available from MyGet in the new ServiceStack.HttpClient NuGet package:

PM> Install-Package ServiceStack.HttpClient

It implements the same IServiceClient API’s as the existing JsonServiceClient so it should largely be a drop-in replacement with your existing Service Client code. It’s currently mostly feature complete with most API’s already implemented (and we’ll continue working on it until it gains feature parity with our existing .NET Service Clients).

The API is similar to the existing client where instead of JsonServiceClient you’ll just use JsonHttpClient, e.g:

var client = new JsonHttpClient("http://techstacks.io");

Since it’s based on HttpClient you’re encouraged to use the non-blocking Async API’s, e.g:

var response = await client.GetAsync(new GetTechnology { Slug = "servicestack" });
response.PrintDump();

Sync API’s are also implemented:

var response = client.Get(new GetTechnology { Slug = "servicestack" });

Where it’s essentially just blocking on the Async Task under the hood (i.e. var response = task.Result).

To get it to use ModernHttpClient you need to install the ModernHttpClient NuGet package and then you can set it to use its NativeMessageHandler for all JsonHttpClient instances with:

JsonHttpClient.GlobalHttpMessageHandlerFactory = () => new NativeMessageHandler();

Please report any issues you find with this new client at: https://github.com/ServiceStack/Issues

Wow! That was fast! I will get this implemented in my client. :gift_heart: ServiceStack