-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Blazor call web API topic with sample #12477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
dbfcba3
to
d9f1a5a
Compare
If you can guide me on the untrusted cert situation ... how to handle it better than this (just have them specify whatever cert that they plan to use here?) ... along with any gotchas! ... then I could roll this into the topic (but not the sample app given that we only have a client-side common sample for our Blazor topics at this time). [EDIT] Also noticed that the Http requests topic does it a bit differently than what I show from a dev's post. https://docs.microsoft.com/aspnet/core/fundamentals/http-requests Alternatively: We could label this topic clearly (a NOTE) that it's for Blazor client-side at this time and cross-link the engineering issue wrt the server-side design. btw ... just curious @dazinator ... is this about what you ended up doing 👇? Project file<ItemGroup>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup> Startup.ConfigureServicesservices.AddScoped(s =>
{
var uriHelper = s.GetRequiredService<IUriHelper>();
return new HttpClient
{
BaseAddress = new Uri(uriHelper.GetBaseUri()),
};
}); OR w/o a UriHelper setting the base address and using the services.AddHttpClient(); Component@using System.Net.Http
@code {
private async Task MakeHttpClientRequest()
{
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback =
(message, cert, chain, errors) => true;
using (var client = new HttpClient(httpClientHandler))
{
// Use client
}
}
}
} |
@danroth27 Shall we proceed with this? The client-side here looks good ... works great ... based on the web API sample provided by our existing ASP.NET Core web API sample app topic. The problem is server-side, which I'm aware is in-design. We could either:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good! Just a few suggestions.
aspnetcore/blazor/common/samples/3.x/BlazorSample/Components/HTTPRequestTester.razor
Outdated
Show resolved
Hide resolved
Yes I registered my singleton httpclient with DI, and configured it myself in the factory delegate. However.. my eyes have just been opened to the So today I am now doing this:
I don't directly inject Are there known issues with doing it this way within a blazor client app? |
@danroth27 Update pass made.
|
I'm not a fan of the note 😜. This topic isn't just for client-side Blazor. Also, the statement about how the The thing to realize here is that you don't typically want to make HTTP requests from a server-side Blazor app back to the same server. Instead you should abstract away this concern using your own service that has different implementations depending on where the app is host. For example, you would define an
Nope 😄. Blazor doesn't include a using statement for
Let's just ignore this for now. We can add more details later if we're missing something. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few minor tweaks, but otherwise I think this looks fine.
You see where this all went wrong, right? 😄 ...
That was the hang-up. What I was doing is making it all about injected I see what you want now, but it seems like if we're going to wait on the design for server-side (not mention it) and not show a workaround (e.g.,
😄 lol ..... I didn't LOOK! 🙈 lol |
Sorry about my confusion, but what workaround are you referring to? How is |
In order to use
Nevermind that ... I thought for sec that we wouldn't use the So, the topic will go with |
I think what this may be referring to is that some folks have been adding an HttpClient as a service in their server-side Blazor apps to create some symmetry with client-side Blazor. But most of the time I don't think that's what you really want, because then in your server-side Blazor app you end up making HTTP requests back to your own server. Instead, if you want to use the same component code independent of where the app is hosted, you should create a service that abstracts away how the data is requested. In Blazor server-side apps your making HTTP requests to third party service and the story is no different than it is for existing ASP.NET Core apps, so we should just say that and point to https://docs.microsoft.com/aspnet/core/fundamentals/http-requests. The |
@danroth27 Recommend one more quick 👁️ on this to confirm that the feedback updates are correct. I ended up not going with a server-side example. I didn't put it in because it's fairly trivial to implement. As you suggest, I punt the reader over to the HTTP Requests topic. However, let me know if you'd like to see an example here. |
aspnetcore/blazor/common/samples/3.x/BlazorSample/Pages/CallWebAPI.razor
Outdated
Show resolved
Hide resolved
aspnetcore/blazor/common/samples/3.x/BlazorSample/Components/HTTPRequestTester.razor
Outdated
Show resolved
Hide resolved
aspnetcore/blazor/common/samples/3.x/BlazorSample/Components/HTTPRequestTester.razor
Outdated
Show resolved
Hide resolved
aspnetcore/blazor/common/samples/3.x/BlazorSample/Components/HTTPRequestTester.razor
Outdated
Show resolved
Hide resolved
Co-Authored-By: Scott Addie <[email protected]>
Thx @scottaddie 🚀 WRT the constants situation: It breaks 💥 in a non-page component (the HTTP Request Tester component) (and Steve has it that way in the engineering repo version of that component), but it's 👍 in a page component (the Call Web API component). Therefore, I made it a constant only in the page component. Idk if that should be reported in engineering, but Dan is 👂 here and will let us know. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more comments, but otherwise looks good!
@scottaddie Do u want to run a final 👁️ over the updates? |
|
Hey @danroth27, this is an extremely useful insight. When reading thought the documentation I was unable to find something like this. For people with an ASP.Net Core app development background (specially those of us who deal with microservices architectures), the Blazor Server HTTP Client situation can be confusing. Thanks for clearing this out. |
Fixes #10743
Internal Review Topic
There is no registered service of type 'System.Net.Http.HttpClient'.
... I see what you mean on the engineering issue. Will take ur advice ...IHttpClientFactory
for server-side.