Autofac - Make sure that the controller has a parameterless public constructor

后端 未结 6 1212
情歌与酒
情歌与酒 2021-02-07 04:28

I know it\'s been asked and answered before - the reason I\'m asking is because (I think) I tried all suggested solutions to this problem but still can\'t resolve it.

I

6条回答
  •  无人及你
    2021-02-07 05:22

    You might be missing calling configuration from WebApiConfig.cs file:

    IocConfigurator.ConfigureDependencyInjection(config);

    ConfigureDependencyInjection can be like this:

    public static void ConfigureDependencyInjection(HttpConfiguration config)
            {
    
                var builder = new ContainerBuilder();
    
                // Register your Web API controllers.
                builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
                // OPTIONAL: Register the Autofac filter provider.
                builder.RegisterWebApiFilterProvider(config);
    
                // OPTIONAL: Register the Autofac model binder provider.
                builder.RegisterWebApiModelBinderProvider();
    
                // Set the dependency resolver to be Autofac.
                var container = builder.Build();
                config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    
            }
    

提交回复
热议问题