3

I am using Simple Injector for the first time. Nuget the MVC QuickStart version. In SimpleInjectorInializer.cs this gets called.

public static class SimpleInjectorInitializer
{
 /// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary>
   public static void Initialize()
   {
      // Did you know the container can diagnose your configuration? 
     // Go to: https://simpleinjector.org/diagnostics
     var container = new Container();

     InitializeContainer(container);    
                container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            container.Verify();

      DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
  }

  private static void InitializeContainer(Container container)
  {                 
   // For instance:
   container.Register<IQueryProcessor, QueryProcessor>(Lifestyle.Singleton);
   container.RegisterInitializer<WebApiController>(
                p=> p.QueryProcessor = container.GetInstance<IQueryProcessor>());
   }

WebApiController.cs. Injection doesn't happen. 

public class WebApiController : ApiController
{

 public IQueryProcessor QueryProcessor { get; set; }

 public WebApiController(IQueryProcessor queryProcessor)
 {

   QueryProcessor = queryProcessor;

 }
2
  • There is definitely something more you need to do for Web API. Do you already have code that set up some things for Web API? Commented May 21, 2015 at 17:24
  • The RegisterInitializer<WebApiController> is probably some test code, but it's best to refrain from using property injection completely. Stick with constructor injection. Commented May 21, 2015 at 20:48

1 Answer 1

5

MVC Controllers are NOT the same thing as WebAPI controllers prior to ASP.NET5 (MVC 6) since MVC Controllers implement the IController interface and Web API controllers implement the IHttpController interface.

Simple Injector makes it very easy to override the default IHttpController instantiation, per their documentation.

using System.Web.Http;
using SimpleInjector;
using SimpleInjector.Integration.WebApi;

// This is the Application_Start event from the Global.asax file.
protected void Application_Start() {
    // Create the container as usual.
    var container = new Container();

    // Register your types, for instance using the RegisterWebApiRequest
    // extension from the integration package:
    container.RegisterWebApiRequest<IUserRepository, SqlUserRepository>();

    // This is an extension method from the integration package.
    container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

    container.Verify();

    GlobalConfiguration.Configuration.DependencyResolver =
        new SimpleInjectorWebApiDependencyResolver(container);

    // Here your usual Web API configuration stuff.
}
Sign up to request clarification or add additional context in comments.

3 Comments

I then have "Could not load file or assembly 'System.Web.Http, Version=4.0.0.0" but easily fixed by adding binding redirects. msdn.microsoft.com/en-us/library/2fc472t2.aspx Thanks!
@kay00: The NuGet package manager will usually fix the binding redirects for you automatically, but for some reason this doesn't happen from time to time. Sometimes deinstalling the package and reinstalling is all it takes to get it working again, and in other cases you will have to fix the binding by hand.
SimpleInjector.Integration.WebApi has dependency on Microsoft.AspNet.WebApi.Core but why would it depend on Core if we are using ASP.NET and not ASP.NET Core? Or is WebApi.Core not Core as in ASP.NET Core?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.