Exemplo n.º 1
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //启用压缩
            BundleTable.EnableOptimizations = true;
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //注入 Ioc
            var iocBuilder = new AutofacBuilder();

            iocBuilder.RegisterDependencyResolver(builder =>
            {
                builder.RegisterControllers(typeof(MvcApplication).Assembly);

                builder.Register(context => new _21EducationDbContext("name=_21Education")).AsSelf().SingleInstance();
                //注册服务
                var serviceType = typeof(IDependency);
                builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => serviceType.IsAssignableFrom(t) && t != serviceType)
                .AsImplementedInterfaces().InstancePerLifetimeScope();
                //注册实体
                var entityType = typeof(IEntity);
                builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => entityType.IsAssignableFrom(t) && t != typeof(IDependency))
                .AsSelf();
            });
            DependencyResolver.SetResolver(iocBuilder.Build());
        }