简体   繁体   English

ASP.NET Core服务注册错误

[英]ASP.NET Core services registration error

I have the following class in a project Models: 我在项目模型中有以下课程:

public interface IUserService
{
    IEnumerable<ApplicationUser> GetUsers(string FirstName = null);
    ApplicationUser GetUserById(string Id);
    ApplicationUser GetUserByUsername(string Username);
    void CreateUser(ApplicationUser User);
    void SaveUser();
}

public class UserService : IUserService
{
    private readonly IUserRepository usersRepository;
    private readonly IUnitOfWork unitOfWork;

    public UserService(IUserRepository usersRepository, IUnitOfWork unitOfWork)
    {
        this.usersRepository = usersRepository;
        this.unitOfWork = unitOfWork;
    }

    #region Members

    public void CreateUser(ApplicationUser User)
    {
        throw new NotImplementedException();
    }

    public ApplicationUser GetUserById(string Id)
    {
        var user = usersRepository.GetUserById(Id);
        return user;
    }

    public ApplicationUser GetUserByUsername(string Username)
    {
        var user = usersRepository.GetUserByUsername(Username);
        return user;
    }

    public IEnumerable<ApplicationUser> GetUsers(string FirstName = null)
    {
        if (string.IsNullOrEmpty(FirstName))
        {
            return usersRepository.GetAll();
        } 
        return usersRepository.GetAll()
            .Where(c => c.Profile.FirstName == FirstName);
    }

    public void SaveUser()
    {
        unitOfWork.Commit();
    }

    #endregion
}

On my ASP.NET Core (Full .NET Framework), in Startup.cs i have: 在我的ASP.NET Core(完整的.NET Framework)上,在Startup.cs中,我具有:

// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddScoped<IUserService, UserService>();

I get an error when i run the application: 运行应用程序时出现错误:

InvalidOperationException: Unable to resolve service for type 'Data.Repositories.Identity.IUserRepository' while attempting to activate 'Service.Identity.UserService'.

Like i said upper, this is an ASP.NET Core with Full .NET Framework. 就像我上面说的那样,这是带有完整.NET Framework的ASP.NET Core。 Any thoughts? 有什么想法吗?

You have to register everything in your services collection what you want to be injected in your classes constructors. 您必须在服务集合中注册所有想要注入到类构造函数中的内容。 As far as I can ses: 据我所知:

public UserService(IUserRepository usersRepository, IUnitOfWork unitOfWork)
{
    this.usersRepository = usersRepository;
    this.unitOfWork = unitOfWork;
}

you expect that IUserRepository and IUnitOfWork will be automatically resolved and injected - so they have to be registered to. 您期望IUserRepository和IUnitOfWork将被自动解析和注入-因此必须将其注册到。 If UnitOfWork requires IDbFactory in the constructor to be injected then it also have to be registered in the service collection. 如果UnitOfWork需要在构造函数中注入IDbFactory,则还必须在服务集合中注册它。 Otherwise Dependency Injection framework won't known to to resolve it. 否则,依赖注入框架将无法解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM