0

I wrote a simple .Net Core 3.0 API using Swashbuckle Swagger and generated an api client via NSwag Studio, then I put a generated Api Client to a .Net Standard 2.0 project. I have a Universal Windows Platform application, which is meant to be connecting to the Web Api and send/receive data etc.

I put a simple code in MainPage.xaml.cs class with System.Net.Http.HttpClient inside

public MainPage()
    {
        this.InitializeComponent();
        var httpClient = new HttpClient();
        var apiClient = new ApiClient(ProjectConstants.API_URL, httpClient);
        var deviceService = new DeviceService(apiClient);
    }

When API is called later in deviceService my program throws an exception on SendAsync method in generated Api Client

"An error occurred while sending the request."

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

I tested API Client and HttpClient on .Net Core console app and it worked fine. I read about Http Client for UWP https://learn.microsoft.com/en-us/windows/uwp/networking/httpclient but it seems UWP should support System.Net.Http.HttpClient too, which I would love to stick to. Is this a Universal Windows Platform bug or do I forgot about adding something necessary to a project?

Hantick
  • 63
  • 7

2 Answers2

0

The System.Net.Http.HttpClient API can be used across platforms, but we do recommend using APIs from Windows.Web.Http.HttpClient NameSpace which is easy to use. So you could use Windows.Web.Http.HttpClient API in UWP,and use GetAsync(PostAsync) to send a request, for example:

Uri requestUri = new Uri("http://www.contoso.com");
Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
httpResponse = await httpClient.GetAsync(requestUri);

For more information about the differences between Windows.Web.Http and System.Net.Http.HttpClient, you can refer to this article

  • 1
    Well, "Do you intend to write cross-platform .NET code (across UWP/ASP.NET 5/iOS and Android)? If yes – then use System.Net.Http API. This allows you to write code that you can re-use on other .NET platforms such as ASP.NET 5 and .NET Framework desktop applications. " I should stick to `System.Net.Http.HttpClient` in my projects – Hantick Oct 28 '19 at 09:10
  • @Hantick Why you add `ConfigureAwait` after `SendAsync` that already use await key word? – Nico Zhu Oct 28 '19 at 09:27
  • @NicoZhu-MSFT it is auto-generated by NSwag – Hantick Oct 28 '19 at 13:06
  • @Hantick, could you try to remove `ConfigureAwait` and test again? – Nico Zhu Oct 29 '19 at 01:13
  • @NicoZhu-MSFT I removed `.ConfigureAwait(false)` everywhere, no effect :( – Hantick Oct 29 '19 at 21:15
  • @Hantick, Sorry to head that, but in uwp platform we suggest `Windows.Web.Http.HttpClient` to used, if you want to make cross platform lib, you need implement httpclient separately base on `Windows.Web.Http.HttpClient`. – Nico Zhu Oct 31 '19 at 08:08
0

Does it have the same api as the portable HttpClient?

Rico Suter
  • 11,548
  • 6
  • 67
  • 93