Esempio n. 1
0
        public RuntimeInstance(Assembly functionAppConfigurationAssembly,
                               Action <IServiceCollection, ICommandRegistry> beforeServiceProviderBuild,
                               Action <IServiceProvider, ICommandRegistry> afterServiceProviderBuild)
        {
            // Find the configuration implementation and service collection
            IFunctionAppConfiguration configuration     = LocateConfiguration(functionAppConfigurationAssembly);
            IContainerProvider        containerProvider =
                // ReSharper disable once SuspiciousTypeConversion.Global - externally provided
                (configuration as IContainerProvider) ?? new DefaultContainerProvider();

            ServiceCollection = containerProvider.CreateServiceCollection();
            CommandingDependencyResolverAdapter adapter = new CommandingDependencyResolverAdapter(
                (fromType, toInstance) => ServiceCollection.AddSingleton(fromType, toInstance),
                (fromType, toType) => ServiceCollection.AddTransient(fromType, toType),
                (resolveType) => ServiceProvider.GetService(resolveType)
                );

            ICommandRegistry commandRegistry;

            // ReSharper disable once SuspiciousTypeConversion.Global - externally provided
            if (configuration is ICommandingConfigurator commandingConfigurator)
            {
                commandRegistry = commandingConfigurator.AddCommanding(adapter);
            }
            else
            {
                CommandingRuntime commandingRuntime = new CommandingRuntime();
                commandRegistry = commandingRuntime.AddCommanding(adapter);
            }

            // Register internal implementations
            RegisterInternalImplementations();

            // Invoke the builder process
            FunctionHostBuilder builder = CreateBuilderFromConfiguration(commandRegistry, configuration);

            Builder = builder;
            FunctionBuilder functionBuilder = (FunctionBuilder)builder.FunctionBuilder;

            SetupAuthorization(builder, functionBuilder);

            RegisterCoreDependencies(builder.FunctionDefinitions);

            RegisterTimerCommandFactories(builder.FunctionDefinitions);

            RegisterHttpDependencies(builder.FunctionDefinitions);

            RegisterCosmosDependencies(builder.FunctionDefinitions);

            beforeServiceProviderBuild?.Invoke(ServiceCollection, commandRegistry);
            ServiceProvider = containerProvider.CreateServiceProvider(ServiceCollection);
            afterServiceProviderBuild?.Invoke(ServiceProvider, commandRegistry);

            builder.ServiceProviderCreatedAction?.Invoke(ServiceProvider);
        }