.NET Core 2.2升级到3.1过程中有哪些潜在问题需要注意,如何避免踩坑?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1196个文字,预计阅读时间需要5分钟。
写在前端+微软在更新.Net Core版本时,动作频频,每次更新版本的时间都让人心生忧虑,其实坑实在太多。往往是慢慢发现某个功能或组件被移除了,或者不支持某些方法。
写在前面
微软在更新.Net Core版本的时候,动作往往很大,使得每次更新版本的时候都得小心翼翼,坑实在是太多。往往是悄咪咪的移除了某项功能或者组件,或者不在支持XX方法,这就很花时间去找回需要的东西了,下面是个人在迁移.Net Core WebApi项目过程中遇到的问题汇总:
开始迁移
1. 修改*.csproj项目文件
<TargetFramework>netcoreapp2.2</TargetFramework> 修改为 <TargetFramework>netcoreapp3.1</TargetFramework>
2 修改Program
public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>().ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile($"你的json文件.json", optional: true, reloadOnChange: true); } );
修改为
public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>() .ConfigureAppConfiguration((hostingContext, config)=> { config.AddJsonFile($"你的json文件.json", optional: true, reloadOnChange: true); }); });
3.1 修改Startup.ConfigureServices
services.AddMvc(); 修改为 services.AddControllers();
3.2 修改Startup.Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env) 修改为 using Microsoft.Extensions.Hosting; public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
IHostingEnvironment在3.0之后已被标记弃用。
路由配置:
app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}" ); }); 修改为 app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
你以为结束了?还没。
坑
这时候你以为结束了,兴高采烈的去服务器装好runningTime和hosting相应的版本,运行……
HTTP Error 500.30 – ANCM In-Process Start Failure
直接cmd,进入到发布目录,执行:
E:\你的路径>dotnet xxx.dll
显示详细错误
而我的相应250代码行是:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
搜索最新的AutoMapper根本没更新或改变,所以不是这个组件的问题。
尝试下载补丁Windows6.1-KB974405-x64.msu,无果……
卸载sdk重置,无果……
修改web.config,无果……
修改应用池32位,无果……
最后,查看发布:勾选上,解决……
Endpoint contains CORS metadata, but a middleware was not found that supports CORS.
顺利可以启动项目之后,发现有些接口:
2020-06-29 10:02:23,357 [14] ERROR System.String - 全局异常捕捉:异常:Endpoint contains CORS metadata, but a middleware was not found that supports CORS.
Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).
提示很明显,在.net core 2.2 的时候
app.UseCors();
不是需要强制在指定位置的,在3.0之后需要设置在app.UseRouting和app.UseEndpoints 之间
app.UseRouting();//跨域 app.UseCors(one); app.UseCors(two); …… app.UseEndpoints(endpoints => ……
The JSON value could not be converted to System.Int32. Path……
运行之后,有些接口没有数据返回,而有些直接报错了。原因又是爸爸把Newtonsoft.Json移除,使用内置的System.Text.Json,所以依赖于Newtonsoft.Json的组件将不可用,那么,只能手动添加。
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.5
然后添加引用
public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(); }
目前还不太建议你使用内置的序列化,因为实在太多功能或方法不支持,详细对比请参考docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to
授权相关
基于策略授权,我想在座的加班狗都是大同小异,在2.2以前:
public class PolicyHandler : AuthorizationHandler<PolicyRequirement> { /// <summary> /// 授权方式(cookie, bearer, oauth, openid) /// </summary> public IAuthenticationSchemeProvider Schemes { get; set; } private IConfiguration _configuration; /// <summary> /// ctor /// </summary> /// <param name="configuration"></param> /// <param name="schemes"></param> /// <param name="jwtApp"></param> public PolicyHandler(IConfiguration configuration, IAuthenticationSchemeProvider schemes) { Schemes = schemes; _jwtApp = jwtApp; _configuration = configuration; } /// <summary> /// 授权处理 /// </summary> /// <param name="context"></param> /// <param name="requirement"></param> protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PolicyRequirement requirement) { var docs.microsoft.com/zh-cn/aspnet/core/migration/22-to-30?view=aspnetcore-2.1&tabs=visual-studio
作者:EminemJK(山治先生)
出处:www.cnblogs.com/EminemJK/
到此这篇关于.Net Core 2.2升级3.1的避坑指南(小结)的文章就介绍到这了,更多相关.Net Core 2.2升级3.1内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计1196个文字,预计阅读时间需要5分钟。
写在前端+微软在更新.Net Core版本时,动作频频,每次更新版本的时间都让人心生忧虑,其实坑实在太多。往往是慢慢发现某个功能或组件被移除了,或者不支持某些方法。
写在前面
微软在更新.Net Core版本的时候,动作往往很大,使得每次更新版本的时候都得小心翼翼,坑实在是太多。往往是悄咪咪的移除了某项功能或者组件,或者不在支持XX方法,这就很花时间去找回需要的东西了,下面是个人在迁移.Net Core WebApi项目过程中遇到的问题汇总:
开始迁移
1. 修改*.csproj项目文件
<TargetFramework>netcoreapp2.2</TargetFramework> 修改为 <TargetFramework>netcoreapp3.1</TargetFramework>
2 修改Program
public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>().ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile($"你的json文件.json", optional: true, reloadOnChange: true); } );
修改为
public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>() .ConfigureAppConfiguration((hostingContext, config)=> { config.AddJsonFile($"你的json文件.json", optional: true, reloadOnChange: true); }); });
3.1 修改Startup.ConfigureServices
services.AddMvc(); 修改为 services.AddControllers();
3.2 修改Startup.Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env) 修改为 using Microsoft.Extensions.Hosting; public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
IHostingEnvironment在3.0之后已被标记弃用。
路由配置:
app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}" ); }); 修改为 app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
你以为结束了?还没。
坑
这时候你以为结束了,兴高采烈的去服务器装好runningTime和hosting相应的版本,运行……
HTTP Error 500.30 – ANCM In-Process Start Failure
直接cmd,进入到发布目录,执行:
E:\你的路径>dotnet xxx.dll
显示详细错误
而我的相应250代码行是:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
搜索最新的AutoMapper根本没更新或改变,所以不是这个组件的问题。
尝试下载补丁Windows6.1-KB974405-x64.msu,无果……
卸载sdk重置,无果……
修改web.config,无果……
修改应用池32位,无果……
最后,查看发布:勾选上,解决……
Endpoint contains CORS metadata, but a middleware was not found that supports CORS.
顺利可以启动项目之后,发现有些接口:
2020-06-29 10:02:23,357 [14] ERROR System.String - 全局异常捕捉:异常:Endpoint contains CORS metadata, but a middleware was not found that supports CORS.
Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).
提示很明显,在.net core 2.2 的时候
app.UseCors();
不是需要强制在指定位置的,在3.0之后需要设置在app.UseRouting和app.UseEndpoints 之间
app.UseRouting();//跨域 app.UseCors(one); app.UseCors(two); …… app.UseEndpoints(endpoints => ……
The JSON value could not be converted to System.Int32. Path……
运行之后,有些接口没有数据返回,而有些直接报错了。原因又是爸爸把Newtonsoft.Json移除,使用内置的System.Text.Json,所以依赖于Newtonsoft.Json的组件将不可用,那么,只能手动添加。
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.5
然后添加引用
public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(); }
目前还不太建议你使用内置的序列化,因为实在太多功能或方法不支持,详细对比请参考docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to
授权相关
基于策略授权,我想在座的加班狗都是大同小异,在2.2以前:
public class PolicyHandler : AuthorizationHandler<PolicyRequirement> { /// <summary> /// 授权方式(cookie, bearer, oauth, openid) /// </summary> public IAuthenticationSchemeProvider Schemes { get; set; } private IConfiguration _configuration; /// <summary> /// ctor /// </summary> /// <param name="configuration"></param> /// <param name="schemes"></param> /// <param name="jwtApp"></param> public PolicyHandler(IConfiguration configuration, IAuthenticationSchemeProvider schemes) { Schemes = schemes; _jwtApp = jwtApp; _configuration = configuration; } /// <summary> /// 授权处理 /// </summary> /// <param name="context"></param> /// <param name="requirement"></param> protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PolicyRequirement requirement) { var docs.microsoft.com/zh-cn/aspnet/core/migration/22-to-30?view=aspnetcore-2.1&tabs=visual-studio
作者:EminemJK(山治先生)
出处:www.cnblogs.com/EminemJK/
到此这篇关于.Net Core 2.2升级3.1的避坑指南(小结)的文章就介绍到这了,更多相关.Net Core 2.2升级3.1内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

