ASP.NET WebAPI 中的 Autofac 模块注册

Autofac module registration in ASP.NET WebAPI

到目前为止,我已经在我的 MVC 项目中的 class 中完成了类型注册,但我现在正尝试使用模块来完成。我的项目结构如下

Project.Data:包含我的 entityframework classes 和 DataContext。我重构了我的 datacontect 以实现一个接口 (IDbContext),我通过 autofac 模块注册了该接口 Project.Business:包含注入 IDbContext 实例的业务逻辑 classes。 类 实现相应的接口,这些接口也通过 autofac 模块注册 Project.Web: asp.net 使用汇编扫描注册所有 autofac 模块的项目

示例代码

Project.Data

//IDbContext
public  interface IDbContext
{
    IDbSet<MyData> MyDatas { get; set; }
    ....
}

//DataContext
public class DataContext : DbContext, IDbContext
{
    public IDbSet<MyData> MyDatas { get; set; }
}

//Module
public class DataInjectionModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<DataContext>().As<IDbContext>();
    }
}

Project.Business

public interface IServeDataService
{
    IEnumerable<object> GetData();
}
public class ServeDataService : IServeDataService
{
    private IDbContext context;
    public ServeDataService()
    {
        this.context = context;
    }
    public IEnumerable<object> GetData()
    {
    }
}

public class BusinessInjectionModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ServeDataService>().As<IServeDataService>();
    }
}

Project.Web

public partial class Startup
{
     public void RegisterContainer()
     {
         var builder = new ContainerBuilder();

         var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();
         builder.RegisterAssemblyModules(assemblies.ToArray());
         builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

         builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

     }
}

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        RegisterContainer();
    }
}

//controller
public class DataController : ApiController
{
    private IServeDataService dataService;
    public DataController(IServeDataService dataService)
    {
        this.dataService = dataService;
    }

    public async Task<IHttpActionResult> Get()
    {
        var data = dataService.GetData();
        return Ok(data);
    }
}

然而,当我尝试调用 api 服务时,出现以下错误

An error occurred when trying to create a controller of type 'DataController'. Make sure that the controller has a parameterless public constructor.

我已经调试并在 RegisterContainer 方法中设置了断点并调用了该方法,我还查看了列出的程序集并列出了我的业务和数据程序集,但我的模块注册从来没有 运行。我做错了什么?

您似乎没有配置 Web API 来使用您的 Autofac 容器。

To get Autofac integrated with Web API you need to reference the Web API integration NuGet package, register your controllers, and set the dependency resolver. You can optionally enable other features as well.
> Autofac - Web API documentation.

为了使其正常工作,您必须在 Autofac ASP.NET Web API Integration nuget 包中添加一个引用,并在您的 RegisterContainer 方法中添加以下行:

// Set the dependency resolver to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);