기존 .NET Framework 프로젝트를 .NET Core/5+ 용으로 변환해 주는 upgrade-assistant, try-convert 도구 소개
재미있는 도구가 preview 상태를 거치고 있군요. ^^
Introducing the .NET Upgrade Assistant Preview
; https://devblogs.microsoft.com/dotnet/introducing-the-net-upgrade-assistant-preview/
실제로 현재 .NET 5+가 있다면 다음의 명령어로 간단하게 설치할 수 있습니다.
// A tool to assist developers in upgrading .NET Framework applications to .NET 6 and beyond
// https://github.com/dotnet/upgrade-assistant
dotnet tool install -g upgrade-assistant
dotnet tool install -g try-convert
테스트를 해볼까요? ^^ .NET 4.0 콘솔 프로젝트를 하나 만들고, 해당 csproj 파일 경로를 인자로 upgrade-assistant를 실행하면,
[0.2.227701 버전 미만]
c:\temp\ConsoleApp1\ConsoleApp1> upgrade-assistant ConsoleApp1.csproj
[tool update]
c:\temp> dotnet tool update -g upgrade-assistant
[0.2.227701 버전 이후]
c:\temp\ConsoleApp1\ConsoleApp1> upgrade-assistant upgrade ConsoleApp1.csproj
다음과 같은 step의 명령행 모드로 진입합니다.
[09:43:14 INF] MSBuild registered from C:\Program Files\dotnet\sdk\5.0.200\
[09:43:14 INF] Registered 1 extensions:
Default extension
[09:43:16 INF] Initializing upgrade step Select an entrypoint
[09:43:16 INF] Setting entrypoint to only project in solution: C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj
[09:43:16 INF] Initializing upgrade step Select project to upgrade
[09:43:16 INF] Setting only project in solution as the current project: C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj
[09:43:16 INF] Initializing upgrade step Back up project
Upgrade Steps
Entrypoint: C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj
Current Project: C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj
1. [Next step] Back up project
2. Convert project file to SDK style
3. Update TFM
4. Update NuGet packages
5. Add template files
6. Update C# source
a. Apply fix for UA0001: ASP.NET Core projects should not reference ASP.NET namespaces
b. Apply fix for UA0002: HtmlString types should be replaced with Microsoft.AspNetCore.Html.HtmlString
c. Apply fix for UA0003: ActionResult types should come from the Microsoft.AspNetCore.Mvc namespace
d. Apply fix for UA0004: Filter types should be used from the Microsoft.AspNetCore.Mvc.Filters namespace
e. Apply fix for UA0005: Do not use HttpContext.Current
f. Apply fix for UA0006: HttpContext.DebuggerEnabled should be replaced with System.Diagnostics.Debugger.IsAttached
g. Apply fix for UA0007: HtmlHelper should be replaced with IHtmlHelper
h. Apply fix for UA0008: UrlHelper should be replaced with IUrlHelper
i. Apply fix for UA0009: HelperResult should be replaced with Microsoft.AspNetCore.Mvc.Razor.HelperResult
j. Apply fix for UA0010: [AllowHtmlAttrubute] should be removed
7. Move to next project
Choose a command:
1. Apply next step (Back up project)
2. Skip next step (Back up project)
3. See more step details
4. Configure logging
5. Exit
>
위의 "Choose a command"에 따라 일반적인 프로젝트의 경우 "1"을 계속해서 입력하는 식으로 진행하면 "Back up project", "Convert project file to SDK style", "Update TFM", "Update NuGet packages", "Add template files", "Update C# source" 단계를 거치면서 마이그레이션 변환이 마무리됩니다.
그런데, 사실 실질적인 작업은 "Convert project file to SDK style"이고 그것은 try-convert 도구가 해주기 때문에 다른 단계들이 별로 필요가 없다면 곧바로 try-convert 도구를 솔루션 파일이 있는 디렉터리에서 실행해도 됩니다.
C:\temp\ConsoleApp1> try-convert
Conversion complete!
그럼 솔루션이 포함한 모든 프로젝트를 .NET Core 유형으로 변환해 줍니다. 아래는 기본적인 .NET 4.0 C# 콘솔 프로젝트가 .NET Core 유형으로 변환된 예제입니다.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</ItemGroup>
</Project>
간단하죠?! ^^
참고로, upgrade-assistant만 설치하고 try-convert 없이 실행하면 "2. Convert project file to SDK style" 단계에서 다음과 같은 오류 메시지와 함께 변환에 실패합니다.
[09:47:57 INF] Applying upgrade step Convert project file to SDK style
[09:47:57 INF] Converting project file format with try-convert
[09:47:57 ERR] Unexpected error applying step
System.ComponentModel.Win32Exception (2): The system cannot find the file specified.
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at Microsoft.DotNet.UpgradeAssistant.ProcessRunner.RunProcessAsync(ProcessInfo args, CancellationToken token) in /_/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/ProcessRunner.cs:line 53
at Microsoft.DotNet.UpgradeAssistant.Steps.ProjectFormat.TryConvertProjectConverterStep.RunTryConvertAsync(IUpgradeContext context, IProject project, CancellationToken token) in /_/src/steps/Microsoft.DotNet.UpgradeAssistant.Steps.ProjectFormat/TryConvertProjectConverterStep.cs:line 64
at Microsoft.DotNet.UpgradeAssistant.Steps.ProjectFormat.TryConvertProjectConverterStep.ApplyImplAsync(IUpgradeContext context, CancellationToken token) in /_/src/steps/Microsoft.DotNet.UpgradeAssistant.Steps.ProjectFormat/TryConvertProjectConverterStep.cs:line 53
at Microsoft.DotNet.UpgradeAssistant.UpgradeStep.ApplyAsync(IUpgradeContext context, CancellationToken token) in /_/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeStep.cs:line 169
Command (Apply next step (Convert project file to SDK style)) did not succeed
백업된 프로젝트 폴더를 복원한 후 다시 빌드하는 경우 다음과 같은 오류 메시지가 발생할 수 있습니다.
Your project does not reference ".NETFramework,Version=v4.0" framework. Add a reference to ".NETFramework,Version=v4.0" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
^^ 고민하지 마시고, /bin, /obj 폴더를 삭제 후 다시 빌드하면 됩니다.
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]