private void RegisterContextIfNeeded(IApplicationContext childContext) { if (!ContextRegistry.IsContextRegistered(childContext.Name)) { ContextRegistry.RegisterContext(childContext); } }
private static void Initialize(Type componentType) { // this is to ensure, that assemblies placed next to the component assembly can be loaded // even when they are not strong named. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; FileInfo componentAssemblyFile = new FileInfo(componentType.Assembly.Location); FileInfo assemblyFile = new FileInfo(componentAssemblyFile.FullName); bool isRunningOutOfProcess = IsRunningOutOfProcess(); FileInfo configFile = new FileInfo(componentAssemblyFile.FullName + ".config"); // no config file and in-proc -> reuse app's context, error otherwise if (!configFile.Exists) { if (!isRunningOutOfProcess) { // check for context with component's name if (ContextRegistry.IsContextRegistered(componentType.Name)) { Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section <spring/context name={1}> from app.config", componentAssemblyFile.FullName, componentType.Name)); _appContext = ContextRegistry.GetContext(componentType.Name); } else { Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section <spring/context> from file '{1}'", componentAssemblyFile.FullName, configFile.FullName)); _appContext = ContextRegistry.GetContext(); } return; } throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require <spring/context> section in configuration file '" + configFile.FullName + "'"); } // set and switch to component assembly's directory (affects resolving relative paths during context instantiation!) componentDirectory = componentAssemblyFile.Directory.FullName; Environment.CurrentDirectory = componentDirectory; if (isRunningOutOfProcess) { Trace.WriteLine(string.Format("configuring COM OutProc Server '{0}' using '{1}'", componentAssemblyFile.FullName, configFile.FullName)); // read in config file ExeConfigurationSystem comConfig = new ExeConfigurationSystem(assemblyFile.FullName); // make the config "global" for this process, replacing any // existing configuration that might already have been loaded ConfigurationUtils.SetConfigurationSystem(comConfig, true); _appContext = ContextRegistry.GetContext(); } else { Trace.WriteLine(string.Format("configuring COM InProc Server '{0}' using section <spring/context> from file '{1}'", componentAssemblyFile.FullName, configFile.FullName)); _appContext = (IApplicationContext)ConfigurationReader.GetSection(new FileSystemResource(configFile.FullName), "spring/context"); } if (_appContext == null) { throw ConfigurationUtils.CreateConfigurationException("Spring-exported COM components require <spring/context> section in configuration file"); } Trace.WriteLine(string.Format("completed configuring COM Component '{0}' using '{1}'", componentAssemblyFile.FullName, configFile.FullName)); }
public void DoesNotRegisterContextWithContextRegistry() { fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(); Assert.IsTrue(fixtureInstance.RegisterContextWithContextRegistry); fixtureInstance.RegisterContextWithContextRegistry = false; fixtureInstance.TestInitialize(); Assert.IsFalse(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name)); fixtureInstance.TestCleanup(); Assert.IsFalse(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name)); }
/// <summary> /// Caches application context. /// </summary> /// <param name="key">Key to use.</param> /// <param name="context">Context to cache.</param> public void AddContext(object key, IConfigurableApplicationContext context) { AssertUtils.ArgumentNotNull(context, "context", "ApplicationContext must not be null"); string keyString = ContextKeyString(key); contextKeyToContextMap.Add(keyString, context); if (RegisterContextWithContextRegistry && !ContextRegistry.IsContextRegistered(context.Name)) { ContextRegistry.RegisterContext(context); } }
public void UnregistersFromContextRegistryWhenDirty() { fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(); Assert.IsTrue(fixtureInstance.RegisterContextWithContextRegistry); fixtureInstance.SetUp(); Assert.IsTrue(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name)); fixtureInstance.TearDown(); Assert.IsTrue(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name)); fixtureInstance.SetUp(); Assert.IsTrue(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name)); fixtureInstance.SetDirty(); fixtureInstance.TearDown(); Assert.IsFalse(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name)); }
public static void RegisterContext(string xmlContext, string rootContextName = SpringAutowire.DefaultRootContextName) { if (!ContextRegistry.IsContextRegistered(rootContextName)) { lock (_lock) { if (!ContextRegistry.IsContextRegistered(rootContextName)) { XmlApplicationContext ctx = new XmlApplicationContext(new XmlApplicationContextArgs { Name = rootContextName, ConfigurationResources = new IResource[] { new StringResource(xmlContext) } }); ContextRegistry.RegisterContext(ctx); } } } }
/// <summary> /// 创建容器 /// <code> /// IApplicationContext Container = ChildContainer.Create(container, contextName, ecs); /// </code> /// </summary> /// <param name="container">IApplicationContext容器</param> /// <param name="contextName">容器名</param> /// <param name="configuration">Configuration配置对象</param> /// <returns></returns> public static IApplicationContext Create(IApplicationContext container, string contextName, Configuration configuration) { string _contextName = contextName.ToLower(); ExeConfigurationSystem config = new ExeConfigurationSystem(configuration); IApplicationContext ctx = config.GetSection(_contextName, container, AbstractApplicationContext.ContextSectionName) as IApplicationContext; if (!ContextRegistry.IsContextRegistered(_contextName) && ctx.Name != "spring.root") { try { ContextRegistry.RegisterContext(ctx); } catch (Exception e) { throw new Exception("Registered ContextName does not exist!Could be a plug-in ContextName for plug-in assembly name (not fullName)", e); } } return(ctx); }
private static AbstractApplicationContext GetAbstractApplicationContext() { if (!ContextRegistry.IsContextRegistered(_RootContext)) { lock (lock_init) { if (!ContextRegistry.IsContextRegistered(_RootContext)) { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.Name = _RootContext; XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(ctx); reader.LoadObjectDefinitions(new StringResource("<?xml version=\"1.0\" encoding=\"utf-8\" ?><objects></objects>")); ContextRegistry.RegisterContext(ctx); } } } return(ContextRegistry.GetContext(_RootContext) as AbstractApplicationContext); }
public void SetUp() { //BasicConfigurator.Configure(); string assemblyName = GetType().Assembly.GetName().Name; //ctx = new XmlApplicationContext("assembly://" + assemblyName + "/Spring.Data.NHibernate/NHDAOTests.xml"); string[] contextFiles = new string[] { "assembly://" + assemblyName + "/Spring.Data.NHibernate/Controllers.xml", "assembly://" + assemblyName + "/Spring.Data.NHibernate/Services.xml", "assembly://" + assemblyName + "/Spring.Data.NHibernate/Dao.xml" }; ctx = new XmlApplicationContext(contextFiles); ctx.Name = AbstractApplicationContext.DefaultRootContextName; if (!ContextRegistry.IsContextRegistered(AbstractApplicationContext.DefaultRootContextName)) { ContextRegistry.RegisterContext(ctx); } }
/// <summary> /// Tells, whether the given <paramref name="contextName"/> was registered, already [true], /// or not [false]. /// </summary> /// <remarks> /// Loading a spring-context with the same <paramref name="contextName"/> repeatedly will cause /// an exception in spring. /// </remarks> public static bool IsContextRegistered(string contextName) { return(ContextRegistry.IsContextRegistered(contextName)); }