类库没有Program.cs-在哪里配置DI



我开始了一个新的类库项目,我想对服务和DbContext等使用依赖注入。

但是,没有Program.cs文件。

我应该在哪里配置DI接口和类?我需要添加一个空的Program.cs吗?

例如,如果您有一个类库作为业务访问层。您可以在根目录中添加一个名为DependencyInjection的类作为以下

public static class DependencyInjection
{
public static void AddApplication(this IServiceCollection service)
{
service.AddScoped<IOrgSettingsService, OrgSettingsService>();
service.AddScoped<IIdentity, IdentityService>();
service.AddScoped<ILdapAuthenticationService, LdapAuthenticationService>();
service.AddScoped<IOrgAuthenticationService, OrgAuthenticationService>();
service.AddScoped<IVacanciesService, VacancyService>();

// assemblers
service.AddScoped<IVacancyAssembler, VacancyAssembler>();
}
}

并在启动类中将其注册为以下

public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddApplication();
}

用于整个应用程序的依赖项注入。主应用程序向所有程序集注入服务。例如,dbcontext从主应用程序注入到程序集。您不应该为每个程序集单独和本地定义依赖实例

最新更新