【发布时间】:2018-09-17 14:37:04
【问题描述】:
我正在 Core 2.0 上创建一个项目,但从过去 2 天开始,我一直在依赖注入。以下是我的代码-
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<IAccountBAL, AccountBAL>();
}
public interface IAccountBAL
{
Task CreateSuperAdmin();
}
public class AccountBAL:IAccountBAL
{
private ApplicationDbContext _connection;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
public AccountBAL(ApplicationDbContext connection, RoleManager<IdentityRole> roleManager, SignInManager<ApplicationUser> signInManager, UserManager<ApplicationUser> userManager)
{
_connection = connection;
_roleManager = roleManager;
_signInManager = signInManager;
_userManager = userManager;
}
public async Task CreateSuperAdmin()
{
//My Code
}
}
但是当我运行我的项目时,我收到错误在尝试激活“UserAuth.BAL.AccountBAL”时无法解析类型“DALUser.Data.ApplicationDbContext”的服务。 但是,当我从以下代码中删除 IAccountBAL _iAccountBAL 时,它可以工作。但是我需要传递这种依赖关系。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAccountBAL _iAccountBAL)
{
}
【问题讨论】:
-
您的
ConfigureServices缺少很多内容。您没有注册ApplicationDbContext(这是您的实际错误),但您也缺少UserManager、RoleManager和SignInManager的注册。 -
@KirkLarkin 你节省了我的休息时间。谢谢你。缺少 UserManager、RoleManager 和 SignInManager 的注册
标签: c# asp.net-core asp.net-core-2.0