7

I am doing a project for multiple platforms where I have followed best practices and created a core project to contain all the code that is the same on all platforms. To do the network communication I would like to use HttpClient which seems to be the recommended approach for communicating with web services going forward.

I am NOT doing a PCL but just a standard C# library to contain the core project. However it seems that there is no common implementation of HttpClient but only platform specific ones. I cannot reference and use it in my core but only in my Android and iOS projects.

I would very much prefer to have all networking code in my core project and as I see it there are two options - neither of which I like.

  1. Make a common interface that I can use in the core project that is implemented as a wrapper on both Android and iOS.
  2. Use another technology that is supported in the core project - I am thinking webclient or the like.

What is the recommended approach and why hasn't Xamarin made HttpClient available as a common library?

Hope someone can help, Thanks

3 Answers 3

7

I recommend checking out ModernHttpClient https://github.com/paulcbetts/modernhttpclient

It is a cross-platform networking library for iOS and Android. From the project:

This library brings the latest platform-specific networking libraries to Xamarin applications via a custom HttpClient handler. Write your app using System.Net.Http, but drop this library in and it will go drastically faster.

Sign up to request clarification or add additional context in comments.

Comments

2

You should be able to use System.Net.Http in your core library just fine, I've done it recently.

Make sure your core library is a .Net 4.5 project. There is a setting in project options for selecting the .Net version. After checking this, all you should have to do is reference System.Net.Http.dll.

If that doesn't solve your issue, are you on Windows or Mac(Xamarin Studio)?

5 Comments

What's the difference between Windows and Man on that?
Also - can I use System.Net.Http in PCL project? I am not able to import them, why?
When I typed this PCLs weren't at a stable point like they are now. Just make sure your PCL is targetting .Net 4.5, Windows Phone 8, WinRT, Xamarin.iOS, and Xamarin.Android. If you target anything lower, System.Net.Http support will get dropped off.
Ok, thanks. Just make me sure - should I be adding "Microsoft HTTP Client Libraries" via NuGet? Because I am targeting 4.5 and I can't see HttpClient without adding this reference. In addition, could you take a look at my question : stackoverflow.com/questions/21711694/… Thanks!
You only need NuGet if you are targeting lower PCL (like Win Phone 7 or .Net 4/Silverlight). I have always had trouble using them with Xamarin, so I just target the new stuff since it doesn't matter for the kind of work I do. We aren't writing distributable frameworks or anything.
1

I'm afraid you can't use HttpClient on Mono at the moment due to licensing restrictions. There is a UserVoice suggestion that you can vote on if you like.

1 Comment

Update: this is no longer true. The license has changed, as can be seen in Darrel's UserVoice link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.