Beispiel #1
0
    public static (IContainerInfo containerInfo, IConfiguration configuration) LoadConfigurationFile(DiImplementationType diImplementationType,
                                                                                                     [NotNull] string configurationRelativePath,
                                                                                                     [CanBeNull] IDiModule[] additionalModulesToLoad = null,
                                                                                                     [CanBeNull] Action <XmlDocument> modifyConfigurationFileOnLoad = null)
    {
        TestsHelper.SetupLogger();

        var ioCConfigurator = new DiContainerBuilder.DiContainerBuilder()
                              .StartFileBasedDi(
            new FileBasedConfigurationParameters(
                new FileBasedConfigurationFileContentsProvider(Path.Combine(Helpers.TestsEntryAssemblyFolder, configurationRelativePath)),
                Helpers.TestsEntryAssemblyFolder, new LoadedAssembliesForTests())
        {
            ConfigurationFileXmlDocumentLoaded = (sender, e) =>
            {
                Helpers.EnsureConfigurationDirectoryExistsOrThrow(e.XmlDocument.SelectElement("/iocConfiguration/appDataDir").GetAttribute("path"));

                // Lets explicitly set the DiManager to Autofac. Since we are going to test failure, the Di manager implementation does not matter.
                // However, this will give us predictability on what modules will be enabled.
                e.XmlDocument.SelectElement("/iocConfiguration/diManagers").SetAttributeValue(ConfigurationFileAttributeNames.ActiveDiManagerName, diImplementationType.ToString());
                modifyConfigurationFileOnLoad?.Invoke(e.XmlDocument);
            },
            AttributeValueTransformers = new[] { new FileFolderPathAttributeValueTransformer() },
        }, out _)
                              .WithoutPresetDiContainer();

        if (additionalModulesToLoad?.Length > 0)
        {
            ioCConfigurator.AddAdditionalDiModules(additionalModulesToLoad);
        }

        var containerInfo = ioCConfigurator.RegisterModules().Start();

        return(containerInfo, containerInfo.DiContainer.Resolve <IConfiguration>());
    }