ASP.NET Core,依赖注入-无法解析服务

时间:2018-10-19 21:44:47

标签: c# asp.net-core dependency-injection

我目前正在按照Dependency injection into controllers in 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();  
    }

UserTransactions

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);        
}

UserController

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。

最后,我知道这个问题与此处发布的其他问题类似,但是他们没有提供我需要的答案。

0 个答案:

没有答案