I am trying to execute a basic DI constructor using Simple Injector, and it seems that it cannot resolve dependencies for Web API controllers.
- I have an API controller in the API folder, which is located outside the Controllers folder.
- I also tried placing it in the Controllers folder, but that doesn't seem to matter much. The stack trace that I get is similar to the one presented in this question .
- I am using a new installation of the NuGet package with Simple Injector MVC Integration Quick Start (version 2.1.0).
- I have a
SimpleInjectorWebApiDependencyResolver database from the documentation, which also matches the one found here . - I am using the Entity Framework and looked at a discussion thread about changes to properly load context.
This does not seem to be a problem, but I still get the following error:
The type "MyProject.API.ArticleController" does not have a default constructor
System.ArgumentException in
System.Linq.Expressions.Expression.New (Type Type) in System.Web.Http.Internal.TypeActivator.Create [TBase] (InstanceType Type) in System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator (HttpRequest Request , Func`1 & activator) under System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, type ControllerType)
It would be nice if someone could offer me some suggestions as to whether something needs to be changed from their current state / call order.
ArticleController (basic structure):
public class ArticleController : ApiController { private readonly IArticleRepository articleRepository; private readonly IUserRepository userRepository; private readonly IReleaseRepository releaseRepository; public ArticleController(IArticleRepository articleRepository, IUserRepository userRepository, IReleaseRepository releaseRepository) { this.articleRepository = articleRepository; this.userRepository = userRepository; this.releaseRepository = releaseRepository; }
SimpleInjectorInitializer:
public static class SimpleInjectorInitializer { public static void Initialize() { var container = new Container(); InitializeContainer(container); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); container.RegisterMvcAttributeFilterProvider(); container.Verify(); DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); } private static void InitializeContainer(Container container) { container.Register<IArticleRepository, ArticleRepository>(); container.Register<IUserRepository, UserRepository>(); container.Register<IReleaseRepository, ReleaseRepository>(); } }
Global.asax.cs:
public class WebApiApplication : System.Web.HttpApplication { private void ConfigureApi() {
c # dependency-injection asp.net-web-api simple-injector
user1417835 Apr 09 '13 at 17:00 2013-04-09 17:00
source share