My WebApi service works when using the runtime Unity configuration, but when I changed it to use the unity configuration from the web.config I get the error
"An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor."
I'm using Unity and Unity.WebApi nuget packages.
When using the runtime configuration (which works fine) my RegisterComponents
method looks like this;
public static void RegisterComponents()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<Repository.Interfaces.IRepository, DataAccess.OracleRepository>(new HierarchicalLifetimeManager());
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
When using the xml configuration in the web.config I use this;
public static void RegisterComponents()
{
IUnityContainer container = new UnityContainer().LoadConfiguration();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
and my unity section in the web.config looks like this;
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IRepository" type="Repository.Interfaces.IRepository, Repository"/>
<alias alias="OracleRepository" type="DataAccess.OracleRepository, DataAccess"/>
<container>
<register type="IRepository" mapTo="OracleRepository" name="oracle">
<lifetime type="hierarchical"/>
</register>
</container>
</unity>
However, at this point when i go to the url the application gives the error;
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>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()</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>Resolution of the dependency failed, type = "GatewayService.Controllers.MyController", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Repository.Interfaces.IRepository, is an interface and cannot be constructed. Are you missing a type mapping? ----------------------------------------------- At the time of the exception, the container was: Resolving GatewayService.Controllers.MyController,(none) Resolving parameter "respository" of constructor GatewayService.Controllers.MyController(Repository.Interfaces.IRepository respository) Resolving Repository.Interfaces.IRepository,(none)</ExceptionMessage>
<ExceptionType>Microsoft.Practices.Unity.ResolutionFailedException</ExceptionType>
...