Example #1
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //×¢²áAutofac;£¨1£©
            //AutofacConfig_WebApi.Regist();


            //×¢²áAutofac£»£¨2£©
            //var builder = new ContainerBuilder();

            //AutofacBuilder.SetupResolveRules(builder);
            //builder.RegisterControllers(Assembly.GetExecutingAssembly());

            //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());


            //builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
            //builder.RegisterWebApiModelBinderProvider();

            //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().AsSelf();

            //var container = builder.Build();

            //DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            //GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            //×¢²áAutofac(3)
            var builder = new ContainerBuilder();

            builder.RegisterApiControllers(Assembly.Load("WebFramework.WebApiControllers"));

            AutofacBuilder.SetupResolveRules(builder);

            builder.RegisterAssemblyTypes(Assembly.Load("WebFramework.Data"), Assembly.Load("WebFramework.Data"))
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces();
            AutofacBuilderRepository.SetupResolveRules(builder);


            builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();

            var container = builder.Build();

            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            //×¢²áAttribute
            var filters = GlobalConfiguration.Configuration.Filters;

            filters.Add(new SecurityFilter()); // global secruity filter

            GlobalFilters.Filters.Add(new GlobalMvcFilter());
        }