具有将Net Framework的遗留企业项目移植到Net Core的经验

具有将Net Framework的遗留企业项目移植到Net Core的经验



网络框架到网络核心



入门部分



我将尝试提供有关如何轻松将现有Enterprise C .Net Framework解决方案移植到.Net Core的信息。我将遍历所有重要部分,并且不会深入讨论以免增加文章的篇幅,下面将有许多指向Microsoft部分的链接,但首先的想法是让您了解如何移植系统的特定部分,以便您可以在评论中讨论。一般而言,本文可以被认为是关于膝盖的手册。



我们有什么



给定:企业系统,它使用以下技术编写(所有内容均在Net Framework下用C#编写):



  • 很多ASMX Web服务
  • 许多WCF服务
  • 工作流基础上的后台任务
  • WebForms和部分ASP.NET MVC 4上的Web应用程序
  • SQL Server Reporting Services报表
  • Windows窗体实用程序和控制台应用程序


Net Core:



  • ( , )
  • NetCore: , ,


.NET Core .NET Framework , .



, — , . , . , .



, (Framework Core), NetStandard ( — netstandard2.0).



, NetCore, :



  • Windows Communication Foundation. API , API
  • Workflow Foundation
  • ASP.NET Web Forms ( System.Web)
  • API MSMQ


API , , , . .



, ,



Portatibility Analyzer



Portatibility Analyzer target' (NetCore, NetFramework, NetStandard, Mono, ...) , , API . Visual Studio.



便携性分析仪





( #if) , API .Net Framewrk .Net Core.



#if:



  • API namespace' .Net Core
  • API
  • .Net Core


, :



: Net Framework NetStandard NetCore:



#if NETFRAMEWORK
#elif NETSTANDARD || NETCOREAPP
#endif


NetCore NetFramework ( , API, .NetStandard2.0):



#if NETFRAMEWORK
#elif NETCOREAPP
#endif


, runtime' .



Target' *.csproj :



    <TargetFrameworks>netstandard2.0;net471;netcoreapp3.1</TargetFrameworks>


→ 



API



API, NetStandard NetCore . , , WCF, , .



, WCF TransactionFlowAttribute, , , , :



#if NETFRAMEWORK
#elif NETSTANDARD || NETCOREAPP
namespace System.ServiceModel
{
    [System.AttributeUsage(System.AttributeTargets.Method)]
    public sealed class TransactionFlowAttribute : Attribute, System.ServiceModel.Description.IOperationBehavior
    {
        public TransactionFlowAttribute(TransactionFlowOption transactions)
        {
            Transactions = transactions;
        }

        public TransactionFlowOption Transactions { get; }

        public void AddBindingParameters(OperationDescription operationDescription,
            BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription,
            DispatchOperation dispatchOperation)
        {
        }

        public void Validate(OperationDescription operationDescription)
        {
        }
    }

    public enum TransactionFlowOption
    {
        NotAllowed,
        Allowed,
        Mandatory,
    }
}

#endif


NuGet



NuGet , NetFramework and NetCore.



https://docs.microsoft.com/ru-ru/dotnet/standard/library-guidance/media/cross-platform-targeting/nuget-package-multiple-assemblies.png





:



  • ( )
  • (Bootstrapper)




Net Framework app.config, web.config, XML API : System.Configuration System.Configuration.ConfigurationManager. , AppSettings ConfigurationSection.



NetCore API, (JSON, INI, XML) (, , . .)



? : . , , .



, StackOverflow



- , NuGet System.Configuration.ConfigurationManager.



NetCore . , ( ConfigurationSection)





EventLog API System.Diagnostics, ILogger, messageLevel (Debug, Info, Warning, Error) . NLog c ILogger.



NetCore API: Microsoft.Extensions.Logging, ILogger<T>.



ILogger, , Microsoft.Extensions.Logging.ILogger<T>, , : NLog, log4Net, Serilog ..





IoC- Unity, — AutoFac, .



NetCore Microsoft.Extensions.DependencyInjection ServiceCollection, :



  • Singleton —
  • Scoped — , Http-
  • Transient —


IServiceProvider, .



: ASP.NET Core.



:



  • , . - , - - ,
  • IoC- ( , )


Global Assembly Cache



Global Assembly Cache, .



Net Core GAC, AssemblyResolver, .







, API , .



*.cproj SDK: <Project Sdk="Microsoft.NET.Sdk"> <TargetFramework>netcoreapp3.1</TargetFramework>



Windows Forms



NetCore 3.0 Windows Forms, Net Core 3.1 legacy ,

.



, "":



  • DataGrid . DataGridView;
  • ToolBar. ToolStrip;
  • MainMenu. MenuStrip;
  • ContextMenu. ContextMenuStrip.


Windows Forms.



Microsoft Windows Forms .NET Core,

.



Windows Forms NetCore 3.0 Visual Studio 2019, GUI Net Framework, NetCore 3.0, .



ASP.NET MVC Asp Net Core MVC



Windows Form , - .



Global.asax Startup.



System.Web HttpSession, Cookies, HttpRequest . . .



: ASP.NET Core



— , , , , HTTP .



: HTTP ASP.NET Core . Middleware. :



ASP.NET Core中间件管道



ASP.NET核心筛选器管道



Middleware:



public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            // Do work that doesn't write to the Response.
            await next.Invoke();
            // Do logging or other work that doesn't write to the Response.
        });

        app.Run(async context =>
        {
            await context.Response.WriteAsync("Hello");
        });
    }
}


API , Razor , : ASP.NET Core MVC : ASP.NET Core MVC.



— Bundler' js css , : ASP.NET Core



ASPNET ASMX AspNetCore WebAPI



- SOAP HTTP JSON XML.



asmx WebAPI WebMethod

Action POST asmx . , SOAP . , Rest-.



— , SOAP, : SoapCore.



JSON-RPC, NetCore, AspNet Core Middleware.



Microsoft guide AspNetCore Middleware SOAP : Custom ASP.NET Core Middleware Example.



ASPNET WebApi AspNetCore WebAPI



, : .



guide': -API ASP.NET ASP.NET Core



MVC:



  • Global.asax Startup
  • Logger, Exception Handler
  • ..


ASPNET Web Forms Blazor



, , , .



Web Forms Blazor:



  • ()
  • Code-behind :
  • Java-Script ( C#)


2 :



  • aspx , XML ( C#) ( , grid . .), html- aspx
  • : Blazor- code-behind


, , ASPNET Web Forms .



WCF



NetCore WCF Client API:



  • BasicHttpBinding, NetTcpBinding
  • security Message , Transport


WCF Net Core, :





WCF AspNetCore WebAPI asmx : — action- WCF. : Post-, URL https://you-service.com/MethodName, JSON-RPC.



— AspNetCore gRPC: WCF " — " gRPC RPC



WCF :



[ServiceContract]
public interface IItemService
{
    [OperationContract]
    Task<Item> GetItem(int id);
}

[DataContract]
public class Item
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}


gRPC protobuf :



message GetItemRequest {
    int32 id = 1;
}

message Item {
    int32 id = 1;
    string name = 2;
}

service ItemService {
    rpc GetItem(GetItemRequest) returns (Item);
}


— , :



  • WebAPI REST, JSON-RPC
  • gRPC


Workflow Foundation



Workflow Foundation ( Microsoft, , ), :





Workflow Foundation, . ?





? , , , ,

. : HTTP Rest like ,

( ) — gRPC . … ( ) Web Forms.



UPD: Portatibility Analyzer.




All Articles