c# - ASP.NET Core,依赖注入 - 无法解析服务
问题描述
我目前正在关注如何将依赖注入到 ASP.NET Core 中的控制器中的教程,并且我不断收到以下错误:
InvalidOperationException:尝试激活“UserManagement.UserTransactions”时无法解析“Infrastructure.Interfaces.IUserRepository”类型的服务。
这是我到目前为止实现的代码:
启动
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityWithMongoStores(Configuration.GetConnectionString("DefaultConnectionMongoDB"))
.AddDefaultTokenProviders();
services.AddScoped<IUserTransactions, UserTransactions>();
services.AddMvc();
}
用户事务
public class UserTransactions : IUserTransactions
{
public IUserRepository _repository;
public UserTransactions(IUserRepository repository)
{
_repository = repository;
}
public void CreateUser(RegisterModel registerModel)
{
//logic to create user here
}
}
IUserTransactions
public interface IUserTransactions
{
void CreateUser(RegisterModel registerModel);
}
用户控制器
public class UserController : Controller
{
private readonly IUserTransactions _userTransactions;
public UserController(IUserTransactions userTransactions)
{
_userTransactions = userTransactions;
}
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CreateUser(RegisterModel registerModel)
{
_userTransactions.CreateUser(registerModel);
return View("Index");
}
}
我目前正在使用 .NET Core 2.0。
最后,我知道这个问题与此处发布的其他问题相似,但他们没有提供我需要的答案。