【问题标题】:InvalidOperationException: Unable to resolve service for type IRepository while attempting to activate UserSeviceInvalidOperationException:尝试激活 UserService 时无法解析 IRepository 类型的服务
【发布时间】:2018-07-07 20:44:02
【问题描述】:

我创建了一个 .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();
}

用户服务

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

用户控制器

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

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

【问题讨论】:

  • 我没有看到您注册IGenericRepository&lt;ApplicationUser&gt;,从错误看来注射器无法解决它。你有什么理由不在ConfigureServices 方法中注册它?
  • 您的ConfigureServices 方法中缺少services.AddScoped&lt;IGenericRepository&lt;ApplicationUser&gt;, GenericRepository&lt;ApplicationUser&gt;&gt;();
  • @CalC 非常感谢它的工作:)

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


【解决方案1】:

我遇到了同样的问题。我只是忘了将services.AddScoped&lt;Internface,class&gt; 添加到我的startup.cs 课程中。

【讨论】:

    【解决方案2】:

    我在 Entity Framework Core 数据库第一种方法中遇到了同样的问题。我按照以下步骤解决了错误。

    第 1 步:检查您的上下文类构造函数是否如下所示

    public partial class ZPHSContext : DbContext
    {
        public ZPHSContext(DbContextOptions<ZPHSContext> dbContextOptions) : base(dbContextOptions)
        {
        }
    }
    

    第 2 步:在 Startup.cs 文件中注册您的上下文类

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<ZPHSContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
    }
    

    第三步:在appsettings.json中添加连接字符串

    "ConnectionStrings": {
        "BloggingDatabase": "Server=Server=****;Database=ZPHSS;Trusted_Connection=True;"
    }
    

    第四步:从上下文类中的OnConfiguring方法中移除默认代码

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-08
      • 2021-04-29
      • 2020-07-01
      • 2021-02-23
      • 2020-05-18
      • 2018-06-08
      • 2019-08-21
      • 2020-08-21
      • 1970-01-01
      相关资源
      最近更新 更多