Skip to content

Instantly share code, notes, and snippets.

@jeffijoe
Last active August 29, 2015 14:00
Show Gist options
  • Save jeffijoe/11204918 to your computer and use it in GitHub Desktop.
Save jeffijoe/11204918 to your computer and use it in GitHub Desktop.
Using PCL HttpClient (and PortableRest) in Xamarin iOS.

Using HttpClient in Xamarin.iOS

A small setup guide for getting HttpClient working in Xamarin.iOS. Hopefully this guide will be deprecated when Microsoft/Xamarin fix these issues.

What went wrong

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.

The solution

Boiled down to these steps, this is how you get a fresh Xamarin.iOS project up and running with HttpClient.

  1. 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.
  2. 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 the App.config in your iOS project.
  3. Almost there! If you try running your project on a device now, you will get the exception mentioned above.
  4. 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).

@EifelMono
Copy link

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.

@jeffijoe
Copy link
Author

@EifelMono did you remember to include it? We have experienced that sometimes installing a library generates an App.config, but does not actually include it in the project. Please toggle "View all files" in Visual Studio, or whatever IDE you are using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment