gpt4 book ai didi

c# - 没有注册类型 'MyType' 的服务

转载 作者:行者123 更新时间:2023-12-03 19:35:52 25 4
gpt4 key购买 nike

我有一个通用的存储库架构,如下所示:

存储库

public interface IRepository<T> where T: class
{
IList<T> Get(Func<T, bool> where);
}

public abstract class Repository<T> : IRepository<T> where T: class
{
private readonly DbSet<T> _entity;

protected Repository(ApplicationDbContext dbContext)
{
_entity = dbContext.Set<T>();
}

public IList<T> Get(Func<T, bool> where)
{
return _entity.Where(where).ToList();
}
}

然后像这样创建具体的实现:

用户存储库
public class UserRepository : Repository<ApplicationUser>
{
public UserRepository(ApplicationDbContext dbContext) : base(dbContext) {}

// Can add any other methods I want that aren't included in IRepository
}

我可能会有很多服务,所以与其将每个服务单独传递到 Controller 中,我想我会尝试传递一个可以为我生成存储库的工厂。

仓库工厂
public interface IRepositoryFactory
{
T GetRepository<T>() where T : class;
}

public class RepositoryFactory : IRepositoryFactory
{
private readonly IServiceProvider _provider;

public RepositoryFactory(IServiceProvider serviceProvider)
{
_provider = serviceProvider;
}

public T GetRepository<T>() where T : class
{
return _provider.GetRequiredService<T>(); // ERROR: No service for type 'UserRepository' has been registered
}
}

现在,在设置依赖注入(inject)时,我注册了这样的服务:

启动.cs
public void ConfigureServices(IServiceCollection services)
{
// [...]

services.AddScoped<IRepository<ApplicationUser>, UserRepository>();
services.AddScoped<IRepositoryFactory, RepositoryFactory>();

// [...]
}

这都在 Controller 中使用,如下所示:

Controller
public class HomeController : Controller
{
private readonly UserRepository _userRepository;

public HomeController(IRepositoryFactory repositoryFactory)
{
_userRepository = repositoryFactory.GetRepository<UserRepository>(); // ERROR: No service for type 'UserRepository' has been registered
}

// [...]
}

当我调用 _provider.GetRequiredService<T>()repositoryFactory.GetRepository<UserRepository>()方法,我在上面的评论中得到错误。
RepositoryFactory进展顺利,但 UserRepository没有注册。我错过了什么?我试过调用 GetRepository构造函数之外的方法,我尝试更改 AddScoped给另一个 Add变体( transient 和单例),但无济于事。

最佳答案

您正在依赖 UserRepository ,这是实现而不是接口(interface),这不好(假设您实际上已在容器中注册了该接口(interface))。相反,您需要依赖接口(interface):IRepository<ApplicationUser> .所以改变你的工厂界面:

public interface IRepositoryFactory
{
IRepository<T> GetRepository<T>() where T : class;
}

执行:
public IRepository<T> GetRepository<T>() where T : class
{
return _provider.GetRequiredService<IRepository<T>>(); // ERROR: No service for type 'UserRepository' has been registered
}

和 Controller :
public class HomeController : Controller
{
private readonly IRepository<ApplicationUser> _userRepository;

public HomeController(IRepositoryFactory repositoryFactory)
{
_userRepository = repositoryFactory.GetRepository<ApplicationUser>(); // ERROR: No service for type 'UserRepository' has been registered
}

// [...]
}

现在不行了,因为你注册了 IRepository<ApplicationUser>在容器中(实现为 UserRepository ),但 UserRepository您尝试解决的问题未注册(如上所述 - 您无论如何都不应该解决它)。

关于c# - 没有注册类型 'MyType' 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com