HttpClient in iOS, Android, and WP8 with a Portable Class Library

wallymwallym USInsider, Beta ✭✭✭

I've been reading, and trying, and pulling my hair out on this. I'm trying to get the HttpClient to run in a PCL library that supports iOS, Android, and WP8. I can get the HttpClient when I have iOS & Android by themselves in a PCL project. When I add WP8, the HttpClient becomes a problem. I got to nuget and add the Microsoft.Net.Http package and I get an install error due to some of the other packages that need to be installed. My next attempt is to remove the Xamarin projects as possible clients. The Microsoft.Net.Http package goes ahead and installs. I then re-add the Xamarin project types. I get errors in the build process regarding ambiguous references. I take out the references, and then get messages that I need to add libraries to the client projects. So, how do I get the HttpClient into a PCL library and have it actually work in WP8? Is this realistically possible? Am I missing something? I read where some people say that it works, but from reading this forum, I have my doubts. Any suggestions or directions are welcome. Thanks for listening to me whine.

Wally

Posts

  • softlionsoftlion FRBeta ✭✭✭

    Here how i did it. Restart from a clean project.

    Create a PCL with iOS, Android, .NET 4.5 and WP8 (Profile 45). Add the microsoft Http nuget. It will install 2 other packages into the PCL.

    Create the Android and iOS projects. Reference your PCL in both projects. For me that's all. It just works.
    You may add the reference "System.Net.Http" in both platform projects. The reference is under Assemblies/Framework.

    NEVER add Microsoft.Http nuget in any native project, nor the BCL nuget. It will create mess and you'll have to clean lots of things before it can work again.

    Note that in profile 49 you have the new reflection api in the PCL. The old api won't work there.

  • SKallSKall USMember ✭✭✭✭

    Does the PCL work also on machines running Win7? Do you need to add any additional packages to Win7 machine to support Profile 45?

  • KrumelurKrumelur DEUniversity ✭✭

    @softlion Have you actually tried using new HttpClient(new CFNetworkHandler()) from iOS? That's where it' getting messy for me.

    The PCL (which uses Microsoft.Net.Http) and the iOS version (using System.Net.Http) build. But at runtime CFNetworkHandler is unknown. I presume because it is not part of Microsoft.Net.Http.

  • softlionsoftlion FRBeta ✭✭✭

    The pcl assembly is mapped to iOS monotouch assemblies and classes , including system.net.http.
    No more need for cfnetworkhandler

  • SKallSKall USMember ✭✭✭✭

    Any idea why I am getting an error on Android when the profile is 49?

    Error CS0012: The typeSystem.Enum' is defined in an assembly that is not referenced. Consider adding a reference to assembly System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' (CS0012)

  • JeffPutz.5342JeffPutz.5342 USMember, University

    I get no love on this either. Similarly, I have the HttpClient in a shared PCL, and using HttpClient is known to work in a WP or Windows project. The iOS project compiles fine, but when I call GetStringAsyn(url) on an HttpClient, it never returns.

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai
    edited January 2014

    The easiest example would be a little app I wrote in like a day, Puppy Kitty Overflow: https://github.com/jamesmontemagno/PuppyKittyOverflow

    I started with a PCL, Profile78, installed HttpClient Nuget (this is only needed because of WP, if you target Profile7 you do not need to). Then I created a new windows phone app and installed the HttpClient NuGet. That should be it. Create android project and reference System.Net.Http from reference assemblies. Create iOS app.

    It is extremely important that your NuGet manager is up to date, go to Tools-> Extensions and updates -> Updates

  • SKallSKall USMember ✭✭✭✭

    James, it is possible but with the state PCL currently is it is not really worth the headache. This problem today, solve it, another problem tomorrow. Once the problems are solved then one still needs to wait until you can use a WP8 targeted project on Win7. As things are right now PCL definitely isn't an option to recommend to anyone.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭
    edited January 2014

    Hi @JamesMontemagno, from what I understand the method you described works well, but one will need Visual Studio in order to download the latest System.Net.Http dll that support Windows Phone 8. Nuget only works with Visual Studio (i.e. Windows) correct? What if one did not have access to Visual Studio, or a Windows machine? If I wanted to create a PCL that targets iOS, Android, and Windows Phone 8, and want to utilize HttpClient how would one do so with just Xamarin Studio and a Mac?

    I tried copying the lastest System.Net.Http, System.Net.Http.Extensions.dll, and System.Net.Http.Primitives.dll (from a Windows machine via Nuget) manually referenced them on my Mac. My PCL compiled, but everytime I tried to call the .GetAsync(), the method would never return and eventually threw a timeout. I suspect System.Net.Http.dll might need to reference the latest BCL bits. I didn't dig into it too deep, but that's my suspicion.

    I think it might be worth while to ship the latest bits in the next release so that way it's not that much of a hassle. Any plans on doing so?

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    @rmacias Take a look at my blog post on the NuGet addin for Xamarin Studio: http://motzcod.es/post/70123632157/xamarin-studio-nuget-pcl-support I also have a short video on there showing how to set it up and use it.

  • sebastiankaysebastiankay USMember
    edited January 2014

    I started with a PCL, Profile78, installed HttpClient Nuget (this is only needed because of WP, if you target Profile7 you do not need to). Then I created a new windows phone app and installed the HttpClient NuGet. That should be it. Create android project and reference System.Net.Http from reference assemblies. Create iOS app.

    This is not working for me, unfortunately.
    I tried different profiles but apparently 78 is the most common one.
    I want to support Touch, Droid and WP8.
    However I'm stuck adding a HTTPClient to my PCL Core.

    Warning 1 The primary reference "System.Net.Http.Extensions" could not be resolved because it has an indirect dependency on the framework assembly "System.Threading.Tasks, Version=2.6.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETPortable,Version=v4.5,Profile=Profile78". To resolve this problem, either remove the reference "System.Net.Http.Extensions" or retarget your application to a framework version which contains "System.Threading.Tasks, Version=2.6.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

    I will try some other profile but right now I am kind of losing my faith in Xamarin and PCL approach for cross-plattform mobile application. It's getting more and more frustrating and buggy the deeper I dig...it's really a pity since it basically looks nice on paper.

    Anyway I appreciate any help you guys could give.

    edit:/ I also tried Restsharp.Portable....same error :/
    edit2:/ same for profile 49, doesn't work...neither RS Portable nor MS Http Client. :/

  • JeffPutzJeffPutz USMember

    The problem I've encountered (iOS) is that I can't get it to return anything by trying to access the result, as in:
    var result = client.GetStringAsync("http://foo").Result;

    That was my first try, just written in a button handler. I knew this would hang the UI if it worked, but was only concerned that the call was being made.

    Then I made a true async/await call in an async page loading method, and it worked:
    var result = await client.GetStringAsync("http://foo");

    For me it seems that my problem is that I don't know how to make a button click handler, generated in the alpha storyboard builder, to be async. Everything seems similar to WP8 development, but it's the little things.

  • sebastiankaysebastiankay USMember

    finally got it working with PCL profile 7!

    Thank you very much for your support, Xamarin! /irony off

  • RBellRBell USMember

    I was having a similar issue with the MonoTouch/iOS project throwing a reference assembly not found error. I ended up recreating my PCL projects and utilizing the Xamarin Studio NuGet plugin and it appeared to resolve all my compilation issues.

    To verify that the issue you are seeing is the same as mine, please open your project in Xamarin Studio, compile and view the Warnings section. You should see something like "All projects referencing 'Name of Project' need to install NuGet package 'Microsoft.Net.Http'". I received similar compiler warnings for the Microsoft.BCL libraries, even though it compiles in Visual Studio 2012 without issues.

  • @ sebastiankey - how did you get it working with profile 7? I'm consistently getting an error of "Could not install package 'Microsoft.Bcl 1.1.7'. You are trying to install this package into a project that targets
    'portable-net45+win+Xamarin.iOS10+MonoAndroid10+MonoTouch10', but the package does not contain any assembly references or content files that are compatible with that framework. " when I try to do so.

  • HumaylHumayl AEMember ✭✭

    HttpResponseMessage response = client.GetAsync("api/AJFMember").Result;
    At this moment; getting below error ,

    "Unknown member: GetAsync"

    Api is hosted on Cloud , through web browser it is accessing correctly.

  • HumaylHumayl AEMember ✭✭

    HttpResponseMessage response = client.GetAsync("api/AJFMember").Result;
    At this moment calling from Visual Studio using xamarin; getting below error ,

    "Unknown member: GetAsync"

    Api is hosted on Cloud , through web browser it is accessing correctly.

  • HumaylHumayl AEMember ✭✭

    ohhh, its my > @Humayl said:

    HttpResponseMessage response = client.GetAsync("api/AJFMember").Result;
    At this moment calling from Visual Studio using xamarin; getting below error ,

    "Unknown member: GetAsync"

    Api is hosted on Cloud , through web browser it is accessing correctly.

    its solved , its my mistake , internet was not dc on Physical device.

Sign In or Register to comment.