我应该如何在 .NET Core 2.2 中注册通用接口
How should i register generic interfaces in .NET Core 2.2
我正在尝试使用通用存储库和服务创建一个新的 .NET Core API,其中每个类型都有自己的接口和相应的实现。我创建了一个通用存储库和服务结构,但我似乎无法在 startup.cs
中正确注册,或者我做错了其他事情。任何帮助将不胜感激!
我有一个正确注册的通用存储库,名称为:IRepository<T>
在我的控制器中,此存储库按预期工作。
我有一项服务名为:UserService
,它继承自 Service<T, IUserrepository>
Service<T, R>
实现 IService<T, R>
其中 T 是类型,R 是 IRepository<T>
services.AddScoped(typeof(IUserService), typeof(UserService));
产生以下错误:
InvalidCastException: Unable to cast object of type API.Services.UserService to type API.Interfaces.IUserService.
services.AddScoped(typeof(IService<User, IRepository<User>>), typeof(UserService));
产生以下错误:
InvalidOperationException: Unable to resolve service for type API.Interfaces.IUserService while attempting to activate API.Controllers.UserController.
我服务
public interface IService<T, R>
where T : BaseEntity
where R : IRepository<T>
{}
IUserService
public interface IUserService : IService<User, IUserRepository> {}
用户服务
public UserService(
IRepository<User> userRepository,
IOptions<AppSettings> appSettings)
: base(userRepository, appSettings)
{}
基础控制器
public class BaseController<T, Y, Z> : ControllerBase
where T : BaseEntity
where Y : IService<T, Z>
where Z : IRepository<T>
{
public readonly IRepository<T> TypeRepository;
public readonly IService<T, Z> TypeService;
public BaseController(
IRepository<T> injectedRepository,
IService<T, Z> injectedService)
{
TypeRepository = injectedRepository;
TypeService = injectedService as IService<T, Z>;
}
}
Startup.cs
services.AddScoped(typeof(IUserService), typeof(UserService));
我在 Startup.cs
中尝试过的事情
services.AddScoped(typeof(IService<User, IRepository<User>>), typeof(UserService));
services.AddScoped(typeof(IService<User, IRepository<User>>), typeof(Service<User, IRepository<User>>));
services.AddScoped<UserService>();
services.AddScoped(typeof(IUserService), typeof(UserService));
我希望注册是正确的,因为那里没有错误,但是当我 运行 解决方案并执行请求时,我得到了 500 个错误。有人可以指出我做错了什么吗?我显然不明白这里非常重要的事情。提前致谢!
Vidmantas and Silvermind 的评论为我指明了正确的方向。
我删除了 Service
-class 并在 UserService
中实现了 IUserService
在我的 startup.cs 中,这一切都通过使用如下实现注册我的接口来实现:
services.AddScoped(typeof(IUserService), typeof(UserService));
我被蒙蔽了双眼,我试图用一个导致这个错误的解决方案来解决这个问题。感谢各位程序员朋友们的光临!我爱你们所有人!
我正在尝试使用通用存储库和服务创建一个新的 .NET Core API,其中每个类型都有自己的接口和相应的实现。我创建了一个通用存储库和服务结构,但我似乎无法在 startup.cs
中正确注册,或者我做错了其他事情。任何帮助将不胜感激!
我有一个正确注册的通用存储库,名称为:IRepository<T>
在我的控制器中,此存储库按预期工作。
我有一项服务名为:UserService
,它继承自 Service<T, IUserrepository>
Service<T, R>
实现 IService<T, R>
其中 T 是类型,R 是 IRepository<T>
services.AddScoped(typeof(IUserService), typeof(UserService));
产生以下错误:
InvalidCastException: Unable to cast object of type API.Services.UserService to type API.Interfaces.IUserService.
services.AddScoped(typeof(IService<User, IRepository<User>>), typeof(UserService));
产生以下错误:
InvalidOperationException: Unable to resolve service for type API.Interfaces.IUserService while attempting to activate API.Controllers.UserController.
我服务
public interface IService<T, R>
where T : BaseEntity
where R : IRepository<T>
{}
IUserService
public interface IUserService : IService<User, IUserRepository> {}
用户服务
public UserService(
IRepository<User> userRepository,
IOptions<AppSettings> appSettings)
: base(userRepository, appSettings)
{}
基础控制器
public class BaseController<T, Y, Z> : ControllerBase
where T : BaseEntity
where Y : IService<T, Z>
where Z : IRepository<T>
{
public readonly IRepository<T> TypeRepository;
public readonly IService<T, Z> TypeService;
public BaseController(
IRepository<T> injectedRepository,
IService<T, Z> injectedService)
{
TypeRepository = injectedRepository;
TypeService = injectedService as IService<T, Z>;
}
}
Startup.cs
services.AddScoped(typeof(IUserService), typeof(UserService));
我在 Startup.cs
中尝试过的事情 services.AddScoped(typeof(IService<User, IRepository<User>>), typeof(UserService));
services.AddScoped(typeof(IService<User, IRepository<User>>), typeof(Service<User, IRepository<User>>));
services.AddScoped<UserService>();
services.AddScoped(typeof(IUserService), typeof(UserService));
我希望注册是正确的,因为那里没有错误,但是当我 运行 解决方案并执行请求时,我得到了 500 个错误。有人可以指出我做错了什么吗?我显然不明白这里非常重要的事情。提前致谢!
Vidmantas and Silvermind 的评论为我指明了正确的方向。
我删除了 Service
-class 并在 UserService
IUserService
在我的 startup.cs 中,这一切都通过使用如下实现注册我的接口来实现:
services.AddScoped(typeof(IUserService), typeof(UserService));
我被蒙蔽了双眼,我试图用一个导致这个错误的解决方案来解决这个问题。感谢各位程序员朋友们的光临!我爱你们所有人!