Forum Xamarin Cross Platform with Xamarin

Cross platform assembly nightmare. Can somebody please help?

KrumelurKrumelur DEUniversity ✭✭
edited December 2013 in Cross Platform with Xamarin

I have a PCL core project here with a class that wants an HttpMessageHandler instance for initialization.
On iOS, I pass a "new CFNetworkHandler()". The iOS project references System.Net.Http from Mono.
The core project references Microsoft.Net.Http 2.2.18 from NUget.
All builds. However if I run the iOS project I get a runtime exception:

System.TypeLoadException: Could not load type 'System.Net.Http.CFNetworkHandler' from assembly 'Client_iOS'.

During build I see this warning:

Warning 3   Found conflicts between different versions of the same dependent assembly. Please set the "AutoGenerateBindingRedirects" property to true in the project file. For more information, see Client_iOS

If I remove the Microsoft.Net.Http package from the core lib and try to add it back, I get an error that the selected target frameworks iOS and Android are not supported. I can change my project settings and remove these targets, add the package and set the targets again but them I'll get the same error as above again.

How can I get things right?


  • KrumelurKrumelur DEUniversity ✭✭

    Meanwhile I played around a bit and got rid of the Microsoft.Net.Http assembly by removing WP8 from my solution and by changing the PCL targets.
    Still, the issue remains. Now the problem is that the PCL is using System.Net.Http from Microsoft and iOS is using the same one but from Mono.
    This means it builds and I can pass new CFNetworkHandler() but at runtime I get the same exception as before. Sigh...

  • SKallSKall USMember ✭✭✭✭

    Welcome to the world of PCL! These are the types of issues why I avoid PCL whenever possible. Going with the other code sharing options is just simpler and faster.

  • KrumelurKrumelur DEUniversity ✭✭

    :-( Not really what I wanted to least without WP8 I would like to get it solved with PCLs.

  • KrumelurKrumelur DEUniversity ✭✭

    I have now tried to use AFNetworkHandler instead of CFNetworkHandler from this component - and this seems to work. I think the reason is that it is coming from a different assembly/namespace. I wish Xamarin would open source the CFNetworkHandler source.

  • SKallSKall USMember ✭✭✭✭

    Are you trying to implement a rest client? If so you might want to take a look at what I have done with PCL:

    There is then implementations for each platform (same code, different assemblies). The approach is to use automatic serialization/deserialization but you can also get/send raw string. I do believe Android has a minor problem similar to yours where it is complaining about missing System.Runtime (another nice PCL bug I assume with different assembly versions).

  • softlionsoftlion FRBeta ✭✭✭

    Try my answer there. It works perfectly in a PCL project without any platform addin or third party lib or other CFNetworkHandler things.

    But it depends on the Xamarin version. I was succesfull with 4.10.01073 / 1.8.365 on VS2012, with the latest beta on Mac (for ios build).

  • OrenNovotnyOrenNovotny USMember, Insider, Beta, University ✭✭

    We've run into a bunch of issues with the System.Net.Http PCL. Seems like it's not getting unified correctly with the Xamarin version in the Fx.

    The answer has always been to make sure the following binding redirect is present in the app.config:

            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="" newVersion="" />

    Keep in mind that Nuget likes to add its own redirects and will sometimes overwrite stay vigilant.

  • KrumelurKrumelur DEUniversity ✭✭

    @OrenNovotny Do you have an Android version too? Or only iOS?

  • OrenNovotnyOrenNovotny USMember, Insider, Beta, University ✭✭
    edited January 2014

    That binding redirect should work for both iOS and Android. The Mono* version of System.Net.Http is For whatever reason, the unification logic isn't working for that. It should be handling the redirects internally as part of the build.

  • softlionsoftlion FRBeta ✭✭✭

    It should be handling the redirects internally as part of the build.

    It does. I systematically remove all binding redirects now. Especially the one added by the nuget packages. And it works well, except VS complaining i should add binding redirects ...

Sign In or Register to comment.