2

I have quite decent problems with this very simple task and asking for help. The question is: what is the best and the most reliable way to perform Http requests with timeout from Xamarin PCL?

There are several options:

  1. Using old HttpWebRequest. This solution should definitely be working, but I have problems with BeginGetRequestStream method described in this question: HttpWebRequest BeginGetRequestStream callback never called Also it requres some extra ugly code to implement timeouts properly.

  2. HttpClient. HttpClient is not available in PCL out-of-the-box (I am using Profile 158 required by MvvmCross libraries). I was trying to install Microsoft HTTP Client Libraries but I still cannot compile the PCL project, error is:

    The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

  3. PortableRest NuGet package. Same problem - does not seem to work in PCL. Lots of compilation errors and warnings, for example:

    The primary reference "System.Net.Http" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETPortable,Version=v4.0,Profile=Profile158". To resolve this problem, either remove the reference "System.Net.Http" or retarget your application to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". (MSB3268) (Prototype.Core)

What am I doing wrong? Why am I not able to use such basic standard libraries?

UPDATE

Finally solved the issue by switching to Profile 78 (.NET 4.5). Now I can use HttpClient which is way better and easy to use. It works reliably with all my requests. MvvmCross libraries work fine with Profile 78 as well.

The only issue I have is lack of Timer class. Maybe I should implement it myself using Task.Delay.

2
  • 1
    On HttpWebRequest I believe your only problem is with Timeout - I believe this was omitted from the Compact Framework and so is not available in WP or in the PCLs. The reason for this omission is the word compact - they had to leave some things out in order to make the compact framework fit inside smaller memory spaces. The MvvmCross network plugin uses HttpWebRequest and I've never had any never called back type problems with it - but it does not support Timeout directly - I find this can be worked around using some simple, clean Timer and Abort code. Commented Mar 23, 2014 at 10:11
  • 1
    If you plan on using HttpClient cross-platform, check out this post by James Montemagno: motzcod.es/post/78863496592/…. Commented Mar 24, 2014 at 14:38

2 Answers 2

4

I recommend using the HttpClient package via NuGet http://www.nuget.org/packages/Microsoft.Net.Http/

This works with profile 78 and MvvmCross. You also get async/await support.

Sign up to request clarification or add additional context in comments.

2 Comments

I've used this in 158 too - e.g. in the video of slodge.blogspot.co.uk/2013/11/…
I originally went that route but there were too many issues getting the HttpClient to work on Android. I ultimately went with an abstracted IRestClient interface and dependency injection. This also made it very easy to use ModernHttpClient on Android and iOS.
1

What about HttpClient, I've extended Profile 7, so it now supports WP7/8, iOS, Android and WinStore and it works great, no problem yes and I've already complete a couple of projects with it.

4 Comments

Do you use MvvmCross?
As I understand I am forced to use 158'th profile since I use mvvmcross libraries (which are great).
The latest versions of Xamarin support profile 78. And yes you can use MvvmCross with this profile.
@Alex Petrenko Yes I'm and I also extend profile 7, to add Win7 to the list of platforms, just forget to add it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.