Autofac

using Autofac;
using Autofac.Integration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace AZ_BMP_DealerSelection_Web.App_Start
{
public class AutoFacConfig
{
/// <summary>
/// 注册方法
/// </summary>
public static void Register()
{
// 实例化一个autofac的创建容器
var builder = new ContainerBuilder();
// 注册api容器需要使用HTTPConfiguration对象
HttpConfiguration config = GlobalConfiguration.Configuration;
SetupResolveRules(builder);
//注册所有的ApiControllers
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
// 创建一个autofac的容器
var container = builder.Build();
// api的控制器对象由autofac来创建
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}

/// <summary>
/// 设置配置规则
/// </summary>
/// <param name="builder"></param>
public static void SetupResolveRules(ContainerBuilder builder)
{
// 告诉autofac框架,将来要创建的控制器类存放在哪个程序集
Assembly controllerAssmbly = Assembly.Load("AZ_BMP_DealerSelection_Web");
builder.RegisterApiControllers(controllerAssmbly);
//属性注入
builder.RegisterAssemblyTypes(Assembly.Load("AZ")).PropertiesAutowired();
}
}
}

posted @ 2021-06-08 14:29  萌橙  阅读(168)  评论(0编辑  收藏  举报