Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Razor Component Server Side : Inject HttpClient in service broke entire application #7851
Describe the bug
When I try to inject HttpClient in a service, then inject the service in a page, it's broke the rendering (blank page, no error in browser console)
1 : Create a Razor Components project, with .net core 3 preview and Visual studio 2019. It create normally two project *.App and *.Server
an application wich works, or at least an exception on why it can't be possible
I read the HttpClient is not natively registered in the server side razor. so i tried registred one in the startup.cs with this piece of code find somewhere. It's seem changing nothing
I cannot reproduce your error. Your code is running without issues on my side (Razor Components 0.8, .Net Core 3.0 Preview 2, VS 2019).
It's hard to find your issue without seeing some more code, so please share your solution.
However, it's enough to register the HttpClient with
To reproduce this error, there is no really code, just create an empty SERVERSIDE razor project and follow the step to reproduce. (I precise server-side because you speak about 0.8 version, however with server side razor components there is no dependency to 0.8 library)
I have only these dependencies in the App Project (front project on server Side)
Honestly, I'm really surprised about the complexity to switch my client-side project to server side, its seem really more difficult compare to what peope say with older version (the type of project are different : console/library, the wwwroot is not in the same project, the dependecy off the App project are different, and now the HttpClient who seem not work on the server side).
I test actually inject an IHttpClientFactory instead
Inside the App project,
Inside the server project,
@darren-zdc i'm not agree, with the last framework (.net core 3 preview), when you create a razor component project, there is no dependency to something like blazor. Try yourself, create a new razor component project in your visual studio (but maybe there is something i don't understand).
I still not understand good how i can design a server side razor component application. There is a lack of documentation, example, tutorial, for exemple how to manage data from server, manage authentication, things like that.
but i definitively not understand how structure my application. With the client-side version of blazor, i called a rest login service which create a JWT and return them to the client. with the server side, it seem weird to call a web service whereas i'm already on the server)
If someone have a good example i can read and understand i take it ! (with the last version of course).
PS : I finally solve my problem by injecting an IHttpClientFactory in my service and register with services.AddHttpClient(); in Startup.
I already migrate my application, it's not really the problem.
design of my actual client side blazor application
Client (App) :
For me, this kind of design is really clear, it was really simple to do that with blazor.
Now i really need to debug with efficiency my application because I retrieve like a hundred thousand of line of code (c# models and silverlight ViewModels adapted to work with no dependency), My project is to rewrite a silverlight application without rewriting the business code because the silverlight application will continue evolve during my development and the code is really complicated, so the two project (the old silverlight and my new Blazor app) will share the same code until the end of the migration (wich can be at least one year).
so I decided to switch to razor components to debug the viewModel included in my blazor app.
but now, there is lot of things i don't know how to design.
I'm really surprised to not find good example of a project on razor component (except the classic project with a button wich increment an integer, but this exemple is definitively too simple).
I don't know if i am the only who block on the server-side logic....
definitively a good example will be welcoming (for example, migrate the flightFinder project in the last server side razor components with the good guidelines of design)
Agree with you buddy. They say Razor Components is no longer experimental (on the contrary to Blazor) but in my opinion they are doing a lot more breaking changes to Razor Components between different preview versions than they were doing between Blazor minor versions. For example in Preview 3 the app and server are now in one project and the routing pipeline has changed which for example broke MVC/API controllers in lot of libraries etc.
The original issue described by this bug appears to now provide a much better error:
As for the design question, you are correct that there is no point in calling into an API hosted on the server when running a server-side Blazor app. Instead you can inject services into your components that access server-side resources directly. To maintain symmetry with client-side Blazor you can inject different implementations of the service interface that either call APIs or access server-side resources as appropriate for the given environment.
Closing as there doesn't appear to be anything else to do here.