今天在这里分享一哈Autofac mvc 依赖注入单个注入的代码
/// <summary> /// 初始化容器 /// </summary> public static void InitAutofc() { ContainerBuilder builder = new ContainerBuilder(); //调用需要注册的类 SetupResolveRules(builder); //注册控制器 builder.RegisterControllers(Assembly.GetExecutingAssembly()); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); AreaRegistration.RegisterAllAreas(); } /// <summary> /// 需要用到的类进行注册 /// </summary> /// <param name="builder">容器</param> private static void SetupResolveRules(ContainerBuilder builder) { //用户信息 builder.RegisterType<TUSERINFODao>().As<ITUSERINFODao>(); builder.RegisterType<TUSERINFOService>().As<ITUSERINFOService>(); }