Using Dependency Injection with .NET Core Class Library (.NET Standard)

前端 未结 1 439
面向向阳花
面向向阳花 2020-12-28 15:23

I have gone through the link:

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection

and learnt that how I can use dependency inj

相关标签:
1条回答
  • 2020-12-28 16:09

    You don't have to do anything in your class library. Only the main application has a composition root (earliest point in an application lifecycle you can set up your object graph).

    This happens in Startup.cs in your ASP.NET Core application. There you also register your dependencies:

    services.AddScoped<IUserManager,UserManager>();
    

    That's it. Class libraries don't have a composition root. Neither they should, because you can't use them without an application and the IoC Container used is a choice of the application not of the library.

    You can however provider convenience methods to do this registrations, like the AddXxx method common in ASP.NET Core or some kind of module system, like in 3rd party IoC containern like Autofac or Castle Windsor.

    0 讨论(0)
提交回复
热议问题