Я внимательно следил за очень полезным ответом here, чтобы организовать мою единицу работы и хранилища динамически с помощью SimpleInjector DI.Зарегистрировать общие типы в SimpleInjector Version 3
Используя сервис тест ниже:
public class TestService
{
public TestService(IRepository<Call> calls){}
}
В контроллере:
public class TestingController : Controller
{
private readonly IUnitOfWork _unitOfWork ;
public TestingController(IUnitOfWork unitOfWork, TestService testService)
{
_unitOfWork = unitOfWork;
}
}
И загрузчик:
public static class BootStrapper
{
public static void ConfigureWeb(Container container)
{
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Options.ConstructorResolutionBehavior = new GreediestConstructorBehavior();
container.Register<DbContext, OCISContext>(Lifestyle.Scoped);
container.Register<ApplicationUserManager>(Lifestyle.Scoped);
container.Register<ApplicationSignInManager>(Lifestyle.Scoped);
container.Register<IAuthenticationManager>(() =>
AdvancedExtensions.IsVerifying(container)
? new OwinContext(new Dictionary<string, object>()).Authentication
: HttpContext.Current.GetOwinContext().Authentication, Lifestyle.Scoped);
container.Register<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(Lifestyle.Scoped);
container.Register<IUnitOfWork, UnitOfWork.UnitOfWork>(Lifestyle.Scoped);
container.RegisterCollection(typeof(IRepository<>), typeof(IRepository<>).Assembly);
container.Register<TestService>(Lifestyle.Scoped);
}
Я получаю ошибку:
An exception of type 'System.InvalidOperationException' occurred in SimpleInjector.dll but was not handled in user code
Additional information: The configuration is invalid. Creating the instance for type TestService failed. The constructor of type TestService contains the parameter with name 'calls' and type
IRepository<Call>
that is not registered. Please ensureIRepository<Call>
is registered, or change the constructor of TestService. There is, however, a registration forIEnumerable<IRepository<Call>>
; Did you mean to depend onIEnumerable<IRepository<Call>>
?
Я также попытался
container.RegisterCollection<IRepository>(new [] {typeof(IRepository)});
container.RegisterCollection(typeof(IRepository), new[] {typeof(IRepository)});
Мое намерение состоит в том, чтобы получить экземпляр GenericRepository как это implents IRepository, как показано в ответ на ссылку выше.
Обновите свой код и добавьте соответствующий код, который показывает, почему вы регистрируете репозитории в UnitOfWork. – Steven