2

I'm trying to use Autofac for DI container for an Asp.Net MVCApplication with WebApi2 and OWIN. My global.asax.cs has nothing in the Application_Start(). My startup.cs has the following Configuration:

public void Configuration(IAppBuilder app)
{
    DependencyInjectionConfig.Configure(app);

    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

And DependencyInjectionConfig.Configure is:

public static void Configure(IAppBuilder app)
{
    var config = new HttpConfiguration(); //this means webapi controllers don't use DI container
    //var config = GlobalConfiguration.Configuration; //this makes MVC view controllers fail to do anything


    var builder = new ContainerBuilder();
    var assm = Assembly.GetExecutingAssembly();

    //MVC and WebAPI Controllers
    builder.RegisterControllers(assm);
    builder.RegisterApiControllers(assm);
    WebApiConfig.Register(config);

    //Model Binders
    builder.RegisterModelBinders(assm);
    builder.RegisterModelBinderProvider();

    //Modules
    builder.RegisterModule<AutofacWebTypesModule>();

    //MVC Views
    builder.RegisterSource(new ViewRegistrationSource());

    //MVC and WebAPI Filters

    builder.RegisterFilterProvider();
    builder.RegisterWebApiFilterProvider(config);

    //Finally, Any custom registrations
    RegisterComponents(builder, assm);
    /* My items show in the builder as expected at this point */

    //build the container
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    //Special OWIN bits
    app.UseAutofacMiddleware(container);
    app.UseAutofacMvc();
    app.UseAutofacWebApi(config);
    app.UseWebApi(config);
}

My problem is that Autofac does not seem do any parameter resolution regardless of if I use var config = new HttpConfiguration(); or var config = GlobalConfiguration.Configuration;. bonus, if I use GlobalConfiguration.Configuration MVC controllers for views completely fails.

The ApiController is pretty straight-forward:

public class MessagesController : ApiController
{
     private IMesageHandler MessageHandler {get; set;}
     public MessagesController(IMessageHandler messageHandler)
     {
         this.MessageHandler = messageHandler;
     }
     [HttpGet]
     public string Get()
     {
         return "Hello World";
     }
}

The exception is that it can't find a parameterless constructor (because the Autofac container appears to be napping).

PMontgomery
  • 424
  • 4
  • 17
  • What does the constructor look like, and are the parameter types registered with the container? –  Oct 18 '16 at 20:59
  • Added the ApiController per your comment. Also, the `RegisterComponents(builder, assm);` call is where all my types get registered and they do appear when I QuickWatch the builder. – PMontgomery Oct 18 '16 at 21:15
  • Okay then... huh. I have no idea. Maybe turn on tracing in Owin for additional diagnostic information? –  Oct 18 '16 at 23:10

0 Answers0