SteveSandersonMS / WebWindow

.NET Core library to open native OS windows containing web UI on Windows, Mac, and Linux. Experimental.
Apache License 2.0
1.98k stars 215 forks source link

HttpClient and general questions. #20

Closed Bartmax closed 4 years ago

Bartmax commented 4 years ago

I see the demo is reading a file instead of doing an http request. It makes sense since it's a desktop app loading data from itself, but I tried to do an actual http request using the blazor specific HttpClient (Microsoft.AspNetCore.Blazor.HttpClient) without luck.

Is this something currently not working? I'm interested to understand why. Also, I see no error on output nor console of the "broswer". Is there anyway to actually see errors ?

Thanks!

SteveSandersonMS commented 4 years ago

Don't use Blazor.HttpClient, because this isn't running under webassembly. Just use the regular System.Net.HttpClient instead.

Bartmax commented 4 years ago

I cannot make it work either using the regular System.Net.HttpClient:

var client = new HttpClient() { BaseAddress = new Uri("https://www.google.com")  };
data = start; // <-- this line is called and works
data = await client.GetStringAsync("/");
data = end; // <-- this is never called, no error, no nothing.

on a service implementation:

try
{
    var response = await client.GetAsync("https://www.google.com/"); 
    // here stops execution without getting into the catch method.
    var sc = response.StatusCode;
    var data = await response.Content.ReadAsStringAsync();
    return data;
} 
catch (Exception e)
{
    return e.Message;
}

the client.GetAsync(anyvalidurl) stop the execution with no error nor entering a catch block.

went ahead and cloned your sample project and modified the FetchData.razor page:

<p>@data</p>
@code {
    string data = "";
    protected override async Task OnInitializedAsync()
    {
        data = "start";
        var response = await new HttpClient().GetAsync("http://www.google.com");
        data = "reponse done"; <-- this is never executed. no error
    }
}

I experienced same behavior. What am I missing ?

SteveSandersonMS commented 4 years ago

That's interesting. Thanks for the extra info. I'll look into it when I can. You might find that running with the debugger enabled from VS, with it configured to break on all .NET exceptions, might show you what the error is.

Bartmax commented 4 years ago

Ok, found out that it actually "works", just takes ~2 mins to get the payload which is still very weird, but at least it works. Not sure if you want to keep this open or not.

SteveSandersonMS commented 4 years ago

I tried this, and it returned the response immediately, so I'm afraid I can't repro your issue. My guess is that it's something to do with your machine's network setup (maybe slow DNS or something).

In any case it's not related to WebWindow since the code you're running for this issue is just pure normal .NET Core code. If there's an issue here, it must apply to .NET Core generally not just to WebWindow.

Closing - hope that makes sense.