I have a generic repository interface,
IRepositroy<T>
an abstract base class:
public abstract class RepositoryBase
{
protected readonly IDbContextFactory _dbContextFactory;
protected readonly IUnitOfWork _unitOfWork;
protected RepositoryBase(IDbContextFactory dbContextFactory, IUnitOfWork uow)
{
this._dbContextFactory = dbContextFactory;
_unitOfWork = uow;
}
}
I then have concrete repository classes implementing those interfaces
public class CarRepo : RepositoryBase, IRepository<Car> ...
I am trying to inject these into my WebApi Controllers with unity, but am unsuccessful.
I get an error saying I do not have a paramterless ctor, and that it is unable to cast CarRepo to type IRepository.
In My container config, I have:
container.RegisterType(typeof(IRepository<>), typeof(CarRepo));
Below is what I get when tying to navigate to that controller method:
{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'CarController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Resolution of the dependency failed, type = \"CarController\", name = \"(none)\".\r\nException occurred while: Resolving parameter \"repository\" of constructor CareController(IRepository1[[CarRepository, DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] repository).\r\nException is: InvalidCastException - Unable to cast object of type 'CarRepository' to type 'IRepository
1[Repository]'.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving CarController,(none)\r\n Resolving parameter \"repository\" of constructor CarController(IRepository1[[CarRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] repository)\r\n","ExceptionType":"Microsoft.Practices.Unity.ResolutionFailedException","StackTrace":" at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable
1 resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)\r\n at Unity.WebApi.UnityDependencyScope.GetService(Type serviceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'CarRepository' to type 'IRepository
1[CarRepository]'.","ExceptionType":"System.InvalidCastException","StackTrace":" at lambda_method(Closure , IBuilderContext )\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)"}}}