public void SunnyDay()
        {
            FileInfo resFile = TestResourceLoader.ExportResource(this, ".config", new FileInfo(Path.GetTempFileName()+".config"));
            string exePath = resFile.FullName.Substring(0, resFile.FullName.Length - ".config".Length);
            Assert.IsTrue(resFile.Exists);
            IInternalConfigSystem prevConfig = null;
            try
            {
                ExeConfigurationSystem ccs = new ExeConfigurationSystem(exePath);
                prevConfig = ConfigurationUtils.SetConfigurationSystem(ccs, true);
                LogSetting settings = (LogSetting) ConfigurationManager.GetSection("logging");
                Assert.AreEqual(typeof (TraceLoggerFactoryAdapter), settings.FactoryAdapterType);

                Assert.AreEqual("from custom config!", ConfigurationManager.AppSettings["key"]);

                Assert.IsNull(ConfigurationManager.GetSection("spring/context"));
            }
            finally
            {
                if (prevConfig != null)
                {
                    ConfigurationUtils.SetConfigurationSystem(prevConfig, true);
                }
                resFile.Delete();
            }
        }
        public void SunnyDay()
        {
            FileInfo resFile = TestResourceLoader.ExportResource(this, ".config", new FileInfo(Path.GetTempFileName() + ".config"));
            string   exePath = resFile.FullName.Substring(0, resFile.FullName.Length - ".config".Length);

            Assert.IsTrue(resFile.Exists);
            IInternalConfigSystem prevConfig = null;

            try
            {
                ExeConfigurationSystem ccs = new ExeConfigurationSystem(exePath);
                prevConfig = ConfigurationUtils.SetConfigurationSystem(ccs, true);
                LogSetting settings = (LogSetting)ConfigurationManager.GetSection("logging");
                Assert.AreEqual(typeof(TraceLoggerFactoryAdapter), settings.FactoryAdapterType);

                Assert.AreEqual("from custom config!", ConfigurationManager.AppSettings["key"]);

                Assert.IsNull(ConfigurationManager.GetSection("spring/context"));
            }
            finally
            {
                if (prevConfig != null)
                {
                    ConfigurationUtils.SetConfigurationSystem(prevConfig, true);
                }
                resFile.Delete();
            }
        }
Exemplo n.º 3
0
        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));
        }
        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));
        }