Asp.net 核心 2.1 到 Asp.net 3.0 升级

Asp.net core 2.1 to Asp.net 3.0 upgrade

我有一个 Web api 应用程序,它在 2.1 中运行良好。 我在 windows 上使用相同的应用程序在 IIS 中托管,在 linux 上不使用 IIS。 现在我正在尝试升级应用程序。

我已经升级了 nuget 包和项目版本 successfully.Now 当尝试调试应用程序时看起来我的配置启动有一些问题 class 如下所示

public static void Main(string[] args)
{

            StartupShutdownHandler.BuildWebHost(args).Build().Run();
}


namespace MyApp
{
    public class StartupShutdownHandler
    {

        public static IWebHostBuilder BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args)
               .UseStartup<StartupShutdownHandler>();

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

        public StartupShutdownHandler(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.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; }).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters(); //this is changed in 3.0
            services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; }).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);

            CorsRelatedPolicyAddition(services);
        }

        private void CorsRelatedPolicyAddition(IServiceCollection services)
        {
            var lstofCors = ConfigurationHandler.GetSection<List<string>>(StringConstants.AppSettingsKeys.CorsWhitelistedUrl);
            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(); });
                });

            }
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

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


        private void OnShutdown()
        {
             Logger.Debug("Application Shutdown");
        }

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

我试过修改一些注释为 //this is changed in 3.0 的行,但它不起作用。 请确定问题所在

以下更改最终适用于 2.1 到 3.0 路径。 我正在做的一项手动更改是在所有不会中断的地方将 newtonsoft 更新为新的内置 json 类型 (例如,对于一种情况,我仍然必须使用 newtonsoft,我正在序列化请求的 Formcollection 和 QueryCollection)

namespace MyApp.Interfaces
{
    public class StartupShutdownHandler
    {

        public static IWebHostBuilder BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args).
            ConfigureKestrel(serverOptions =>{}).UseIISIntegration()
            .UseStartup<StartupShutdownHandler>();

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


        public StartupShutdownHandler(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.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddControllers(options => options.RespectBrowserAcceptHeader = true).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters(); //updated            
            CorsRelatedPolicyAddition(services);
        }

        private void CorsRelatedPolicyAddition(IServiceCollection services)
        {
            var lstofCors = ConfigurationHandler.GetSection<List<string>>(StringConstants.AppSettingsKeys.CorsWhitelistedUrl);
            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.UseRouting();
            app.UseCors(MyAllowSpecificOrigins);
            app.UseEndpoints(endpoints =>
            {                
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
            });            
            applicationLifetime.ApplicationStarted.Register(StartedApplication);
            applicationLifetime.ApplicationStopping.Register(OnShutdown);
        }


        private void OnShutdown()
        {
             Logger.Debug("Application Ended");
        }

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