/// <summary>
        /// Gets the or create builder.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>RocketHostBuilder.</returns>
        internal static RocketHostBuilder GetOrCreateBuilder(IHostBuilder builder)
        {
            if (!Builders.TryGetValue(builder, out var conventionalBuilder))
            {
                var diagnosticSource          = new DiagnosticListener("Rocket.Surgery.Hosting");
                var dependencyContext         = DependencyContext.Default;
                var logger                    = new DiagnosticLogger(diagnosticSource);
                var serviceProviderDictionary = new ServiceProviderDictionary(builder.Properties);
                serviceProviderDictionary.Set <ILogger>(logger);
                var assemblyCandidateFinder = new DependencyContextAssemblyCandidateFinder(dependencyContext, logger);
                var assemblyProvider        = new DependencyContextAssemblyProvider(dependencyContext, logger);
                var scanner = new SimpleConventionScanner(assemblyCandidateFinder, serviceProviderDictionary, logger);
                conventionalBuilder = new RocketHostBuilder(builder, scanner, assemblyCandidateFinder, assemblyProvider, diagnosticSource, serviceProviderDictionary);

                var host = new RocketContext(builder);
                builder
                .ConfigureHostConfiguration(host.CaptureArguments)
                .ConfigureHostConfiguration(host.ConfigureCli)
                .ConfigureAppConfiguration(host.ReplaceArguments)
                .ConfigureAppConfiguration(host.ConfigureAppConfiguration)
                .ConfigureServices(host.ConfigureServices)
                .ConfigureServices(host.DefaultServices);
                Builders.Add(builder, conventionalBuilder);
            }

            return(conventionalBuilder);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the or create builder.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="startupInstance">The startup instance.</param>
        /// <param name="environment">The environment.</param>
        /// <returns>RocketFunctionHostBuilder.</returns>
        internal static RocketFunctionHostBuilder GetOrCreateBuilder(IWebJobsBuilder builder, object startupInstance, IRocketEnvironment environment)
        {
            if (!Builders.TryGetValue(builder, out var conventionalBuilder))
            {
                var diagnosticSource  = new DiagnosticListener("Rocket.Surgery.Hosting");
                var functionsAssembly = startupInstance.GetType().Assembly;

                var location = Path.GetDirectoryName(functionsAssembly.Location);
                DependencyContext dependencyContext = null;
                while (dependencyContext == null && !string.IsNullOrEmpty(location))
                {
                    var depsFilePath = Path.Combine(location, functionsAssembly.GetName().Name + ".deps.json");
                    if (File.Exists(depsFilePath))
                    {
                        using (var stream = File.Open(depsFilePath, FileMode.Open, FileAccess.Read))
                        {
                            dependencyContext = new DependencyContextJsonReader().Read(stream);
                            break;
                        }
                    }
                    location = Path.GetDirectoryName(location);
                }
                var logger = new DiagnosticLogger(diagnosticSource);
                var assemblyCandidateFinder = new DependencyContextAssemblyCandidateFinder(dependencyContext, logger);
                var assemblyProvider        = new DependencyContextAssemblyProvider(dependencyContext, logger);
                var properties = new ServiceProviderDictionary();
                var scanner    = new SimpleConventionScanner(assemblyCandidateFinder, properties, logger);
                conventionalBuilder = new RocketFunctionHostBuilder(builder, functionsAssembly, startupInstance, environment, scanner, assemblyCandidateFinder, assemblyProvider, diagnosticSource, properties);
                Builders.Add(builder, conventionalBuilder);
            }

            return(conventionalBuilder);
        }
        public void Assemblies_FindsCandidateAssemblies()
        {
            // Arrange
            var assemblies = new[]
            {
                GetType().Assembly,
                typeof(DependencyContextAssemblyProvider).Assembly,
                typeof(Ensure).Assembly
            };
            var dependencyContext = DependencyContext.Load(assemblies[0]);
            var provider          = new DependencyContextAssemblyProvider(dependencyContext);

            // Act
            var discoveredAssemblies = provider.Assemblies;

            // Assert
            Assert.NotNull(discoveredAssemblies);
            Assert.Equal(3, discoveredAssemblies.Count);
            Assert.Contains(discoveredAssemblies, a => a.FullName.Equals(assemblies[0].FullName));
            Assert.Contains(discoveredAssemblies, a => a.FullName.Equals(assemblies[1].FullName));
            Assert.Contains(discoveredAssemblies, a => a.FullName.Equals(assemblies[2].FullName));
        }
        /// <summary>
        /// Fors the dependency context.
        /// </summary>
        /// <param name="dependencyContext">The dependency context.</param>
        /// <param name="diagnosticSource">The diagnostic source.</param>
        /// <returns>Func&lt;IHostBuilder, IRocketHostBuilder&gt;.</returns>
        public static Func <IHostBuilder, IRocketHostBuilder> ForDependencyContext(
            DependencyContext dependencyContext,
            DiagnosticSource diagnosticSource = null)
        {
            return(builder =>
            {
                var b = RocketHostExtensions.GetOrCreateBuilder(builder);
                if (diagnosticSource != null)
                {
                    b = b.With(diagnosticSource);
                }

                var logger = new DiagnosticLogger(b.DiagnosticSource);
                var assemblyCandidateFinder = new DependencyContextAssemblyCandidateFinder(dependencyContext, logger);
                var assemblyProvider = new DependencyContextAssemblyProvider(dependencyContext, logger);
                var scanner = new SimpleConventionScanner(assemblyCandidateFinder, b.ServiceProperties, logger);
                return RocketHostExtensions.Swap(b, b
                                                 .With(assemblyCandidateFinder)
                                                 .With(assemblyProvider)
                                                 .With(scanner)
                                                 );
            });
        }
        /// <summary>
        /// Gets the or create builder.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>RocketHostBuilder.</returns>
        internal static RocketHostBuilder GetOrCreateBuilder(IHostBuilder builder)
        {
            if (!Builders.TryGetValue(builder, out var conventionalBuilder))
            {
                var diagnosticSource          = new DiagnosticListener("Rocket.Surgery.Hosting");
                var dependencyContext         = DependencyContext.Default;
                var logger                    = new DiagnosticLogger(diagnosticSource);
                var serviceProviderDictionary = new ServiceProviderDictionary(builder.Properties);
                serviceProviderDictionary.Set <ILogger>(logger);
                serviceProviderDictionary.Set(HostType.Live);
                var assemblyCandidateFinder = new DependencyContextAssemblyCandidateFinder(dependencyContext, logger);
                var assemblyProvider        = new DependencyContextAssemblyProvider(dependencyContext, logger);
                var scanner = new SimpleConventionScanner(assemblyCandidateFinder, serviceProviderDictionary, logger);
                conventionalBuilder = new RocketHostBuilder(
                    builder,
                    scanner,
                    assemblyCandidateFinder,
                    assemblyProvider,
                    diagnosticSource,
                    serviceProviderDictionary
                    );

                conventionalBuilder.Set(
                    new ConfigurationOptions
                {
                    ApplicationConfiguration =
                    {
                        b => b.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true),
                        b => b.AddYamlFile("appsettings.yml",  optional: true, reloadOnChange: true),
                        b => b.AddYamlFile("appsettings.yaml", optional: true, reloadOnChange: true),
                        b => b.AddIniFile("appsettings.ini",   optional: true, reloadOnChange: true)
                    },
                    EnvironmentConfiguration =
                    {
                        (b, environmentName) => b.AddJsonFile(
                            $"appsettings.{environmentName}.json",
                            optional: true,
                            reloadOnChange: true
                            ),
                        (b, environmentName) => b.AddYamlFile(
                            $"appsettings.{environmentName}.yml",
                            optional: true,
                            reloadOnChange: true
                            ),
                        (b, environmentName) => b.AddYamlFile(
                            $"appsettings.{environmentName}.yaml",
                            optional: true,
                            reloadOnChange: true
                            ),
                        (b, environmentName) => b.AddIniFile(
                            $"appsettings.{environmentName}.ini",
                            optional: true,
                            reloadOnChange: true
                            )
                    }
                }
                    );

                var host = new RocketContext(builder);
                builder
                .ConfigureHostConfiguration(host.ComposeHostingConvention)
                .ConfigureHostConfiguration(host.CaptureArguments)
                .ConfigureHostConfiguration(host.ConfigureCli)
                .ConfigureAppConfiguration(host.ReplaceArguments)
                .ConfigureAppConfiguration(host.ConfigureAppConfiguration)
                .ConfigureServices(host.ConfigureServices)
                .ConfigureServices(host.DefaultServices);
                Builders.Add(builder, conventionalBuilder);
            }

            return(conventionalBuilder);
        }