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

后端 未结 6 1214
情歌与酒
情歌与酒 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:10

    I think you are missing registering autofac at app start code.

    Use this:

    protected void Application_Start()
    {
        IocConfig.Config();
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    

    For more details, refer this blog http://www.codeproject.com/Articles/808894/IoC-in-ASP-NET-MVC-using-Autofac

    0 讨论(0)
  • 2021-02-07 05:11

    Check this answer.
    It helps me configure correct ContainerBuilder() for WebApi controllers.

    If you are looking here a solution for such error you should check your DependencyResolver Configuration first.

    I faced the same issue and the problem was that I was using Autofac code samples for ContainerBuilder() object for MVC controllers and not API.

    My code for register both type of controllers (MVC and Api):

    var builder = new ContainerBuilder();
    builder.RegisterControllers(Assembly.GetExecutingAssembly()); //Register MVC Controllers
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //Register WebApi Controllers
    builder.RegisterType<Type>().As<IType>();
    
    var container = builder.Build();
    
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //Set the MVC DependencyResolver
    GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container); //Set the WebApi DependencyResolver
    
    0 讨论(0)
  • 2021-02-07 05:14

    Open you ServiceModule File

    Register the Interface and service name those are mentioned in controller.

    Example are as below:-

    builder.RegisterType().As();

    0 讨论(0)
  • 2021-02-07 05:20

    Assembly.GetCallingAssembly() will return the calling assembly, not the assembly where your types is defined.

    Assembly.GetCallingAssembly Method
    Returns the Assembly of the method that invoked the currently executing method.

    In order to make it works, you should use typeof(IocConfig).Assembly or Assembly.GetExecutingAssembly

    0 讨论(0)
  • 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);
    
            }
    
    0 讨论(0)
  • 2021-02-07 05:27

    I encountered this error as well and the root cause was that one of the Controller's dependencies wasn't registered correctly in Autofac.

    The InnerException has the detail (in my case it was an Autofac.Core.DependencyResolutionException) and the ExceptionMessage included the detail of this dependency. Along the lines of:

    "An error occurred during the activation of a particular registration... Cannot resolve parameter 'XXXX'

    0 讨论(0)
提交回复
热议问题