Hi,
I am trying a scenario where I would use server-side blazor with WebApi.
For that, I created project from self-hosted template and modified Startup method to match the one created in server-side template and I also changed script include in index.html.
But I get an error in razor page when I try to inject HttpClient:
Unhandled Promise Rejection: Error: System.InvalidOperationException: Cannot provide a value for property 'Http' on type 'CarFleet4.Client.Pages.FetchData'. There is no registered service of type 'System.Net.Http.HttpClient'.
I can probably manually provide HttpClient for injection, but I was wondering if this is intended or not?
Thanks,
Mario
I believe your issue is related to: Server Side Blazor doesn't register HttpClient by default
This may help you: https://github.com/Suchiman/BlazorDualMode
If it does, please let us know...
Hi,
Well, almost.
I am trying to use server-side Blazor with Blazor.FlexGrid (https://github.com/Mewriick/Blazor.FlexGrid) with LazyDataSetLoader - which is injected as singleton.
Above code injects HttpClient as Scoped which results in following error when invoking view:
System.InvalidOperationException: Cannot consume scoped service 'System.Net.Http.HttpClient' from singleton 'Blazor.FlexGrid.DataSet.ILazyDataSetLoader1[CarFleet4.Client.Pages.DrivingData]'.`
However, if I try to inject HttpClient as singleton I get another error when adding HttpClient to services:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.DependencyInjection.dll but was not handled in user code: 'Cannot resolve scoped service 'Microsoft.AspNetCore.Blazor.Services.IUriHelper' from root provider.'
I apologise if I am missing obvious stuff or trying something that is not supported nor should work, I am just really excited about Blazor :-), especially server-side and I am exploring how I could use it for my next project.
Regards,
Mario
Ok, I got it working by simply not using IUriHelper, but instead hard-coding base url which is fine for testing purposes.
However, why exclude HttpClient injection from server-side Blazor?
For instance, server-side blazor with WebApi is completely valid scenario where you have web application and mobile application and/or external apps accessing data through WebApi.
@mblataric, I agree with your last comment... I believe that in most cases Web Api should be used even if one uses the server-side flavor of Blazor, which requires HttpClient. Previously, the focus was on creating services to retrieve data, but now, it seems to me, to have shifted to the use of Web Api, regardless of the Blazor execution mode.
Don't forget that Blazor is 'under construction', and its creators seek to produce the best product.
I believe that in the future Server Side Blazor will register HttpClient by default
Suchiman, is the expert who can answer your questions most seriously...
@mblataric see my Explanation here https://github.com/Suchiman/BlazorDualMode/issues/1#issuecomment-429947281
We inject an HttpClient on the client-side because historically we had to setup the correct underlying message handler for you and we also prepopulate the right base address. On the server neither is needed. On the server we'd expect you to use something like the IHttpClientFactory
@danroth27 What is the reasoning for using IHttpClientFactory instead of HttpClient (named or unnamed)? It seems like aspnet core has a lot of support for HttpClient in DI.
@devlife On the server IHttpClientFactory provides a number of benefits for making general HTTP requests that are outlined here: https://docs.microsoft.com/aspnet/core/fundamentals/http-requests.
The HttpClient provided for Blazor WebAssembly Apps is bit more specialized and is specifically configured for making requests back to the server of origin.
Most helpful comment
I believe your issue is related to: Server Side Blazor doesn't register HttpClient by default
This may help you: https://github.com/Suchiman/BlazorDualMode
If it does, please let us know...