带有 Web Api 的 AutoFac,仍在寻找无参数构造函数

AutoFac with WebApi, Still looking for parameter less constructor

我在我的 WebApi 项目中有以下 class 并且从 Global.ASAX class 调用了 RegisterContainer() 方法。我正在我的 api 控制器中注入服务。问题是它抛出异常并且没有得到实例化更正。它说没有找到无参数控制器。知道我的代码有什么问题吗

public class IocConfig
{
    #region Public Methods and Operators

    /// <summary>
    ///     Adds dependencies into the container
    /// </summary>
    /// <returns></returns>
    public static void RegisterContainer()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        RegisterModules(builder);
        RegisterControllers(builder);
    }

    #endregion

    #region Methods

    private static void RegisterControllers(ContainerBuilder builder)
    {
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    }

    private static void RegisterDependencyResolver(IContainer container, ContainerBuilder builder)
    {
        IDependencyResolver resolver = new AutofacWebApiDependencyResolver(container);

        DependencyResolver.SetResolver(resolver);
    }

    private static void RegisterModules(ContainerBuilder builder)
    {

        builder.RegisterModule(new DomainModule());
        builder.RegisterModule(new RepositoryModule());
        builder.RegisterModule(new ServiceFacadeModule());
        builder.RegisterModule(new MappingModule());

    }

    #endregion
}

}

更新:

我已经更改了它,只在 Global.Asax 中添加了以下几行。

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        //IocConfig.RegisterContainer();
        //WebApiConfig.Register(GlobalConfiguration.Configuration);

        var builder = new Autofac.ContainerBuilder();
        builder.RegisterControllers(typeof(WebApiApplication).Assembly).PropertiesAutowired();
        builder.RegisterModule(new RepositoryModule());
        builder.RegisterModule(new ServiceFacadeModule());
        builder.RegisterModule(new RepositoryModule());
        builder.RegisterModule(new MappingModule());
        builder.RegisterModule(new DomainModule());
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));


    }

您正在尝试在构建容器后注册您的模块。

您需要转移电话

RegisterModules(builder);

之前用builder.Build()创建容器:

RegisterModules(builder);
IContainer container = builder.Build();

在您更新的代码中,您现在缺少两件事:

  • 正在使用 RegisterApiControllers
  • 注册您的 Api 控制器
  • 设置 AutofacWebApiDependencyResolver

因此您的代码应如下所示:

var builder = new Autofac.ContainerBuilder();
builder.RegisterControllers(typeof(WebApiApplication).Assembly).PropertiesAutowired();
builder.RegisterApiControllers(typeof(WebApiApplication).Assembly).PropertiesAutowired();
builder.RegisterModule(new RepositoryModule());
builder.RegisterModule(new ServiceFacadeModule());
builder.RegisterModule(new RepositoryModule());
builder.RegisterModule(new MappingModule());
builder.RegisterModule(new DomainModule());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

您似乎正在注册 MVC。

根据 Autofac WebAPI 集成 documentation,您必须在启动时执行此操作:

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();

var config = GlobalConfiguration.Configuration;
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

请记住,您需要安装 WebAPI 集成包:

Install-Package Autofac.WebApi