Ejemplo n.º 1
0
 public ServiceResolverAdapter(System.Web.Mvc.IDependencyResolver dependencyResolver)
 {
     if (dependencyResolver == null)
     {
         throw new ArgumentNullException("dependencyResolver");
     }
     this.dependencyResolver = dependencyResolver;
 }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //设置AutoFac解析器
            System.Web.Mvc.IDependencyResolver autoFacResolver = MvcContainer.Init(); //初始化容器
            DependencyResolver.SetResolver(autoFacResolver);                          //将AutoFac解析器设置为系统DI解析器
        }
Ejemplo n.º 3
0
        private void RegisterWorkerDependancyResolvers()
        {
            _requestLifetimeScopeProvider = new RequestLifetimeScopeProvider(_applicationContainer);

            var mvcResolver = new Autofac.Integration.Mvc.AutofacDependencyResolver(_applicationContainer, _requestLifetimeScopeProvider);

            _originalResolver = DependencyResolver.Current;
            DependencyResolver.SetResolver(mvcResolver);
        }
Ejemplo n.º 4
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //初始化容器,并返回适用于MVC的AutoFac解析器
            System.Web.Mvc.IDependencyResolver autoFacResolver = Container.Init();
            //将AutoFac解析器设置为系统DI解析器
            DependencyResolver.SetResolver(autoFacResolver);
        }
Ejemplo n.º 5
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //ImageResizer
            //new PluginName().Install(ImageResizer.Configuration.Config.Current);
            #region IoC
            // 文档参考 http://autofaccn.readthedocs.io/zh/latest/getting-started/index.html
            var builder = new ContainerBuilder();
            // Mvc Register
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).AsSelf().PropertiesAutowired();
            builder.RegisterFilterProvider();

            //WebApi Register
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).AsSelf().PropertiesAutowired();
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
            builder.RegisterWebApiModelBinderProvider();

            //Services
            builder.RegisterType <ArticleService>().As <IArticleService>().InstancePerDependency();

            // 注册Autofac Module
            builder.RegisterModule(new LoggingModule());

            var connectionString = ConfigurationManager.ConnectionStrings["components"].ConnectionString;
            //数据库
            builder.Register <IDbContext>(c => new DIILContext(connectionString)).InstancePerLifetimeScope();
            //repositories
            builder.RegisterGeneric(typeof(EfRepository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope();

            var container = builder.Build();

            // WebApiResolver
            var webApiResolver = new AutofacWebApiDependencyResolver(container);
            apiResolver = webApiResolver;
            GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;

            // Set the dependency resolver for MVC.
            var resolver = new AutofacDependencyResolver(container);
            mvcResolver = resolver;
            DependencyResolver.SetResolver(mvcResolver);

            #endregion
        }
Ejemplo n.º 6
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            Mapper.Initialize(x => {
                x.CreateMap <LogInfo, VLogInfo>();
                x.CreateMap <NoticeInfo, NoticeInfoDto>();
            });
            //Mapper.Initialize(x => x.CreateMap<NoticeInfo, NoticeInfoDto>());

            //初始化容器,并返回适用于MVC的AutoFac解析器
            System.Web.Mvc.IDependencyResolver autoFacResolver = Container.Init();
            //将AutoFac解析器设置为系统DI解析器
            DependencyResolver.SetResolver(autoFacResolver);
        }
Ejemplo n.º 7
0
        public void Application_Start_SetANewDependencyResolver()
        {
            var mockMvcDefaultDependencyResolver = MockRepository.GenerateMock <IDependencyResolver>();

            System.Web.Mvc.DependencyResolver.SetResolver(mockMvcDefaultDependencyResolver);
            var mockKernel           = MockRepository.GenerateMock <IKernel>();
            var stubWindsorContainer = MockRepository.GenerateStub <IWindsorContainer>();

            stubWindsorContainer.Stub(s => s.Kernel).Return(mockKernel);

            var httpApplicationByConventions = new HttpApplicationByConventions(stubWindsorContainer);

            httpApplicationByConventions.Application_Start();

            IDependencyResolver currentDependencyResolver = System.Web.Mvc.DependencyResolver.Current;

            Assert.IsNotNull(currentDependencyResolver);
            var windsorDependencyResolver = (WindsorDependencyResolver)currentDependencyResolver;

            Assert.AreEqual(mockMvcDefaultDependencyResolver, windsorDependencyResolver.DefaultMvcResolver);
            Assert.AreEqual(mockKernel, windsorDependencyResolver.Kernel);
        }
 /// <summary>
 /// Registers the DependencyResolver implementation with the MVC runtime.
 /// <remarks>
 /// You must override this method in a derived class to control the manner in which the
 /// <see cref="System.Web.Mvc.IDependencyResolver"/> is registered.
 /// </remarks>
 /// </summary>
 public virtual void RegisterDependencyResolver(System.Web.Mvc.IDependencyResolver resolver)
 {
     ThreadSafeDependencyResolverRegistrar.Register(resolver);
 }
		public ServiceResolverAdapter(System.Web.Mvc.IDependencyResolver dependencyResolver)
		{
			if (dependencyResolver == null) throw new ArgumentNullException("dependencyResolver");
			this.dependencyResolver = dependencyResolver;
		}
Ejemplo n.º 10
0
 protected virtual void RegisterMvcDependencyResolver(System.Web.Mvc.IDependencyResolver resolver)
 {
     // Mvc
     System.Web.Mvc.DependencyResolver.SetResolver(resolver);
 }
Ejemplo n.º 11
0
 public static IDependencyResolver ToServiceResolver(this System.Web.Mvc.IDependencyResolver dependencyResolver)
 {
     return(new ServiceResolverAdapter(dependencyResolver));
 }
Ejemplo n.º 12
0
 public WindsorControllerFactory(System.Web.Mvc.IDependencyResolver resolver)
 {
     _resolver = resolver;
 }
Ejemplo n.º 13
0
 public WebApiResolverAdapter(Mvc.IDependencyResolver dependencyResolver)
 {
     _dependencyResolver = dependencyResolver;
 }
 public WindsorControllerFactory(System.Web.Mvc.IDependencyResolver resolver)
 {
     _resolver = resolver;
 }