将Asp.net Core 2.1升级到Asp.net 3.0 [英] Asp.net core 2.1 to Asp.net 3.0 upgrade

查看:78
本文介绍了将Asp.net Core 2.1升级到Asp.net 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API应用程序,在2.1中可以正常工作。
我正在使用相同的应用程序在Windows上的IIS中托管,而在Linux上没有IIS。
现在我正在尝试升级应用程序。

I have a web api application which is working fine in 2.1. I am using same application to host in IIS on windows and without IIS on linux. Now I am trying to upgrade the application.

我已经成功升级了nuget软件包和项目版本。现在尝试调试应用程序时出现了一些问题在我的配置启动类中,如下所示

I have upgraded the nuget packages and project version successfully.Now when trying to debug app looks there is some problem in my congiruation startup class which is as below

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");
        }
    }
}

我试过将某些行弄皱注释为////这在3.0中已更改,但是它不起作用。
请确定问题

I have tried chagned some lines which are commented as //this is changed in 3.0 but it doesn't work. Please identify the problem

推荐答案

以下更改最终适用于2.1到3.0的路径。
我正在做的一项手动更改是在不破坏
的所有地方将newtonsoft更新为新的内置json类型(例如,在某些情况下,我仍必须使用newtonsoft来序列化Formcollection和QueryCollection

Following changes eventually work for 2.1 to 3.0 path. One manual change i am doing is updating newtonsoft to new builtin json type in all places where it doesn't break (e.g for one case i have to still use newtonsoft where i am serializing Formcollection and QueryCollection of the request)

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");
        }
    }
}

这篇关于将Asp.net Core 2.1升级到Asp.net 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆