Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Issue with HttpClient and Xamarin Forms #2711

Closed
noelfernandes opened this issue May 15, 2018 · 5 comments
Closed

Issue with HttpClient and Xamarin Forms #2711

noelfernandes opened this issue May 15, 2018 · 5 comments
Labels
s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. t/bug 🐛

Comments

@noelfernandes
Copy link

noelfernandes commented May 15, 2018

Description

I'm having issues trying to do a http request using HttpClient and Modernhttpclient-updated. It worked initially in v2.5.1.527436 but started giving issues in v3+

Steps to Reproduce

  1. Fire up a new XF .NET Standard solution.
  2. Update nugets to latest stable Xamarin forms.
  3. Add Newtonsoft.JSON & Modernhttpclient-updated nugets to solution.
  4. Create the necessary Model classes. I have a model called 'Comment' that looks as follows:
    public class Comment { public int postId { get; set; } public int id { get; set; } public string name { get; set; } public string email { get; set; } public string body { get; set; } }
  5. Create a Service class with the required interface to download data (using HttpClient and Modernhttpclient-updated) from an API and serialize to List. I am using this link for API, for testing.
  6. In Android Project properties, Android Options > Advanced, set HttpClient Implementation = Android and SSL/TLS Implementation = Native TLS 1.2+
  7. Place breakpoint on client.GetAsync().
  8. Attach phone, set Android project as default and hit debug.
  9. When the debugger reaches line mentioned in point 6, hit F10.

Expected Behavior

It should download the data from the API in JSON format and response should have a StatusCode of 200 Ok if successful or anything else indicating appropriate http errors.

Actual Behavior

The solution just exits debug mode.

Basic Information

  • Version with issue: Xamarin Forms 3.0.0.482510
  • Last known good version: Not sure. I think it was XF v2.5.1.527436 but I cannot get it to work in this version either.
  • IDE: VS 15.6.4
  • Platform Target Frameworks:
    • iOS: Haven't tested as yet. Currently focusing on Android
    • Android: 8.1
    • UWP: No UWP project
  • Android Support Library Version:
  • Nuget Packages:
    Modernhttpclient-updated 2.7.0, Newtonsoft.Json 11.0.2, Latest Xamarin Forms 3.0.0.482510 stable
  • Affected Devices:
    Nokia 5

Screenshots

Reproduction Link

I have reproduced the error here in this solution
This the output from the Debug Console

Other things tried so far:

  • I have tried setting Android Options > Advanced, set HttpClient Implementation = Default and SSL/TLS Implementation = Default. No difference
  • I have also debugged the solution without the Modernhttpclient-updated nuget. No difference
  • Since the current version (i.e. 2.7.0) of Modernhttpclient was updated and released around the same time of XF3+, I have also tried downgrading Modernhttpclient-updated to v2.6.0. No difference.
  • I have reverted back to XF v2.5.1.527436 to see if it works but it did not. No difference.
@pauldipietro
Copy link
Contributor

If you can please upload this as a minimized reproduction (so we have precisely the same code you're using), it would be much appreciated. Can you also clarify if the HttpRequest is still returning a 200 status/the status you would expect for the executed request, and that the problem is strictly that the debug quits? If that is the case, then the issue could potentially lie elsewhere.

@pauldipietro pauldipietro added t/bug 🐛 s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. labels May 15, 2018
@noelfernandes
Copy link
Author

noelfernandes commented May 15, 2018

Hi @pauldipietro,
Thanks for your response.
Also! Please excuse my ignorance...what do you mean by minimized reproduction. The link I have provided to the solution in my git repo contains the bare minimum to reproduce this issue. You will see that its a basic XF project containing the minimum nugets to reproduce the issue. Is there anything else I should be providing?

No! As mentioned, the httprequest does not produce any sort of status, instead while debugging, it just exits after spewing out logs on thw debug console. If run without debugging, it just exits.

Also! I have tried this out without using modernhttpclient-updated nuget and face the same.

Thanks.

@Jalroka
Copy link

Jalroka commented May 16, 2018

@pauldipietro In the demo project after retrieving the posts, the count is not being assigned to the label. That is only happening in the constructor before the request is made to get the posts.

Try changing your method to read like this.
public async void FetchData()
{
HttpRequestService = new RequestService();
Posts = await HttpRequestService.GetAsync<List>("https://jsonplaceholder.typicode.com/comments", string.Empty);
this.LblMessage.Text = $"We have {Posts.Count} posts";
}

As a side note method with an async void signature is a bad idea and typically should only be used for an event handler, otherwise it's best to use async Task.

@noelfernandes
Copy link
Author

@Jalroka : You're a Star

Thank you very much. That worked.

With regards to the side note, the reason why it was async void is because I was calling it from a constructor. I have seen quite a few articles on that. However now that I am using RelayCommands, I have changed the method signature to async Task and I use the RelayCommand's .Execute(null) method in the constructor of the ViewModel. All works well now.

Once again. Thank you very much.

@Jalroka
Copy link

Jalroka commented Dec 21, 2018 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. t/bug 🐛
Projects
None yet
Development

No branches or pull requests

3 participants