C
C#7mo ago
Very Funny

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 issues
No description
22 Replies
PixxelKick
PixxelKick7mo ago
You have your IAdminService registered in the BlazorApp9 project, but your Admin page is over in the BlazorApp9.Client project
Very Funny
Very Funny7mo ago
I have IAdminService and AdminService in both my client and server projects
PixxelKick
PixxelKick7mo ago
oh my bad so you do
viceroypenguin
viceroypenguin7mo ago
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?
Very Funny
Very Funny7mo ago
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
viceroypenguin
viceroypenguin7mo ago
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.
Very Funny
Very Funny7mo ago
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
viceroypenguin
viceroypenguin7mo ago
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.
Very Funny
Very Funny7mo ago
Ah. I can get back to you on that later today when I’m home
viceroypenguin
viceroypenguin7mo ago
specifically, the server URLs are "https://localhost:7148;http://localhost:5066", from the launchsettings.json
Very Funny
Very Funny7mo ago
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
viceroypenguin
viceroypenguin7mo ago
@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.
Very Funny
Very Funny7mo ago
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
Very Funny
Very Funny7mo ago
No description
viceroypenguin
viceroypenguin7mo ago
Sure, what does the exception message say? Can you think of what it means and how that relates to the URIs in your code?
Very Funny
Very Funny7mo ago
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
viceroypenguin
viceroypenguin7mo ago
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.
Very Funny
Very Funny7mo ago
Even though the following is correct. The URI base address doesn't seem to presist to my client side AdminService
No description
viceroypenguin
viceroypenguin7mo ago
but there's a way to deal with that, and i don't know what that is yet.
Very Funny
Very Funny7mo ago
c#
public AdminService(HttpClient httpClient)
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri("https://localhost:7148/");
}
c#
public AdminService(HttpClient httpClient)
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri("https://localhost:7148/");
}
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
viceroypenguin
viceroypenguin7mo ago
i'm curious how you think keyedsingleton is going to help you... do you know what keyed registrations are for?
Very Funny
Very Funny7mo ago
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