C
C#•2y ago
joy

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
services.AddScoped<IDailyConversion, DailyConversion>();
services.AddScoped<IDailyConversion, DailyConversion>();
... 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
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder•2y ago
IDaily != IDailyConversion It doesn't matter if IDaily inherit from IDailyConversion or not, the registration has to be exact.
joy
joy•2y ago
my bad, i mistype the name, yup.. it should be (edited the message)
services.AddScoped<IDailyConversion, DailyConversion>();
services.AddScoped<IDailyConversion, DailyConversion>();
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
joy
joy•2y ago
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
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
joy
joy•2y ago
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..?
Yawnder
Yawnder•2y ago
Not too sure why you would say that, but no. @qian Can you show your registrations? All of them.
joy
joy•2y ago
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..
Yawnder
Yawnder•2y ago
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)
joy
joy•2y ago
yes.. it does..
Yawnder
Yawnder•2y ago
You do AddScope<DailyConversion>(). If you're trying to inject an interface, this will not work.
joy
joy•2y ago
my controller looks like this..
Yawnder
Yawnder•2y ago
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.
joy
joy•2y ago
yup.. it enter the class as well..
Yawnder
Yawnder•2y ago
Inside the foreach. Can you see type == typeof(DailyController) ? (The answer is probably no)
joy
joy•2y ago
no.. it doesnt enter inside the for loop...
Yawnder
Yawnder•2y ago
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?
joy
joy•2y ago
the program still doesnt enter this loop
Yawnder
Yawnder•2y ago
No. services.AddScoped<DailyController>(); It's normal that it doesn't enter the loop.
joy
joy•2y ago
still the same message from the browser with parameterless constructor..
Yawnder
Yawnder•2y ago
I really feel like your whole project is setup wrong, but it's hard to say with bits and pieces.
joy
joy•2y ago
im not sure if this part could be an issue..
services.AddControllersAsServices(typeof(StartUp).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
.Where(t => typeof(IController).IsAssignableFrom(t)
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)));
services.AddControllersAsServices(typeof(StartUp).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
.Where(t => typeof(IController).IsAssignableFrom(t)
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)));
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
joy
joy•2y ago
i think im able to setup a new project.. is it the issue with web application project that i setup?
joy
joy•2y ago
Yawnder
Yawnder•2y ago
No idea what the issue is. You have all the information, and all I have are bits and pieces through screenshots.
joy
joy•2y ago
is it possible if i push the project to github for you to see clearer.. is that okay?
Yawnder
Yawnder•2y ago
Nah, I already spent enough time on that, I need to go to bed, sorry.
joy
joy•2y ago
Okay 😢
Want results from more Discord servers?
Add your server
More Posts
Safe to ignore non-null value on exit contructor? (disabling warning)Can I safely add this as a ignore? I don't really know how I would fix this otherwiseHow should I start learninghi everybody I started learning c#, going through basics and want to ask those who in industry alreaCreating an instance of a class using DI [Answered]This is purely just out of curiosity. If you have a class which takes some arguments in its ctor andIs there any benefit to write a Property with a private variable ?Why: ```cs private int myVar; public int MyVar { get => myVar; set => myVar = value; } ``` may be beHow add analyzer for suggesting to add missing parameters or removing them?Soo I have analyzer which checks if parameter type is proper soo then I need to make codefix which j[ASP.NET] B-Logic Services, ValidationI am using anemic models, therefore I want to implement Business Logic Services. And the questions Optimizing some string manipulationI want to both substring an input string at the last occurrence of `'/'` and normalize it into *onlyRight way to use Interfaces in Large Scale Blazor Server Web ApplicationsHello there, iam currently working on a <see title> app for something iam not allowed to leak now iaUsing dependency injection for internal classes inside a libraryI'm making a library, where I feel like it would be useful to be able to use Microsoft's dependency System.InvalidOperationException The operation is not allowed on non-connected socketsi have client and server, client creates and connects tcp client to server tcp listener, client tcp