コード例 #1
0
        public void IsWarmUpRequest_ReturnsExpectedValue()
        {
            var environment     = new TestEnvironment();
            var hostEnvironment = new ScriptWebHostEnvironment(environment);
            var request         = HttpTestHelpers.CreateHttpRequest("POST", "http://azure.com/api/warmup");

            Assert.False(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            // Reset environment
            environment.Clear();
            hostEnvironment = new ScriptWebHostEnvironment(environment);

            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
            Assert.False(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId, "12345");
            Assert.True(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            request = HttpTestHelpers.CreateHttpRequest("POST", "http://azure.com/api/csharphttpwarmup");
            Assert.True(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            request = HttpTestHelpers.CreateHttpRequest("POST", "http://azure.com/api/warmup");
            request.Headers.Add(ScriptConstants.AntaresLogIdHeaderName, "xyz123");
            Assert.False(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            request = HttpTestHelpers.CreateHttpRequest("POST", "http://azure.com/api/foo");
            Assert.False(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            // Reset environment
            environment.Clear();
            hostEnvironment = new ScriptWebHostEnvironment(environment);

            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
            Assert.False(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));

            request = HttpTestHelpers.CreateHttpRequest("POST", "http://azure.com/api/warmup");
            environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerName, "TestContainer");
            Assert.True(environment.IsLinuxConsumption());
            Assert.True(HostWarmupMiddleware.IsWarmUpRequest(request, hostEnvironment, environment));
        }
コード例 #2
0
        public static IApplicationBuilder UseWebJobsScriptHost(this IApplicationBuilder builder, IApplicationLifetime applicationLifetime, Action <WebJobsRouteBuilder> routes)
        {
            IEnvironment environment = builder.ApplicationServices.GetService <IEnvironment>() ?? SystemEnvironment.Instance;
            IOptionsMonitor <StandbyOptions>         standbyOptions         = builder.ApplicationServices.GetService <IOptionsMonitor <StandbyOptions> >();
            IOptionsMonitor <HttpBodyControlOptions> httpBodyControlOptions = builder.ApplicationServices.GetService <IOptionsMonitor <HttpBodyControlOptions> >();
            IServiceProvider serviceProvider = builder.ApplicationServices;

            builder.UseMiddleware <HttpRequestBodySizeMiddleware>();
            builder.UseMiddleware <SystemTraceMiddleware>();
            builder.UseMiddleware <HostnameFixupMiddleware>();
            if (environment.IsLinuxConsumption())
            {
                builder.UseMiddleware <EnvironmentReadyCheckMiddleware>();
            }

            if (standbyOptions.CurrentValue.InStandbyMode)
            {
                builder.UseMiddleware <PlaceholderSpecializationMiddleware>();
            }

            // Specialization can change the CompatMode setting, so this must run later than
            // the PlaceholderSpecializationMiddleware
            builder.UseWhen(context => httpBodyControlOptions.CurrentValue.AllowSynchronousIO || context.Request.IsAdminDownloadRequest(), config =>
            {
                config.UseMiddleware <AllowSynchronousIOMiddleware>();
            });

            // This middleware must be registered before we establish the request service provider.
            builder.UseWhen(context => !context.Request.IsAdminRequest(), config =>
            {
                config.UseMiddleware <HostAvailabilityCheckMiddleware>();
            });

            builder.UseWhen(context => HostWarmupMiddleware.IsWarmUpRequest(context.Request, standbyOptions.CurrentValue.InStandbyMode, environment), config =>
            {
                config.UseMiddleware <HostWarmupMiddleware>();
            });

            // This middleware must be registered before any other middleware depending on
            // JobHost/ScriptHost scoped services.
            builder.UseMiddleware <ScriptHostRequestServiceProviderMiddleware>();

            if (environment.IsLinuxConsumption())
            {
                builder.UseMiddleware <AppServiceHeaderFixupMiddleware>();
            }

            builder.UseMiddleware <ExceptionMiddleware>();
            builder.UseWhen(HomepageMiddleware.IsHomepageRequest, config =>
            {
                config.UseMiddleware <HomepageMiddleware>();
            });
            builder.UseWhen(context => !context.Request.IsAdminRequest() && HttpThrottleMiddleware.ShouldEnable(serviceProvider), config =>
            {
                config.UseMiddleware <HttpThrottleMiddleware>();
            });

            builder.UseMiddleware <JobHostPipelineMiddleware>();
            builder.UseMiddleware <FunctionInvocationMiddleware>();

            // Register /admin/vfs, and /admin/zip to the VirtualFileSystem middleware.
            builder.UseWhen(VirtualFileSystemMiddleware.IsVirtualFileSystemRequest, config => config.UseMiddleware <VirtualFileSystemMiddleware>());

            // MVC routes (routes defined by Controllers like HostController, FunctionsController, ... must be added before functions/proxy routes so they are matched first and can not be overridden by functions or proxy routes)
            // source here: https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs
            builder.UseMvc();

            // Ensure the HTTP binding routing is registered after all middleware
            builder.UseHttpBindingRouting(applicationLifetime, routes);

            return(builder);
        }
        public static IApplicationBuilder UseWebJobsScriptHost(this IApplicationBuilder builder, IApplicationLifetime applicationLifetime, Action <WebJobsRouteBuilder> routes)
        {
            IEnvironment environment = builder.ApplicationServices.GetService <IEnvironment>() ?? SystemEnvironment.Instance;
            IOptionsMonitor <StandbyOptions>         standbyOptions         = builder.ApplicationServices.GetService <IOptionsMonitor <StandbyOptions> >();
            IOptionsMonitor <HttpBodyControlOptions> httpBodyControlOptions = builder.ApplicationServices.GetService <IOptionsMonitor <HttpBodyControlOptions> >();
            IOptionsMonitor <HttpOptions>            httpOptions            = builder.ApplicationServices.GetService <IOptionsMonitor <HttpOptions> >();

            builder.UseMiddleware <SystemTraceMiddleware>();
            builder.UseMiddleware <HostnameFixupMiddleware>();
            if (environment.IsLinuxConsumption())
            {
                builder.UseMiddleware <EnvironmentReadyCheckMiddleware>();
            }

            if (standbyOptions.CurrentValue.InStandbyMode)
            {
                builder.UseMiddleware <PlaceholderSpecializationMiddleware>();
            }

            // Specialization can change the CompatMode setting, so this must run later than
            // the PlaceholderSpecializationMiddleware
            builder.UseWhen(context => httpBodyControlOptions.CurrentValue.AllowSynchronousIO || context.Request.IsAdminDownloadRequest(), config =>
            {
                config.UseMiddleware <AllowSynchronousIOMiddleware>();
            });

            // This middleware must be registered before we establish the request service provider.
            builder.UseWhen(context => !context.Request.IsAdminRequest(), config =>
            {
                config.UseMiddleware <HostAvailabilityCheckMiddleware>();
            });

            builder.UseWhen(context => HostWarmupMiddleware.IsWarmUpRequest(context.Request, standbyOptions.CurrentValue.InStandbyMode, environment), config =>
            {
                config.UseMiddleware <HostWarmupMiddleware>();
            });

            // This middleware must be registered before any other middleware depending on
            // JobHost/ScriptHost scoped services.
            builder.UseMiddleware <ScriptHostRequestServiceProviderMiddleware>();

            if (environment.IsLinuxConsumption())
            {
                builder.UseMiddleware <AppServiceHeaderFixupMiddleware>();
            }

            builder.UseMiddleware <ExceptionMiddleware>();
            builder.UseWhen(HomepageMiddleware.IsHomepageRequest, config =>
            {
                config.UseMiddleware <HomepageMiddleware>();
            });
            builder.UseWhen(context => HttpThrottleMiddleware.ShouldEnable(httpOptions.CurrentValue) && !context.Request.IsAdminRequest(), config =>
            {
                config.UseMiddleware <HttpThrottleMiddleware>();
            });
            builder.UseMiddleware <ResponseContextItemsCheckMiddleware>();
            builder.UseMiddleware <JobHostPipelineMiddleware>();
            builder.UseMiddleware <FunctionInvocationMiddleware>();

            // Register /admin/vfs, and /admin/zip to the VirtualFileSystem middleware.
            builder.UseWhen(VirtualFileSystemMiddleware.IsVirtualFileSystemRequest, config => config.UseMiddleware <VirtualFileSystemMiddleware>());

            // Ensure the HTTP binding routing is registered after all middleware
            builder.UseHttpBindingRouting(applicationLifetime, routes);

            builder.UseMvc();

            return(builder);
        }