예제 #1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="FileBasedConfiguration" /> class.
 /// </summary>
 /// <param name="configurationFileContentsProvider">
 ///     The configuration file contents provider. An example implementation of
 ///     <see cref="IConfigurationFileContentsProvider" /> implementation is
 ///     <see cref="FileBasedConfigurationFileContentsProvider" />
 /// </param>
 /// <param name="entryAssemblyFolder">
 ///     The location where the executable is.
 ///     For non test projects <see cref="IGlobalsCore.EntryAssemblyFolder" /> can be used as a value for this parameter.
 ///     However, for tests projects <see cref="IGlobalsCore.EntryAssemblyFolder" /> might be
 ///     be the folder where the test execution library is, so a different value might need to be passed.
 /// </param>
 /// <param name="loadedAssemblies"> Instance of <see cref="ILoadedAssemblies"/> used to add add all or some of currently
 ///                     loaded assemblies as dependencies for  dynamically generated assemblies.
 ///                     Use an instance of <see cref="AllLoadedAssemblies"/> to add references to all assemblies loaded into current application
 ///                     domain to the dynamically generated assembly. Use <see cref="NoLoadedAssemblies"/> to not add any additional assemblies
 ///                     references to any additional assemblies as dependencies for  dynamically generated assemblies.
 ///                     Provide your own implementation to add only some of loaded assemblies as dependencies.
 /// </param>
 public FileBasedConfigurationParameters([NotNull] IConfigurationFileContentsProvider configurationFileContentsProvider,
                                         [NotNull] string entryAssemblyFolder, [NotNull] ILoadedAssemblies loadedAssemblies)
 {
     ConfigurationFileContentsProvider = configurationFileContentsProvider;
     EntryAssemblyFolder = entryAssemblyFolder;
     LoadedAssemblies    = loadedAssemblies;
 }
 /// <summary>
 ///     Creates an instance of <see cref="IFileBasedDiContainerConfigurator" /> for file based dependency injection
 ///     configuration.
 /// </summary>
 /// <param name="configurationFileContentsProvider">
 ///     The configuration file contents provider.
 ///     An example implementation of <see cref="IConfigurationFileContentsProvider" /> implementation is
 ///     <see cref="FileBasedConfigurationFileContentsProvider" />
 /// </param>
 /// <param name="entryAssemblyFolder">The entry assembly folder.</param>
 /// <param name="loadedAssemblies"> Instance of <see cref="ILoadedAssemblies"/> used to add add all or some of currently
 ///                     loaded assemblies as dependencies for  dynamically generated assemblies.
 ///                     Use an instance of <see cref="AllLoadedAssemblies"/> to add references to all assemblies loaded into current application
 ///                     domain to the dynamically generated assembly. Use <see cref="NoLoadedAssemblies"/> to not add any additional assemblies
 ///                     references to any additional assemblies as dependencies for  dynamically generated assemblies.
 ///                     Provide your own implementation to add only some of loaded assemblies as dependencies.
 /// </param>
 /// <param name="loadedConfiguration">
 ///     Output parameter that returns an instance of
 ///     <see cref="ConfigurationFile.IConfiguration" />.
 /// </param>
 /// <param name="configurationFileXmlDocumentLoaded">The configuration file XML document loaded.</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] IConfigurationFileContentsProvider configurationFileContentsProvider,
                                                           [NotNull] string entryAssemblyFolder,
                                                           [NotNull] ILoadedAssemblies loadedAssemblies,
                                                           out IConfiguration loadedConfiguration,
                                                           [CanBeNull] ConfigurationFileXmlDocumentLoadedEventHandler configurationFileXmlDocumentLoaded = null)
 {
     return(StartFileBasedDi(new FileBasedConfigurationParameters(configurationFileContentsProvider, entryAssemblyFolder, loadedAssemblies)
     {
         ConfigurationFileXmlDocumentLoaded = configurationFileXmlDocumentLoaded
     }, out loadedConfiguration));
 }
예제 #3
0
    private void LoadDiContainerAndValidate(CreateFileBasedConfigurationParametersDelegate createFileBasedConfigurationParameters,
                                            [CanBeNull, ItemNotNull] IEnumerable <string> additionalReferencedAssemblies = null,
                                            [CanBeNull] ILoadedAssemblies loadedAssemblies = null, [CanBeNull] Action <XmlDocument> modifyConfigurationFileOnLoad = null)
    {
        loadedAssemblies ??= new LoadedAssemblies(false);

        var fileBasedConfigurationParameters = createFileBasedConfigurationParameters(
            new FileBasedConfigurationFileContentsProvider(Path.Combine(Helpers.TestsEntryAssemblyFolder, _configurationRelativePath)),
            Helpers.TestsEntryAssemblyFolder, loadedAssemblies);

        if (additionalReferencedAssemblies != null)
        {
            fileBasedConfigurationParameters.AdditionalReferencedAssemblies = additionalReferencedAssemblies;
        }

        fileBasedConfigurationParameters.AttributeValueTransformers         = new[] { new FileFolderPathAttributeValueTransformer() };
        fileBasedConfigurationParameters.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);
        };

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

        var diContainer = ioCConfigurator.DiContainer;

        Assert.AreEqual(18, diContainer.Resolve <IDogWeightsCalculator>().GetDogWeightInKilograms());
        diContainer.Dispose();
    }