I\'ve been at it for days, but I can\'t get Unity to inject anything with RegisterType<>
into my Controller
. I\'m using Web Api 2, in Visual Stud
Unfortunately the exception you are seeing can occur for several reasons. One of them is when Unity
cannot resolve one or more of your injections.
An error occurred when trying to create a controller of type 'FooController'. Make sure that the controller has a parameterless public constructor.
So, based on the information in your question your setup is apparently correct, since IMapper
can be injected. Therefore I guess that UnitOfWork
and RFContext
have dependencies that Unity
cannot resolve. Maybe a repository?
UPDATE:
The problem here is that your RFContext
has several constructors.
https://msdn.microsoft.com/en-us/library/cc440940.aspx#cnstrctinj_multiple
When a target class contains more than one constructor with the same number of parameters, you must apply the InjectionConstructor attribute to the constructor that the Unity container will use to indicate which constructor the container should use. As with automatic constructor injection, you can specify the constructor parameters as a concrete type, or you can specify an interface or base class for which the Unity container contains a registered mapping.
In this case Unity doesn't know how to resolve your RFContext
, and will try to use the constructor with the most parameters. You can solve it by using
container.RegisterType<IRFContext, RFContext>(new InjectionConstructor());