Служба MyAppApp работает, когда используется конфигурация Unity для выполнения, но когда я изменил ее, чтобы использовать конфигурацию единства из web.config, я получаю ошибкуОшибка конструктора конструктора без параметров с помощью Unity.WebApi и конфигурации единства в web.config
«при попытке создать контроллер типа „MyController“произошла ошибка. Убедитесь, что контроллер имеет без параметров открытого конструктора.»
Я использую Unity и Unity.WebApi nuget пакеты.
При использовании конфигурации времени выполнения (которая отлично работает) мой метод RegisterComponents
выглядит следующим образом;
public static void RegisterComponents()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<Repository.Interfaces.IRepository, DataAccess.OracleRepository>(new HierarchicalLifetimeManager());
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
При использовании конфигурации xml в web.config я использую это;
public static void RegisterComponents()
{
IUnityContainer container = new UnityContainer().LoadConfiguration();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
и мой раздел единства в web.config выглядит следующим образом;
<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>
Однако в этот момент, когда я перехожу к URL-адресу, приложение выдает ошибку;
<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>
...
удалить атрибут name из тега register. – Nkosi