How to correct Null Reference Exception when using HttpClient with Xamarin and iOS

Problem:

When I use HttpClient in my Xamarin project it works with Android but throws a Null Reference exception on iOS.

Solution:

Add an app.config with the the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.0.5.0" />
         </dependentAssembly>

      </assemblyBinding>
   </runtime>
</configuration>

Explanation:

The namespace for HttpClient exists on Android and iOS which conflicts with the one in System.Net.HttpClient. The redirect makes sure you find the correct version.

Add comment

Loading