A small setup guide for getting HttpClient working in Xamarin.iOS. Hopefully this guide will be deprecated when Microsoft/Xamarin fix these issues.
At work we were trying to get PortableRest working in iOS, but we kept running into issues with either the build process or something about us not setting Http headers correctly. For example:
System.ArgumentException: This header must
be modified with the appropiate property.
That exception led me to believe we were doing something wrong, but it only occured on iOS devices, not the simulator, which was weird.
Boiled down to these steps, this is how you get a fresh Xamarin.iOS project up and running with HttpClient.
- Create a new Xamarin.iOS project, and add a reference to your Portable Class Library that is using the HttpClient. In my case, we were using our own REST API.
- Using NuGet (or whatever is available at the time of reading) install the Microsoft Http Client libraries:
Install-Package Microsoft.Net.Http
. This will also install it's dependencies, Microsoft BCL, and add/modify theApp.config
in your iOS project. - Almost there! If you try running your project on a device now, you will get the exception mentioned above.
- Open
App.config
, and add an Assembly Redirect for the HttpClient. This ensures the "native" version of HttpClient is being used.
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.5.0" newVersion="2.0.5.0" />
</dependentAssembly>
See the 2.0.5.0
? That's the Mono version of the HttpClient. Replace it with the correct version number (2.0.5.0 at this time).
How did you add the App.config to the iOS project? If I add a App.config to the project it does not fix the problem.