ASP.NET Core 3.1。将请求降落到服务器的延迟,直到达到执行为止

时间:2019-12-23 07:14:00

标签: asp.net-core-3.1

我有一个asp.net core 3.1应用程序,正在调查延迟情况。 我正在观察的是请求到达服务器后直到我的动作之前的延迟 我的应用程序代码可以在以下位置找到 https://github.com/aspnet/AspNetCore/issues/17985 请让我知道我应该做些什么以避免这种延迟 我当前的启动代码是

using log4net;
using log4net.Appender;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace MyApp
{
    public class Startup
    {



        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private const string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";


        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
             services.AddControllers(options => options.RespectBrowserAcceptHeader = true).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();//for twilio            
            CorsRelatedPolicyAddition(services);
            services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });
        }

        private void CorsRelatedPolicyAddition(IServiceCollection services)
        {
            List<string> lstofCors = new List<string>();//from some configruation
            if (lstofCors != null && lstofCors.Count > 0 && lstofCors.Any(h => !string.IsNullOrWhiteSpace(h)))
            {
                services.AddCors(options =>
                {
                    options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.WithOrigins(lstofCors.ToArray()).AllowAnyMethod().AllowAnyHeader(); });
                });

            }
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLifetime)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMiddleware<RequestLoggingMiddleware>();
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseCors(MyAllowSpecificOrigins);

            app.UseForwardedHeaders();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
            });

            //app.UseMvc(); //this is changed in 3.0
            applicationLifetime.ApplicationStarted.Register(StartedApplication);
            applicationLifetime.ApplicationStopping.Register(OnShutdown);
        }


        private void OnShutdown()
        {
            try
            {
                Logger.Debug("Flushing out messages");

                var rep = LogManager.GetRepository(Assembly.GetEntryAssembly());
                foreach (var appender in rep.GetAppenders())
                {
                    var buffered = appender as BufferingAppenderSkeleton;
                    if (buffered != null)
                    {
                        buffered.Flush();
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Fatal(ex, ex);
            }
        }

        private void StartedApplication()
        {
            Logger.Debug("Application Started");
        }
    }
}

0 个答案:

没有答案