网站内容设计上的特色,网站维护能自己做吗,电商网站建设属于研发费用吗,网站seo在线诊断对于这个容器注入#xff0c;个人也不是很熟悉#xff0c;很多还不懂#xff0c;只会基本的操作#xff0c;几天把它记录下来#xff0c;说不定以后帮助就大了呢#xff0c;这方面跟安卓差距还是挺大的 下面记录下应用的流程 步骤#xff1a; 1.添加应用 2.重写工厂个人也不是很熟悉很多还不懂只会基本的操作几天把它记录下来说不定以后帮助就大了呢这方面跟安卓差距还是挺大的 下面记录下应用的流程 步骤 1.添加应用 2.重写工厂这里讲的是常用的构造方法注入方式 1 public class CreateAutofacFactory : DefaultControllerFactory2 {3 public override IController CreateController(RequestContext requestContext, string controllerName)4 {5 //获取容器6 IContainer ic CreateControllers.CreateController().Icontainer;7 //获取控制器类型8 Type t this.GetControllerType(requestContext, controllerName);9 //从容器中解析出对象
10 return ic.Resolve(t) as IController;
11 }
12 } View Code 1 public class CreateControllers2 {3 private CreateControllers() { }4 private static CreateControllers createController;5 public static CreateControllers CreateController()6 {7 if (createController null)8 createController new CreateControllers();9 return createController;
10 }
11 private IContainer iContainer { get; set; }
12 public IContainer Icontainer
13 {
14 get
15 {
16 if (iContainer null)
17 {
18 ContainerBuilder cb new ContainerBuilder();
19 //用代码注册类型
20 //cb.RegisterTypeStudentDao().AsIStudentDao();
21 //cb.RegisterTypeStudentManager().AsIStudentManager();
22 //cb.RegisterTypeHomeController();
23
24 //从配置文件注册类型
25 cb.RegisterModule(new ConfigurationSettingsReader(autofac));
26 iContainer cb.Build();//创建容器
27 }
28 return iContainer;
29 }
30 }
31 } View Code 3.启动更换工厂 1 public class MvcApplication : System.Web.HttpApplication2 {3 protected void Application_Start()4 {5 AreaRegistration.RegisterAllAreas();6 7 WebApiConfig.Register(GlobalConfiguration.Configuration);8 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);9 RouteConfig.RegisterRoutes(RouteTable.Routes);
10 BundleConfig.RegisterBundles(BundleTable.Bundles);
11
12 //换ControllerFactory(服务器一启动就更换工厂)
13 ControllerBuilder.Current.SetControllerFactory(new CreateAutofacFactory());
14 }
15 } View Code 4.添加配置文件 1 configSections2 !-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID237468 --3 section nameentityFramework typeSystem.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version5.0.0.0, Cultureneutral, PublicKeyTokenb77a5c561934e089 requirePermissionfalse /4 5 !--在配置文件中添加配置节点 name是配置节点的标签名 6 type是读取配置节点内容的类 格式命名空间名.类名程序集名--7 section nameautofac typeAutofac.Configuration.SectionHandler, Autofac.Configuration/8 9 /configSections
10
11 autofac
12 components
13 component typeWinning.Tjgi.Backstage3G.UI.Controllers.HomeController,Winning.Tjgi.Backstage3G.UI
14 /
15 component
16 typetest.Dal.UserDao,test.Dal
17 servicetest.IDal.IUserDao,test.IDal/
18 component
19 typetest.Bll.UserManager,test.Bll
20 servicetest.IBll.IUserManager,test.IBll /
21 /components
22 /autofac View Code 5.构造函数 1 IUserManager uMan null;
2 public HomeController(IUserManager uMan)
3 {
4 this.uMan uMan;
5 } View Code 五步走起如果过程出错的话基本上都是配置文件配置出错还有就是经常忘记更新dll类库在这里要建议下把类库的输出路径改成到UI项目里经常动手才会记忆深刻一直在努力中希望能走出自己的道路虽然脆弱却不退缩。转载于:https://www.cnblogs.com/LiuZhen/p/4023836.html