Skip to content

Change recommended pattern #257

@dotMorten

Description

@dotMorten

In the readme file, it's stated you should use the library this way:

var httpClient = new HttpClient(new NativeMessageHandler());

However on Android this is bad practice. Instead the practice should be to "just" create an HttpClient with the empty constructor. An app can then choose the handler they want to use app-wide for the entire app and all 3rd party libraries using the environment settings, with this single line call at start-up:

 System.Environment.SetEnvironmentVariable("XA_HTTP_CLIENT_HANDLER_TYPE", "ModernHttpClient.NativeMessageHandler,ModernHttpClient");

The problem with explicitly declaring a message handler, is that it completely overrides the http handler selection and forces whichever one you decided at that spot. So libraries shouldn't be doing this, as it prevents app developers from using whichever handler they want app-wide.

You can see how all this works here:
https://github.com/xamarin/xamarin-android/blob/0c26b71339a1027902f72e3842077feb7d9c7d9b/src/Mono.Android/Android.Runtime/AndroidEnvironment.cs#L319-L328

It is also documented here:
https://developer.xamarin.com/guides/android/advanced_topics/environment/
https://developer.xamarin.com/guides/android/application_fundamentals/http-stack/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions