zoukankan      html  css  js  c++  java
  • .Net Core2.2升级.Net Core3.1采坑日记

    一、WebApi
    1、Swagger的一个小坑(升级Swashbuckle.AspNetCore  4.0.1=>5.2.1)

    .Net Core2.2  Startup.cs
     //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "API接口", Description = "接口", Version = "v1" }); // 为 Swagger JSON and UI设置xml文档注释路径 var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径) var xmlPath = Path.Combine(basePath, "xxx.xml"); c.IncludeXmlComments(xmlPath); });
    .Net Core3.1  Startup.cs
     //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "API接口", Description = "接口", Version = "v1" }); // 为 Swagger JSON and UI设置xml文档注释路径 var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径) var xmlPath = Path.Combine(basePath, "xxx.xml"); c.IncludeXmlComments(xmlPath); });

    2、Json格式设置的坑

    .Net Core2.2  Startup.cs
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddControllersAsServices().AddJsonOptions
                (
                    json =>
                    {
                        //统一设置JsonResult
                        json.SerializerSettings.ContractResolver = new DefaultContractResolver();
                        json.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                    }
                );
    .Net Core3.1 Startup.cs
    添加包 Microsoft.AspNetCore.Mvc.NewtonsoftJson services.AddMvc(option
    => { option.EnableEndpointRouting = false; }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0).AddControllersAsServices().AddNewtonsoftJson ( json => { //统一设置JsonResult json.SerializerSettings.ContractResolver = new DefaultContractResolver(); json.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; } );

    二、Web

    1、AddMvc的坑

    .Net Core2.2
    services.AddMvc(option =>
                {
                    option.Filters.Add<IdentityAuthorizeFilter>();
                    option.Filters.Add<ExceptionFillters>();
                });
    .Net Core3.1
    services.AddControllersWithViews(option => {
                    option.Filters.Add<IdentityAuthorizeFilter>();
                    option.Filters.Add<ExceptionFillters>();
                });

    2、Autofac的坑,.net core3.1不支持IServiceProvider

    .Net Core2.2 Startup.cs
    public IServiceProvider ConfigureServices(IServiceCollection services)
            {
               #region 其他设置
               #endregion
    
                var builder = new ContainerBuilder();
                builder.Populate(services);
    
                //告诉autofac框架注册Service层所在程序集中的所有类的对象实例到IService层所在程序集中的所有类的对象实例
                Assembly iserAss = Assembly.Load("CBest.AFS.Interface");
                Assembly serAss = Assembly.Load("CBest.AFS.Service");
                //创建serAss中的所有类的instance以此类的实现接口存储
                builder.RegisterAssemblyTypes(iserAss, serAss).AsImplementedInterfaces().PropertiesAutowired();//支持构造方式注入和属性方式注入,不加PropertiesAutowired则只支持构造方式
    
                //注入到控制器
                var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
                    .Where(type => typeof(Controller).IsAssignableFrom(type)).ToArray();
                builder.RegisterTypes(controllersTypesInAssembly).PropertiesAutowired();
                //创建一个Autofac的容器
                var container = builder.Build();
                return new AutofacServiceProvider(container);
    }    
    .Net Core3.1 Startup.cs中新增以下方法
    //依赖注入
            public void ConfigureContainer(ContainerBuilder builder)
            {
                Assembly iserAss = Assembly.Load("XXX.Interface");
                Assembly serAss = Assembly.Load("XXX.Service");
                //创建serAss中的所有类的instance以此类的实现接口存储
                builder.RegisterAssemblyTypes(iserAss, serAss).AsImplementedInterfaces().PropertiesAutowired();//支持构造方式注入和属性方式注入,不加PropertiesAutowired则只支持构造方式
    
                //注入到控制器
                var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
                    .Where(type => typeof(Controller).IsAssignableFrom(type)).ToArray();
                builder.RegisterTypes(controllersTypesInAssembly).PropertiesAutowired();
            }

    3、授权和身份认证的坑

    .Net Core2.2  Program.cs
    public static void Main(string[] args)
            {
                NLogBuilder.ConfigureNLog("nlog.config");
    
                CreateWebHostBuilder(args).UseUrls("http://*:8011").Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                       .UseStartup<Startup>();
    .Net Core3.1 Program.cs
    public static void Main(string[] args)
            {
                NLogBuilder.ConfigureNLog("nlog.config");
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>().UseUrls("http://*:8011");
                    });
    .Net Core2.2 Startup.cs Configure函数
    app.UseAuthentication()
    app.UseMvc(routes => routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}"));
    .Net Core3.1 Startup.cs Configure函数
    app.UseAuthorization();
    app.UseAuthentication();
    app.UseRouting(); app.UseEndpoints(endpoints
    => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
    .Net Core2.2 
    await HttpContext.SignInAsync(pricipal);
    .Net Core3.1
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, pricipal);

      await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, pricipal, new AuthenticationProperties()
      {
        IsPersistent = true,//是否持久化
        ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12),//过期时间设置
        AllowRefresh = true //是否允许刷新??
      });

     
  • 相关阅读:
    JAVA网络编程入门
    悲观锁和乐观锁
    原子性---Atomic
    volatile关键字
    leetcode_111. 二叉树的最小深度
    leetcode_110. 平衡二叉树
    leetcode_108. 将有序数组转换为二叉搜索树
    leetcode_107. 二叉树的层次遍历 II
    leetcode_104. 二叉树的最大深度
    leetcode_101. 对称二叉树
  • 原文地址:https://www.cnblogs.com/jianghaidong/p/12620146.html
Copyright © 2011-2022 走看看