C
C#•2y ago
joy

dependency injection in net framework

hi all, i understand that in net core we can add dependency injection like this
IConfiguration Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient();
builder.Services.AddScoped<Note>();
IConfiguration Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient();
builder.Services.AddScoped<Note>();
but this way doesnt seem to work in net framework, can anyone help advise how can we do this in net framework?
27 Replies
Henkypenky
Henkypenky•2y ago
im pretty sure you need to add the nuget: Microsoft.Extensions.DependencyInjection for .net framework
joy
joy•2y ago
i installed the package, but i got this message esp on this line
var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(args);
Error CS0103 The name 'WebApplication' does not exist in the current context
Error CS0103 The name 'WebApplication' does not exist in the current context
do you know why?
Saber
Saber•2y ago
because that stuff doesn't exist in .net framework?
joy
joy•2y ago
i actually following the way that net core registers their services in a web application.. how can we do this in net framework..? or there's no way we can register our services in net framework..?
Saber
Saber•2y ago
you can, you just need to do it in a valid way for .net framework so referencing anything in .net core makes 0 sense because its more than likely not applicable. Instead look up examples on how to do it in .net framework
Henkypenky
Henkypenky•2y ago
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api maybe this can help you it's something along the lines of
config.Services.Add(...........);
config.Services.Add(...........);
joy
joy•2y ago
sry, i saw that to add the
config.Services.Add
config.Services.Add
they added into this method, but in the web application doesnt have this Startup class.. do we create it manually?
Henkypenky
Henkypenky•2y ago
sorry friend I have literally 0 experience with .net fw any reason you are using it? maybe consider going straight to .net 6 if you can
joy
joy•2y ago
😦 yup.. it's a requirement to use net framework..
Henkypenky
Henkypenky•2y ago
keep on pushing then, there probably are people here that still know that ancient framework, moreover know the asp part of it which was even more rare back then
joy
joy•2y ago
okay...
joy
joy•2y ago
i come across an article about dependency injection on net framework mvc https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-dependency-injection not sure which Bootstrapper.cs class they refer..
ASP.NET MVC 4 Dependency Injection
Note: This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC and ASP.NET MVC 4 filters. If you have not used ASP.NET MVC 4 filters before, we rec...
joy
joy•2y ago
but i think they're using unity.. anyone please help advise? 😢
Saber
Saber•2y ago
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...
joy
joy•2y ago
hi @IEntityTypeConfiguration<Saber> thank you for the reference, i tried to implement it and some of the error message disappear, i havent build the project because net framework can't seem to read the appsettings.json config file (usually net framework use app/web.config). i research on few websites that read json configuration file using net framework, the solution provides to install microsoft.extensions.* packages, i have done that but doesnt seem to work, do you have any idea why is that so?
Saber
Saber•2y ago
add the correct nugets and usings not much more beyond that you can do
joy
joy•2y ago
i double checked.. it's the correct package when i hover to the IConfigurationRoot..
Saber
Saber•2y ago
did you add the Microsoft.Extensions.Configuration.Json nuget
joy
joy•2y ago
yup.. i double checked the "Installed" part of all packages, it's installed..
joy
joy•2y ago
joy
joy•2y ago
i checked the net core version, the .AddJsonFile should extend from IConfigurationBuilder, but the error message on net framework extend from ConfigurationBuilder...
joy
joy•2y ago
is it because it only supports net framework 4.6.1 version? (currently im using net 4.8 version)
joy
joy•2y ago
still not working..
joy
joy•2y ago
ive tried several tutorials and still the same.. is it safe to say that it's impossible to read appsettings json using net framework? for example : https://benfoster.io/blog/net-core-configuration-legacy-projects
Saber
Saber•2y ago
dunno just spent 3 seconds to make a net framework 4.8 project and it works perfectly fine for me, so its clearly on you
joy
joy•2y ago
wait.. i think it works in console app but not in the web application... yes.. i retry to create a console app and it works.. wait.. it was on me yes.. i dont know why after i recreate a new project now it's working... omg...
joy
joy•2y ago
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(typeof(AppRepository<Note>));
services.AddScoped(typeof(AppRepository<Note>));
... do you have any idea maybe what could be the issue..?