以真实项目为例,从.NET Core 2.2迁移到.NET Core 3.1

图片



本文是对nopCommerce项目更新的逻辑延续,该项目是用于创建在线商店的免费开源CMS。上次我们讨论了将项目从ASP.NET MVC迁移到ASP.NET Core 2.2的经验现在,我们将研究向.NET Core 3.1的迁移过程。鉴于对.Net Core 3.1的官方支持将持续到2022年12月,因此迁移主题非常重要。因此,如果您想获得更新框架的所有好处,紧跟技术创新并紧跟不断增长的全球趋势,那么该开始迁移了。



过渡到.NET Core 3.1期间必须解决哪些任务



, . review .NET Core 3.0 , , , JSON. , .NET Core 2.2 . , . . , .NET Core 2.2.



.NET Core 3.1



. .NET Core 2.2 .NET Core 3.1 Microsoft. , .



Generic Host



.NET Core 2.1 Generic Host Web Host, , (DI) . .NET Core 3. Generic Host, Generic Host Builder Web Host Builder. , WPF - .



public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<Startup>();
            });
    }
}


WebHostBuilder, ASP.NET Core 3.1, .





global.json



.NET Core 2.0, . SDK, . SDK .NET Core 3.0 allowPrerelease rollForward. .NET Core SDK.



{
  "sdk": {
    "version": "3.1.201",
    "rollForward": "latestFeature",
    "allowPrerelease": false
  }
}


GitHub.



, , global.json . , . , SDK , , .



ASP.NET Core Module V2



.NET Core 2.2 IIS .NET Core, Kestrel ( - .NET Core) IIS Kestrel. IIS . , , IIS Kestrel . «OutOfProcess».





.NET Core 2.2 ​​ «InProcess». IIS Kestrel, IIS. , Kestrel. , .





.NET Core 3.1 , ASP.NET Core IIS. .



<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Copyright>Copyright (c) Nop Solutions, Ltd</Copyright>
    <Company>Nop Solutions, Ltd</Company>
    <Authors>Nop Solutions, Ltd</Authors>
    <Version>4.4.0.0</Version>
    <Description>Nop.Web is also an MVC web application project, a presentation layer for public store and admin area.</Description>
    <PackageLicenseUrl>https://www.nopcommerce.com/license</PackageLicenseUrl>
    <PackageProjectUrl>https://www.nopcommerce.com/</PackageProjectUrl>
    <RepositoryUrl>https://github.com/nopSolutions/nopCommerce</RepositoryUrl>
    <RepositoryType>Git</RepositoryType>
    <!--Set this parameter to true to get the dlls copied from the NuGet cache to the output of your project-->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <!--When true, compiles and emits the Razor assembly as part of publishing the project-->
    <RazorCompileOnPublish>false</RazorCompileOnPublish>
  </PropertyGroup>


, Linux -.

, , , .



Endpoint Routing



.NET Core 2.1 Middleware ( ASP.NET Core MVC) HTTP-. , , , , , MVC . .NET Core 2.2 , (Endpoints), .

.NET Core 3.1 Endpoint Routing , . middleware:



  • EndpointRoutingMiddleware — , URL ,
  • EndpointMiddleware




, , , middleware , .



/// <summary>
/// Configure Endpoints routing
/// </summary>
/// <param name="application">Builder for configuring an application's request pipeline</param>
public static void UseNopEndpoints(this IApplicationBuilder application)
{
    //Add the EndpointRoutingMiddleware
    application.UseRouting();

    //Execute the endpoint selected by the routing middleware
    application.UseEndpoints(endpoints =>
    {
        //register all routes
        EngineContext.Current.Resolve<IRoutePublisher>().RegisterRoutes(endpoints);
    });
}


, .

GitHub.



# 8.0



.NET Core C# 8.0. . , , “ ”.





.





nopCommerce, , .. eCommerce .



Windows 10 (10.0.19041.388), IIS 10 (10.0.19041.1) - - Kestrel . Apache JMeter, . , 50 , — 516 , — 50 , — 80 1 5 . MS SQL Server 2017 (14.0.2014.14).



JMeter , . .



20% ( )





(Average) 13.7% ( )





, .. (throughput) 12.7% ( )





, , , . , . ASP.NET Core .





— 1,5 . , 2 . .



, AspNetCoreModule, .NET Core 2.2. .





, .







.NET Core - . , , . .NET Core , . 13%. , , , . , , performance refactoring : .



, , , .NET Core , , .NET Core 3.1.0 ( 3.5.1) — security patch.



, .NET Core 3.1 LTS 2.1, , . LTS .NET Core. .NET 5, .NET Core 3.1 — .



将来,我们计划进一步更新nopCommerce应用程序,以添加.NET Core平台提供的越来越多的新功能。其中之一是使用System.Text.Json而不是Newtonsoft.Json的举动这是一种处理JSON对象的性能更高,更安全,更标准化的方法。我们还计划实现和使用C#8.0提供的尽可能多的功能。



您可以通过nopcommerce.com或访问GitHub上的存储库来了解有关我们项目的更多信息




All Articles