使用WebApi的AutoFac,仍在寻找参数较少的构造函数

时间:2015-04-29 10:31:37

标签: asp.net-web-api autofac

我在WebApi项目中有以下类,并且从Global.ASAX类调用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));


    }

1 个答案:

答案 0 :(得分:3)

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

您需要移动电话

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);