InvalidOperationException:尝试激活UserSevice时无法解析类型IRepository的服务

时间:2018-01-29 07:33:48

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

我创建了一个.NET Core 2.0 MVC应用程序,并使用依赖注入和存储库模式将存储库注入我的控制器。但是,我 我的UserService要在UserController中注入时出错:

  

InvalidOperationException:尝试激活'FFLEX.Services.User.UserService'时,无法解析类型'FFLEX.Services.Repository.IGenericRepository`1 [FFLEX.Core.ApplicationUser]'的服务。

GenericRepository

public class GenericRepository<T> : IGenericRepository<T> where T : class {
    protected ApplicationDbContext _context;

    public GenericRepository(ApplicationDbContext context) {
        _context = context;
    }

    public IQueryable<T> GetAll() {
        return _context.Set<T>();
    }

    public virtual async Task<ICollection<T>> GetAllAsyn() {
        return await _context.Set<T>().ToListAsync();
    }
}

IUserService

public interface IUserService {
    Task<ICollection<ApplicationUser>> GetAllNonSuperAdminUsers();
}

UserService

public class UserService : IUserService {
    protected readonly IGenericRepository<ApplicationUser> _repository;

    public UserService(IGenericRepository<ApplicationUser> repository) {
        repository = repository;
   }

    public async Task<ICollection<ApplicationUser>> GetAllNonSuperAdminUsers() {
        return await _repository.GetAllAsyn();
    }
}

UserController

public class UserController : Controller {
    private readonly IUserService _userService;

    public UserController(IUserService userService) {
        userService = userService;
    }

    public async Task<IActionResult> Index() {
        var test = await PrepareUserViewModel();
        return View(test);
    }

    public async Task<UserViewModel> PrepareUserViewModel() {
        UserViewModel vm = new UserViewModel();
        vm.UserCount = 1;
        vm.Subscribers = 1;
        vm.Guest = 1;
        vm.Users = await _userService.GetAllNonSuperAdminUsers();
        return vm;
    }
}

Startup.cs配置

public void ConfigureServices(IServiceCollection services) {
    services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(FFLEXConsts.ConnectionString));

    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

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

    services.AddMvc();
}

我不确定我做错了什么。请帮助我,因为它似乎没有任何问题。

1 个答案:

答案 0 :(得分:0)

我对Entity框架工作核心数据库第一种方法有同样的问题。我已通过以下步骤解决了问题。

我们在实体框架工作核心数据库第一种方法中遇到此错误。我按照以下步骤操作,错误得到解决enter code here

    Step1: Check Your context class constructor enter code here`r should be like this

      public partial class ZPHSContext : DbContext
        {
            public ZPHSContext(DbContextOptions<ZPHSContext> dbContextOptions):base(dbContextOptions)`enter code here`
            {

            }

        }

    Step2: In Startup file register your context class
      public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
                services.AddDbContext<ZPHSContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));

            }

    Step3: Connection string in appsettings

    "ConnectionStrings": {
        "BloggingDatabase": "Server=Server=****;Database=ZPHSS;Trusted_Connection=True;"
      }
Step4: Remove default code in OnConfiguring method in context class

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        }