1
votes

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>
        ...
1
remove the name attribute form the register tag.Nkosi

1 Answers

0
votes

Quoting the official documentation,

The <register> Element

The <register> element is the basic building block for any configuration file. It enables you to specify type mappings and injection configuration for a type. If you specify a name, that name is used for the type mapping. If you do not specify a name, it creates a default mapping for the specified types. You can specify a lifetime manager for each mapping. If no explicit lifetime manager is configured for a type, it uses the transient lifetime manager.

So I suggest removing the name attribute from the register tag in the web config.