DryIOC: Registering a generic type throws: Implementation type is not specified when using automatic constructor selection

1.5k views Asked by At

I want to register a generic base repository that accepts ,entity and entity key, I have the IBaseRepo and it's implementation. I used this line of code below

container.Register(typeof(IRepository<,>), typeof(RepositoryBase<,>),
    made: FactoryMethod.ConstructorWithResolvableArgumentsIncludingNonPublic);

but i get below error

InvalidOperationException: An error occurred when trying to create a controller of type 'ValuesController'. Make sure that the controller has a parameterless public constructor.

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",

Inner exception:

DryIoc.ContainerException: Implementation type is not specified when using automatic constructor selection: IRepository {ReturnDefault} as parameter "user"

in UserRepository: IUserRepository {ReturnDefault} as parameter "repository"

in scoped ValuesController {ServiceKey=DefaultKey(0), ReturnDefault}

at DryIoc.Throw.ThrowIfNull[T](T arg, Int32 error, Object arg0, Object arg1, Object arg2, Object arg3) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxx\\xxx\\DryIoc\\Container.cs:line 11562
at DryIoc.FactoryMethod.<>c__DisplayClass5_0.<Constructor>b__0(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 4426
at DryIoc.ReflectionFactory.ClosedGenericFactoryGenerator.GetGeneratedFactory(Request request, Boolean ifErrorReturnDefault) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 8866
at DryIoc.Container.DryIoc.IContainer.ResolveFactory(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 852
at DryIoc.ReflectionFactory.CreateExpressionOrDefault(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 8713
at DryIoc.Factory.GetExpressionOrDefault(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 8038
at DryIoc.ReflectionFactory.CreateExpressionOrDefault(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 8714
at DryIoc.Factory.GetExpressionOrDefault(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 8038
at DryIoc.Factory.GetDelegateOrDefault(Request request) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 8162
at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, Object serviceKey, Boolean ifUnresolvedReturnDefault, Type requiredServiceType, RequestInfo preResolveParent, IScope scope) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 524
at DryIoc.Container.ResolveAndCacheDefaultDelegate(Type serviceType, Boolean ifUnresolvedReturnDefault) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 563
at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, Boolean ifUnresolvedReturnDefault) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\Container.cs:line 477
at DryIoc.WebApi.DryIocDependencyScope.GetService(Type serviceType) in C:\\Users\\Workload\\documents\\visual studio 2017\\Projects\\xxxxxx\\xxxxxx\\DryIoc\\WebApi\\DryIocWebApi.cs:line 216
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
1

There are 1 answers

0
dadhi On BEST ANSWER

The RepositoryBase<> is abstract class and therefore can not be used for implementing the service. IoC is not a magic (at least DryIoc) and cannot create abstract class for you.

You need to provide non-abstract implementation type in order for this code to work.