Dependency Injection not working (.NET 8)
I am trying to set up DI for my services but I keep running into the same error, regardless of what I try.
builder.Services.AddScoped<IAdminService, AdminService>();
Is what I keep coming back to in my client side app, but nothing is working.
You can find my code here https://github.com/Ghrafkly/BlazorApp9. You can do a find string on WEQ
to quickly locate the files that are causing issues22 Replies
You have your IAdminService registered in the
BlazorApp9
project, but your Admin page is over in the BlazorApp9.Client
projectI have IAdminService and AdminService in both my client and server projects
oh my bad so you do
https://github.com/Ghrafkly/BlazorApp9/blob/b99db7a3a0c867d5731e776f8730e268c31db130/BlazorApp9/BlazorApp9.Client/Program.cs#L16
what value is in
builder.HostEnvironment.BaseAddress
?
what is the correct server API base url?Idk. That’s what was in the default configs when I generated my .NET 7 project. I just copied it across
I am aware that there will be things I can’t copy verbatim from 7 to 8. Just a matter of figuring out what out what
you should know. or you should be able to debug it. if you can't debug it, then i would encourage you to spend more time learning how to debug programs, as a critical tool in your programming belt. regardless, there's nothing else i can do to help you unless you can try the basics.
I’ve tried debugging but I haven’t gotten any insights from it
I do use debugging a lot at work and in other projects. But this is also my first C# project ever so maybe I am missing something
so what is
builder.HostEnvironment.BaseAddress
? you should know this from debugging
and what is the server API base URL? You should know this from either launchsettings.json or from the browser window open to the server project.Ah. I can get back to you on that later today when I’m home
specifically, the server URLs are "https://localhost:7148;http://localhost:5066", from the launchsettings.json
It should be 7148 as that is what my application is running on
Also I am running my project with https, so yeah 7148 definitely
@Very Funny created a PR that fixes it for you.
You have to use the same interface from
Client
for both server and client Service. Then the server can implement the interface for the client page on the server side, while the client can implement the interface fro the client page on the client side.Looking forward to trying it out when I get home
I coulda sworn I had tried it that way, maybe I thought about it but never actually gave it a shot.
The past few days have just been a haze of errors and bugs 😞
IT WORKS, thank you so so much
feels like I can breathe again
am having issues with my endpoints and URI
It loads the data properly at first then it errors
Sure, what does the exception message say? Can you think of what it means and how that relates to the URIs in your code?
I spent the past few hours looking into it
I think the double reference of IAdminService might be breaking it. Cause it also causes my ManageAdmin page to load twice
it also causes my ManageAdmin page to load twice - this is more likely due to the fact that the first rendering is done on the server and the second rendering is done on the client. of course it's going to run twice.
Even though the following is correct. The URI base address doesn't seem to presist to my client side AdminService
but there's a way to deal with that, and i don't know what that is yet.
I have a hacky fix that works, I might just keep it like this for now
I looked into AddKeyedSingleton etc. It feels like that would be what I'd need. Unsure how I would implement it though
i'm curious how you think keyedsingleton is going to help you...
do you know what keyed registrations are for?
not really, just glanced at the documentation and one youtube tutorial
What are the Pro Cons of
@rendermode InteractiveServer
for server-side pages vs @rendermode InteractiveAuto
for client-side pages?
cause @rendermode InteractiveServer
might save me from the past 4 days of pain I have had