dependency injection net framework
hi all, I'm following a reference on how to use dependency injection in net framework
https://stackoverflow.com/questions/43311099/how-to-create-dependency-injection-for-asp-net-mvc-5
i tried to implement the solution the way they register the services, but when i run the controller i saw that the Interface is not initialized.. even though when i try to debug, the program run through the initialization part like this ... does anyone have any idea maybe what could be the issue..?
Stack Overflow
How to create dependency injection for ASP.NET MVC 5?
Creating Dependency Injection with ASP.NET Core is fairly easy. The documentation explains it very well here and this guy has a killer video to explain it.
However, I want to do the same thing wit...
29 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
IDaily != IDailyConversion
It doesn't matter if IDaily
inherit from IDailyConversion
or not, the registration has to be exact.my bad, i mistype the name, yup.. it should be (edited the message)
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
yes.. actually i implemented the interface..
tbh.. i tested the code works in net core, but for some reason i have to do it in net framework, so i try to move the code, register all the service and all in net framework way... but somehow i got null value...
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
im using net framework 4.8 version.. will net version affect the dependency injection implementation (like it can't be used in a certain version..)?
@Yawnder @MansEternalQuest can i understand.. is it not possible to pass Interface to a controller constructor (in net framework) like above..?
Not too sure why you would say that, but no.
@qian Can you show your registrations? All of them.
yup.. it's a bit long so i do screenshots instead of copying the code.. here are the services that are registered..
actually because only after i added an empty constructor for this DailyConversion controller it will works, but not the constructor with interface parameter inside.. so i wondering if it's different in net framework..
If you put a breakpoint in your
AddControllersAsServices
, does it hit it properly?
What does your Controller look like? (the class definition and the constructor)yes.. it does..
You do
AddScope<DailyConversion>()
. If you're trying to inject an interface, this will not work.my controller looks like this..
No, I mean inside
AddControllersAsServices
. Do you see it hit the controller registration.
Ok. Remove that parameterless constructor
Change AddScope<DailyConversion>()
for AddScope<IDailyConversion, DailyConversion>()
like it was before.yup.. it enter the class as well..
Inside the foreach. Can you see
type == typeof(DailyController)
?
(The answer is probably no)no.. it doesnt enter inside the for loop...
So it means your lambda filters it out.
Can you just add
IController
on your controller and try again?
(Maybe it's not registering it as implementing IController
because Controller
inherits from it, and not directly.)
DailyController : Controller, IController
But that wouldn't change anything actually.
Can you explicitly register your controller and try again?the program still doesnt enter this loop
No.
services.AddScoped<DailyController>();
It's normal that it doesn't enter the loop.still the same message from the browser with parameterless constructor..
I really feel like your whole project is setup wrong, but it's hard to say with bits and pieces.
im not sure if this part could be an issue..
because this part im following the link from stackoverflow.. but then i found another version of answer from stackoverflow that we dont use "StartUp"..
https://stackoverflow.com/questions/54584671/dependency-injection-in-net-4-7
i think im able to setup a new project.. is it the issue with web application project that i setup?
No idea what the issue is. You have all the information, and all I have are bits and pieces through screenshots.
is it possible if i push the project to github for you to see clearer.. is that okay?
Nah, I already spent enough time on that, I need to go to bed, sorry.
Okay 😢