Exemplo n.º 1
0
        private IFunctionsRequestHandler BuildFunctionsRequestHandler(IFunctionsHostBuilder builder)
        {
            IWebHostEnvironment webHostEnv;

            using (var functionsServiceProvider = builder.Services.BuildServiceProvider())
            {
                var functionsHostingEnv = functionsServiceProvider.GetRequiredService <IHostEnvironment>();

                webHostEnv = CreateWebHostEnvironment(functionsHostingEnv);
            }

            var config = CreateConfiguration(webHostEnv);

            ServiceCollection applicationServices = CreateBasicApplicationServices(webHostEnv, config);

            // build service collection used for creating an instance of TStartup.
            var startupServices = new ServiceCollection();

            startupServices.AddSingleton(config);
            startupServices.AddSingleton(webHostEnv);
            startupServices.AddSingleton <IHostEnvironment>(webHostEnv);

            TStartup startupInstance;

            using (var startupServiceProvider = startupServices.BuildServiceProvider())
            {
                startupInstance = ActivatorUtilities.CreateInstance <TStartup>(startupServiceProvider);
            }

            var startupMethods = typeof(TStartup).GetMethods().ToList();

            // get ConfigureServices method of TStartup
            var configureServicesMethod = startupMethods
                                          .SingleOrDefault(mi => string.Equals(mi.Name, "ConfigureServices", StringComparison.InvariantCulture));

            // invoke ConfigureService method from instance of TStartUp if exists.
            var configureServicesResult = configureServicesMethod?.Invoke(startupInstance, new object[] { applicationServices });

            // if the ConfigureService method returns a ServiceProvider we use this provider for the application. If not, we build it :)
            var applicationServiceProvider = configureServicesResult as IServiceProvider ?? applicationServices.BuildServiceProvider();

            var applicationBuilder = new ApplicationBuilder(applicationServiceProvider, new FeatureCollection());

            var configureMethod = startupMethods
                                  .SingleOrDefault(mi => string.Equals(mi.Name, "Configure", StringComparison.InvariantCulture));

            if (configureMethod != null)
            {
                InvokeConfigure(configureMethod, startupInstance, applicationBuilder);
            }

            var requestDelegate        = applicationBuilder.Build();
            var functionRequestHandler = new FunctionsRequestHandler(applicationBuilder.ApplicationServices, requestDelegate);

            return(functionRequestHandler);
        }
        public static IHostBuilder ConfigureWebAppFunctionHost <TStartup>(this IHostBuilder builder) where TStartup : class
        {
            try
            {
                var factory = new WebApplicationFactory <TStartup>();
                var client  = factory.CreateClient(new WebApplicationFactoryClientOptions
                {
                    AllowAutoRedirect = false
                });

                var requestHandler = new FunctionsRequestHandler(client);

                return(builder.ConfigureServices(services =>
                {
                    services.AddSingleton <IFunctionsRequestHandler>(requestHandler);
                }));
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);

                // log startup exceptions to application insights if instrumentation key is available.
                var appInsightsInstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
                if (!string.IsNullOrWhiteSpace(appInsightsInstrumentationKey))
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    // TODO: Check if the telemetry client can be resolved somehow.
                    var telemetryClient = new TelemetryClient {
                        InstrumentationKey = appInsightsInstrumentationKey
                    };
#pragma warning restore CS0618 // Type or member is obsolete

                    telemetryClient.TrackException(e);
                }

                throw;
            }
        }