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解析器 }
private void RegisterWorkerDependancyResolvers() { _requestLifetimeScopeProvider = new RequestLifetimeScopeProvider(_applicationContainer); var mvcResolver = new Autofac.Integration.Mvc.AutofacDependencyResolver(_applicationContainer, _requestLifetimeScopeProvider); _originalResolver = DependencyResolver.Current; DependencyResolver.SetResolver(mvcResolver); }
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); }
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 }
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); }
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; }
protected virtual void RegisterMvcDependencyResolver(System.Web.Mvc.IDependencyResolver resolver) { // Mvc System.Web.Mvc.DependencyResolver.SetResolver(resolver); }
public static IDependencyResolver ToServiceResolver(this System.Web.Mvc.IDependencyResolver dependencyResolver) { return(new ServiceResolverAdapter(dependencyResolver)); }
public WindsorControllerFactory(System.Web.Mvc.IDependencyResolver resolver) { _resolver = resolver; }
public WebApiResolverAdapter(Mvc.IDependencyResolver dependencyResolver) { _dependencyResolver = dependencyResolver; }