Mój kontroler ASP.Net MVC 4 Web API nie działa z Unity.WebApi. W tym samym projekcie proste kontrolery działają poprawnie z Unity.Mvc3. Ale kiedy uruchomić kontroler sieci Web API pochodzący z ApiController Dostaję wiadomość:Kontroler ASP.Net MVC 4 Web API nie współpracuje z Unity.WebApi
{ "$ id": "1", "Message": "Wystąpił błąd", "ExceptionMessage":” Typ Parametr "ElectricTests.Controllers.Api.DocumentsController" nie ma domyślnego konstruktora "," ExceptionType ":" System.ArgumentException "," StackTrace ":" w System.Linq.Expressions.Expression.New (typ typu) \ r \ n co System.Web.Http.Internal.TypeActivator.Create [TBase] (typ instancetype) \ r \ n co System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator (HttpRequestMessage żądanie sterownik Typ Typ Func`1 & aktywator) \ r \ n co System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create (HttpRequestMessage prośba HttpControllerDescriptor controllerDescriptor, typ controllerType) "}
My ApiController:
public class DocumentsController : ApiController
{
private readonly IDocumentsRepository _repository;
public DocumentsController(IDocumentsRepository repository) {
_repository = repository;
}
public IEnumerable<FormattedDocument> GetFormattedDocuments()
{
return _repository.GetAllFormattedDocuments();
}
...
Bootstrapper.cs:
public static class Bootstrapper {
public static void Initialise() {
IUnityContainer container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer() {
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IDocumentsRepository, DocumentsRepository>();
container.RegisterType<IQuestionsRepository, QuestionsRepository>();
container.RegisterType<ITestRepository, TestsRepository>();
return container;
}
}
gdzie jest mój błąd?
Tekst wyjątku: DocumentsController nie ma domyślnego konstruktora. –
Ten problem rozwiązano w tym artykule: http://www.asp.net/web-api/overview/extensibility/using-twe-web-api-dependency -resolver – Oblomingo