Пример #1
0
    public void FileBasedConfigurationParameters_AdditionalReferencedAssemblies_Demo()
    {
        TestsHelper.SetupLogger();

        var fileBasedConfigurationParameters = new FileBasedConfigurationParameters(
            new FileBasedConfigurationFileContentsProvider(
                Path.Combine(Helpers.TestsEntryAssemblyFolder, @"DocumentationTests\AutoServiceCustom\DemoIoCConfiguration_autoServiceCustom.xml")),
            Helpers.TestsEntryAssemblyFolder,
            // LoadedAssembliesForTests is an implementation of ILoadedAssemblies that has a method
            // "IEnumerable<Assembly> GetAssemblies()" that returns list of assemblies to add as references to
            // generate dynamic assembly.
            new LoadedAssembliesForTests())
        {
            AdditionalReferencedAssemblies = new []
            {
                // List additional assemblies that should be added to dynamically generated assembly as references
                Path.Combine(Helpers.GetTestFilesFolderPath(), @"DynamicallyLoadedDlls\TestProjects.DynamicallyLoadedAssembly1.dll"),
                Path.Combine(Helpers.GetTestFilesFolderPath(), @"DynamicallyLoadedDlls\TestProjects.DynamicallyLoadedAssembly2.dll")
            },
            AttributeValueTransformers         = new[] { new FileFolderPathAttributeValueTransformer() },
            ConfigurationFileXmlDocumentLoaded = (sender, e) =>
                                                 Helpers.EnsureConfigurationDirectoryExistsOrThrow(e.XmlDocument.SelectElement("/iocConfiguration/appDataDir").GetAttribute("path"))
        };

        var containerInfo = new DiContainerBuilder.DiContainerBuilder()
                            .StartFileBasedDi(fileBasedConfigurationParameters, out _)
                            .WithoutPresetDiContainer()
                            .RegisterModules().Start();

        var autoImplementedInterfaceInstance = containerInfo.DiContainer.Resolve <IoC.Configuration.Tests.DocumentationTests.AutoServiceCustom.ISimpleAutoImplementedInterface1>();

        Assert.AreEqual(10, autoImplementedInterfaceInstance.GetValue());
    }
Пример #2
0
        protected static void ClassInitializeCommon(DiImplementationType diImplementationType, Action <ConfigurationFileXmlDocumentLoadedEventArgs> configurationFileXmlDocumentLoadedEventHandler)
        {
            // JetBrains.ReSharper.TestRunner.Merged
            TestsHelper.SetupLogger();
            Log4Tests.LogLevel = LogLevel.Debug;

            _diImplementationType = diImplementationType;

            _mainLifeTimeScopeTerminatedExecuted = false;
            var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder();


            var fileBasedConfigurationParameters = new FileBasedConfigurationParameters(new FileBasedConfigurationFileContentsProvider(
                                                                                            Path.Combine(Helpers.TestsEntryAssemblyFolder, "IoCConfiguration_Overview.xml")),
                                                                                        Helpers.TestsEntryAssemblyFolder, new LoadedAssembliesForTests())
            {
                AttributeValueTransformers         = new[] { new FileFolderPathAttributeValueTransformer() },
                ConfigurationFileXmlDocumentLoaded = (sender, e) =>
                {
                    Helpers.EnsureConfigurationDirectoryExistsOrThrow(e.XmlDocument.SelectElement("/iocConfiguration/appDataDir").GetAttribute("path"));

                    Helpers.ReplaceActiveDiManagerInConfigurationFile(e.XmlDocument, _diImplementationType);
                    configurationFileXmlDocumentLoadedEventHandler?.Invoke(e);
                }
            };

            _containerInfo = diContainerBuilder.StartFileBasedDi(
                fileBasedConfigurationParameters, out _)
                             .WithoutPresetDiContainer()
                             .AddAdditionalDiModules(new TestModule2())
                             .RegisterModules()
                             .Start();

            _diContainer = _containerInfo.DiContainer;

            var injectedStartupActionsHelper = _diContainer.Resolve <ClassToTestServicesInjection <IStartupAction> >();

            _startupActionsToTest = injectedStartupActionsHelper.Implementations.Where(x => x is IStartupActionState &&
                                                                                       new[]
            {
                "DynamicallyLoadedAssembly1.Implementations.StartupAction1",
                "DynamicallyLoadedAssembly1.Implementations.StartupAction2"
            }
                                                                                       .Contains(x.GetType().FullName)).ToList();

            var injectedPluginsHelper = _diContainer.Resolve <ClassToTestServicesInjection <IPlugin> >();

            _pluginsToTest = injectedPluginsHelper.Implementations.Where(x => x is IPluginState &&
                                                                         new[]
            {
                "TestPluginAssembly1.Implementations.Plugin1",
                "TestPluginAssembly2.Implementations.Plugin2"
            }
                                                                         .Contains(x.GetType().FullName)).ToList();

            _diContainer.MainLifeTimeScope.LifeTimeScopeTerminated += (sender, e) => { _mainLifeTimeScopeTerminatedExecuted = true; };
        }
        /// <summary>
        ///     Creates an instance of <see cref="IFileBasedDiContainerConfigurator" /> for file based dependency injection
        ///     configuration.
        /// </summary>
        /// <param name="fileBasedConfigurationParameters">An instance of <see cref="FileBasedConfigurationParameters"/> used to load and process the configuration file.</param>
        /// <param name="loadedConfiguration">
        ///     Output parameter that returns an instance of
        ///     <see cref="ConfigurationFile.IConfiguration" />.
        /// </param>
        /// <exception cref="ConfigurationParseException">Throws this exception if configuration parse/load fails.</exception>
        /// <exception cref="LoggerWasNotInitializedException"></exception>
        /// <exception cref="OROptimizer.DynamicCode.DynamicCodeGenerationException">Throws this exception if dynamic code generation fails.</exception>
        /// <exception cref="Exception">Throws this exception.</exception>
        public IFileBasedDiContainerConfigurator StartFileBasedDi([NotNull] FileBasedConfigurationParameters fileBasedConfigurationParameters,
                                                                  out IConfiguration loadedConfiguration)
        {
            if (!LogHelper.IsContextInitialized)
            {
                throw new LoggerWasNotInitializedException();
            }

            var configuration = new FileBasedConfiguration(fileBasedConfigurationParameters);

            configuration.Init();
            loadedConfiguration = configuration.Configuration;
            return(new FileBasedDiContainerConfigurator(configuration));
        }
Пример #4
0
        private void LoadConfiguration(Action <IContainerInfo, IConfiguration> testConfiguration,
                                       ConfigurationFileXmlDocumentLoadedEventHandler configurationFileXmlDocumentLoaded)
        {
            var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder();

            var fileBasedConfigurationParameters = new FileBasedConfigurationParameters(new FileBasedConfigurationFileContentsProvider(
                                                                                            Path.Combine(Helpers.TestsEntryAssemblyFolder, "IoCConfiguration_Overview.xml")),
                                                                                        Helpers.TestsEntryAssemblyFolder, new LoadedAssembliesForTests())
            {
                AttributeValueTransformers         = new [] { new FileFolderPathAttributeValueTransformer() },
                ConfigurationFileXmlDocumentLoaded = configurationFileXmlDocumentLoaded
            };

            using (var containerInfo = diContainerBuilder.StartFileBasedDi(
                       fileBasedConfigurationParameters, out var loadedConfiguration)
                                       .WithoutPresetDiContainer()
                                       //.AddAdditionalDiModules(new SuccessfulConfigurationLoadTests.SuccessfulConfigurationLoadTests.TestModule2())
                                       .RegisterModules()
                                       .Start())
            {
                testConfiguration(containerInfo, loadedConfiguration);
            }
        }