Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change recommended pattern #257

Open
dotMorten opened this issue Dec 5, 2017 · 0 comments
Open

Change recommended pattern #257

dotMorten opened this issue Dec 5, 2017 · 0 comments

Comments

@dotMorten
Copy link

dotMorten commented Dec 5, 2017

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant