public static void RegisterTypes(IUnityContainer container) { //注意:要从web.config加载,请取消注释以下行。 //确保将Unity.Configuration添加到using语句。 // container.LoadConfiguration(); // TODO:在这里注册你的类型的映射。 container.RegisterInstance <IUnityContainer>(container);//注入自己!!?? //类型查找器 ITypeFinder typeFinder = new WebTypeFinder(); container.RegisterInstance <ITypeFinder>(typeFinder); //注入Core层中的配置 var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; container.RegisterInstance <ApplicationConfig>(config); //各层注册器 //找到所有实现了IDependencyRegister接口的类 //遍历所有的类,并通过反射创建实例,强转,注册 var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
private static void register_dependency() { //依赖项 => dependencies var typeFinder = new WebTypeFinder(); var builder = new ContainerBuilder(); builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance(); builder.Update(XCodeContainer.Current); //找到其他程序集中实现IDependencyRegistrar的类 => register dependencies provided by other assemblies builder = new ContainerBuilder(); var drTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); var drInstances = new List <IDependencyRegister>(); foreach (var drType in drTypes) { drInstances.Add((IDependencyRegister)Activator.CreateInstance(drType)); } //sort drInstances = drInstances.AsQueryable().ToList(); foreach (var dependencyRegistrar in drInstances) { dependencyRegistrar.RegisterTypes(builder); } builder.Update(XCodeContainer.Current); DependencyResolver.SetResolver(new AutofacDependencyResolver(XCodeContainer.Current)); }
public static void Configure(Microsoft.Practices.Unity.IUnityContainer container) { string configurationFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Unity.config"); var configurationFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configurationFilePath }; var configuration = ConfigurationManager.OpenMappedExeConfiguration(configurationFileMap, ConfigurationUserLevel.None); var section = (Microsoft.Practices.Unity.Configuration.UnityConfigurationSection)configuration.GetSection("unity"); section.Configure(container); var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; container.RegisterInstance <ApplicationConfig>(config); ITypeFinder typeFinder = new WebTypeFinder(); container.RegisterInstance <ITypeFinder>(typeFinder); var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
/// <summary> /// Configured Unity Container. /// </summary> //public static IUnityContainer Container => container.Value; #endregion /// <summary> /// Registers the type mappings with the Unity container. /// </summary> /// <param name="container">The unity container to configure.</param> /// <remarks> /// There is no need to register concrete types such as controllers or /// API controllers (unless you want to change the defaults), as Unity /// allows resolving a concrete type even if it was not previously /// registered. /// </remarks> public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. // Make sure to add a Unity.Configuration to the using statements. // container.LoadConfiguration(); // TODO: Register your type's mappings here. // container.RegisterType<IProductRepository, ProductRepository>(); container.RegisterType(typeof(ILogger), typeof(NLogLogger)); container.RegisterInstance(container); ITypeFinder typeFinder = new WebTypeFinder(); var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; container.RegisterInstance(config); var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
/// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. // container.LoadConfiguration(); // TODO: Register your types here // container.RegisterType<IProductRepository, ProductRepository>(); //注册unity容器 //IUnityContainer->UnityContainer container.RegisterInstance(container); //配置先后顺序,先读取配置文件的,在读取IDependencyRegister接口的 Configuration(container); //通过查找器,查找bin中实现了IDependencyRegister接口的类型 ITypeFinder typeFinder = new WebTypeFinder(); //获取配置节信息 var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; //注册实例 container.RegisterInstance(config); var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); //遍历实现了IDependencyRegister接口类型 foreach (var registerType in registerTypes) { //创建这个对象 var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterType(container); } }
public ServiceLocatorWeb() { ContainerBuilder builder = new ContainerBuilder(); var typeFinder = new WebTypeFinder(); builder.RegisterInstance(typeFinder) .As <ITypeFinder>() .SingleInstance(); builder.RegisterAssemblyModules(typeFinder.GetAssemblies().ToArray()); _container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(_container)); JobFactory.SetResolver(this); }
public static void RegisterTypes(IUnityContainer container) { container.RegisterInstance(container); ITypeFinder typeFinder = new WebTypeFinder(); IEnumerable <Type> registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { IDependencyRegister register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
public static void RegisterComponents(IUnityContainer container) { container.RegisterInstance(container); ITypeFinder typeFinder = new WebTypeFinder(); var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container); }
public static void RegisterTypes(ContainerBuilder container) { //在程序启动的时候将配直节初始化将配置节注入进去 var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; container.RegisterInstance <ApplicationConfig>(config); ITypeFinder typeFinder = new WebTypeFinder(); //实现这个接口的都要注入 var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
/// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> public static void RegisterTypes(IUnityContainer container) { container.RegisterInstance(container); ITypeFinder typeFinder = new WebTypeFinder(); var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; container.RegisterInstance(config); var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
/// <summary> /// Registers the type mappings with the Unity container. /// </summary> /// <param name="container">The unity container to configure.</param> /// <remarks> /// There is no need to register concrete types such as controllers or /// API controllers (unless you want to change the defaults), as Unity /// allows resolving a concrete type even if it was not previously /// registered. /// </remarks> public static void RegisterTypes(IUnityContainer container) { //注意:要从web.config加载,请取消注释以下行。 //确保将Unity.Configuration添加到using语句。 // container.LoadConfiguration(); // 将unity.config文件,增加到容器中 string configurationFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Unity.config"); var configurationFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configurationFilePath }; var configuration = ConfigurationManager.OpenMappedExeConfiguration(configurationFileMap, ConfigurationUserLevel.None); var section = (UnityConfigurationSection)configuration.GetSection("unity"); section.Configure(container); // TODO:在这里注册你的类型的映射。 // container.RegisterInstance(container);//注入自己,在释放的时候有可能会出现错误 container.RegisterType <ExceptionHandlingAttribute>(); //container.RegisterType<IProductRepository, ProductRepository>(new per); //类型查找器 ITypeFinder typeFinder = new WebTypeFinder(); container.RegisterInstance <ITypeFinder>(typeFinder); //注入Core层中的配置 var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; container.RegisterInstance <ApplicationConfig>(config); //各层注册器 //找到所有实现了IDependencyRegister接口的类 //遍历所有的类,并通过反射创建实例,强转,注册 var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
protected override void RegisterDependencies(CopterConfig config) { ContainerBuilder builder = new ContainerBuilder(); IContainer container = builder.Build(); ContainerManager = new WebApiContainerManager(container); //we create new instance of ContainerBuilder //because Build() or Update() method can only be called once on a ContainerBuilder. builder = new ContainerBuilder(); ITypeFinder typeFinder = new WebTypeFinder(); if (config != null) { builder.RegisterInstance(config).As <CopterConfig>().SingleInstance(); } builder.RegisterInstance(this).As <IEngine>().SingleInstance(); builder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance(); IEnumerable <Type> dependencyTypes = typeFinder.FindClassesOfType <IDependencyRegistrar>(); List <IDependencyRegistrar> dependencies = new List <IDependencyRegistrar>(); foreach (Type type in dependencyTypes) { IDependencyRegistrar item = (IDependencyRegistrar)Activator.CreateInstance(type); dependencies.Add(item); } dependencies = dependencies.AsQueryable().OrderBy(x => x.Order).ToList(); foreach (IDependencyRegistrar dependency in dependencies) { dependency.Register(builder, typeFinder, config); } builder.Update(container); // 注册到 webapi 容器 HttpConfiguration configuration = GlobalConfiguration.Configuration; IDependencyResolver resolver = new AutofacWebApiDependencyResolver(ContainerManager.Container); configuration.DependencyResolver = resolver; }
/// <summary> /// Registers the type mappings with the Unity container. /// </summary> /// <param name="container">The unity container to configure.</param> /// <remarks> /// There is no need to register concrete types such as controllers or /// API controllers (unless you want to change the defaults), as Unity /// allows resolving a concrete type even if it was not previously /// registered. /// </remarks> public static void RegisterTypes(IUnityContainer container) { container.RegisterInstance(container); //先注册容器 ITypeFinder typeFinder = new WebTypeFinder(); //web bin查找器 var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; //读取配置节 container.RegisterInstance <ApplicationConfig>(config); //注册配置文件自定义的配置节 //只要是实现了IDependencyRegister的都注册进来 //通过反射 var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>();//查凡是实现了IDependencyRegister的dll foreach (var registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); //遍历创建实例 register.RegisterTypes(container); //调用对象的注入方法(传一个容器过去) } }
/// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> public static void RegisterTypes(IUnityContainer container) { container.RegisterInstance(container); ITypeFinder typeFinder = new WebTypeFinder(); //注入自定义配置文件 var config = ConfigurationManager.GetSection("CustomerConfig") as CustomerConfig; container.RegisterInstance(config); //找到继承IDependencyRegister类型的所有实例 var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); foreach (Type registerType in registerTypes) { var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterTypes(container); } }
public static void RegisterTypes(IUnityContainer container) { //注册unity容器 //IUnityContainer->UnityContainer container.RegisterInstance(container); //通过查找器,查找bin中实现了IDependencyRegister接口的类型 ITypeFinder typeFinder = new WebTypeFinder(); //获取配置节信息 var config = ConfigurationManager.GetSection("applicationConfig") as ApplicationConfig; //注册实例 container.RegisterInstance(config); var registerTypes = typeFinder.FindClassesOfType <IDependencyRegister>(); //遍历实现了IDependencyRegister接口类型 foreach (var registerType in registerTypes) { //创建这个对象 var register = (IDependencyRegister)Activator.CreateInstance(registerType); register.RegisterType(container); } }