< BACK TO BLOG

Change the web application target framework from .NET 5 to .NET 6

πŸ—“οΈ2021-12-15

.NET 5 μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨μ„ .NET 6 μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨μœΌλ‘œ λ³€ν™˜ν•˜κ³ , κ΄€λ ¨ λ‚΄μš©μ„ μš”μ•½ν•΄μ„œ μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€.

Target Project

GitHub: bbonkr/bing-wallpaper μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨μ˜ λŒ€μƒ ν”„λ ˆμž„μ›Œν¬λ₯Ό .NET 5 μ—μ„œ .NET 6(으)둜 λ³€κ²½ν•©λ‹ˆλ‹€.

Requirements

  • λŒ€μƒ ν”„λ ˆμž„μ›Œν¬λ₯Ό .NET 6(으)둜 λ³€κ²½
  • Program.cs 파일의 λ‚΄μš©μ„ μ΅œμƒμœ„ λ¬Έ (Top level statements) 으둜 μž¬μž‘μ„±
  • 파일 λ²”μœ„ λ„€μž„μŠ€νŽ˜μ΄μŠ€ (File scoped namespace) λ₯Ό μ‚¬μš©

Task logs

Change target framework

.NET 5 μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨μ„ .NET 6 μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨μœΌλ‘œ λ³€κ²½ν•˜λŠ” μž‘μ—…μ€ λ‹€λ₯Έ μ΄μŠˆκ°€ μ—†μœΌλ©΄ 맀우 κ°„λ‹¨ν•œ μž‘μ—…μž…λ‹ˆλ‹€.

ν”„λ‘œμ νŠΈ νŒŒμΌμ„ νŽΈμ§‘κΈ°λ‘œ μ—΄κ³ , TargetFramework μš”μ†Œμ˜ 값을 net5.0 μ—μ„œ net6.0 으둜 λ³€κ²½ν•˜λ©΄ κΈ°λ³Έ μž‘μ—…μ€ λμž…λ‹ˆλ‹€.

μ‚¬μš©μ€‘μΈ (의쑴) νŒ¨ν‚€μ§€κ°€ .NET 6 μ—μ„œ μ‚¬μš©ν•  수 μ—†λ‹€λ©΄ λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆμœΌλ―€λ‘œ, λŒ€μƒ ν”„λ ˆμž„μ›Œν¬λ₯Ό λ³€κ²½ν•˜κΈ° 전에 μ‚¬μš©μ€‘μΈ (의쑴) νŒ¨ν‚€μ§€κ°€ .NET 6 μ—μ„œ μ‚¬μš©κ°€λŠ₯ν•œμ§€ κ²€ν† ν›„ 진행여뢀λ₯Ό κ²°μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.

Top level statements

.NET 6 (을)λ₯Ό λŒ€μƒμœΌλ‘œ μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨ ν”„λ‘œμ νŠΈλ₯Ό μž‘μ„±ν•˜λ©΄, Program.cs νŒŒμΌμ— μ΅œμƒμœ„ 문으둜 μ›Ήμ‘μš©ν”„λ‘œκ·Έλž¨(WebApplication) κ΅¬μ„±ν•˜λŠ” μ½”λ“œκ°€ μ œκ³΅λ©λ‹ˆλ‹€.

.NET 5 둜 μž‘μ„±λœ μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨ μ½”λ“œλ„ 맀우 κ°„νŽΈν•˜κ²Œ μ΅œμƒμœ„ 문을 μ‚¬μš©ν•˜λ„λ‘ μ½”λ“œλ₯Ό λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Program.cs 파일의 κΈ°μ‘΄ μ½”λ“œλŠ” κ·ΈλŒ€λ‘œ 남겨두고, 파일이 λ§ˆμ§€λ§‰ 쀄에 μž‘μ„±μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.

λ¨Όμ €, μ›Ή μ‘μš©ν”„λ‘œκ·Έλž¨ λΉŒλ” μΈμŠ€ν„΄μŠ€λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.

var builder = WebApplication.CreateBuilder(args);

편의λ₯Ό μœ„ν•΄ ꡬ역을 μ„€μ •ν•©λ‹ˆλ‹€.

var builder = WebApplication.CreateBuilder(args);
// configure service
// [1]
// configure
// [2]
// run
// [3]

[1] λΆ€λΆ„μ—λŠ” Startup.cs 파일의 ConfigureServices λ©”μ„œλ“œμ˜ μ½”λ“œλ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€.

public class Startup 
{
    public void ConfigureServices(IServiceCollection services)
    {
        // μƒλž΅
        services.AddControllersWithViews();
        // μƒλž΅
    }
}

ConfigureServices λ©”μ„œλ“œμ˜ λ‚΄μš©μ„ κ°€μ Έμ˜¨ ν›„ builder.Services 속성을 μ‚¬μš©ν•΄μ„œ μ•„λž˜μ™€ 같이 μž‘μ„±λ  수 μžˆμŠ΅λ‹ˆλ‹€.

var builder = WebApplication.CreateBuilder(args);
// configure service
// μƒλž΅
builder.Services.AddControllersWithViews();
// μƒλž΅
// configure
// [2]
// run
// [3]

[2] λΆ€λΆ„μ—λŠ” Startup.cs 파일의 Configure λ©”μ„œλ“œμ˜ μ½”λ“œλ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€.

public class Startup 
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // μƒλž΅
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();
        });     
        // μƒλž΅
    }
}

Configure λ©”μ„œλ“œμ˜ λ‚΄μš©μ„ κ°€μ Έμ˜¨ ν›„ μ•„λž˜μ™€ 같이 μž‘μ„±λ  수 μžˆμŠ΅λ‹ˆλ‹€.

var builder = WebApplication.CreateBuilder(args);
// configure service
// μƒλž΅
builder.Services.AddControllersWithViews();
// μƒλž΅
// configure
var app = builder.Build();
// μƒλž΅
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapDefaultControllerRoute();
});     
// μƒλž΅
// run
// [3]

λ‹€λ₯Έ ꡬ성은 app.Services 속성, app.Environment 속성을 μ°Έμ‘°ν•˜λ©΄ λ™μΌν•˜κ²Œ μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

[3] λΆ€λΆ„μ—λŠ” Program.cs 에 μž‘μ„±λ˜μ–΄ μžˆλŠ” Main λ©”μ„œλ“œμ˜ λ‚΄μš©μ„ κ°€μ Έμ˜΅λ‹ˆλ‹€.

μ™„μ„±λœ Program.cs νŒŒμΌμ€ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

var builder = WebApplication.CreateBuilder(args);
// configure service
// μƒλž΅
builder.Services.AddControllersWithViews();
// μƒλž΅
// configure
var app = builder.Build();
// μƒλž΅
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapDefaultControllerRoute();
});     
// μƒλž΅
// run
app.run();

이후, Program.cs 파일의 κΈ°μ‘΄ λ‚΄μš© namespace ν¬ν•¨ν•΄μ„œ Program 클래슀λ₯Ό μ œκ±°ν•©λ‹ˆλ‹€.

Use File scoped namespace

λ‚˜λ¨Έμ§€ νŒŒμΌμ„ ν•˜λ‚˜μ”© μ—΄μ–΄μ„œ λ„€μž„μŠ€νŽ˜μ΄μŠ€ λΈ”λŸ­μ„ 파일 λ²”μœ„ λ„€μž„μŠ€νŽ˜μ΄μŠ€λ‘œ λ³€κ²½ν•©λ‹ˆλ‹€.

ν•œλ²ˆμ— μ μš©ν•˜λŠ” 방법을 아직 λͺ»μ°Ύμ•„μ„œ ν•˜λ‚˜μ”© λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€.

using System;

namespace SomeNamesapce {
    // μƒλž΅
}

μ•„λž˜μ™€ 같이 λΈ”λŸ­μ„ μ œκ±°ν•˜λŠ” μž‘μ—…μž…λ‹ˆλ‹€.

using System;

namespace SomeNamesapce;

// μƒλž΅

Visual Studio λ₯Ό μ‚¬μš©μ€‘μ΄λ©΄, namespace μ΄λ¦„μ—μ„œ μ»¨ν…μŠ€νŠΈ 메뉴 (Ctrl + .)λ₯Ό ν™•μΈν•˜λ©΄ 파일 λ²”μœ„ λ„€μž„μŠ€νŽ˜μ΄μŠ€λ‘œ λ³€κ²½ 메뉴가 μžˆμŠ΅λ‹ˆλ‹€.

Dockerfile

도컀 이미지 λΉŒλ“œλ₯Ό μ‚¬μš©μ€‘μ΄λ©΄, λΉŒλ“œ 이미지와 λŸ°νƒ€μž„ 이미지λ₯Ό λ³€κ²½ν•©λ‹ˆλ‹€.

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
# μƒλž΅
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
# μƒλž΅

μ•„λž˜μ™€ 같이 .NET 6 ν™˜κ²½μœΌλ‘œ λ³€κ²½ν•©λ‹ˆλ‹€.

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
# μƒλž΅
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
# μƒλž΅

CI/CD Environment

GitHub Action λ˜λŠ” Azure pipeline λ“± λ‹€λ₯Έ CI/CD λ₯Ό μ‚¬μš©μ£Όμ΄λ©΄ μž‘μ—… μ •μ˜μ—μ„œ ν™˜κ²½ κ΅¬μ„±μ‹œ μ‚¬μš©λ˜λŠ” .NET SDK 버전을 .NET 6 (으)둜 λ³€κ²½ν•©λ‹ˆλ‹€.

GitHub Actions 의 actions/setup-dotnet@v1 νŒ¨ν‚€μ§€μ˜ ν™˜κ²½λ³€μˆ˜ 값을 6.0.x 둜 μ§€μ •ν•©λ‹ˆλ‹€.

# μƒλž΅
jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    steps:
      # μƒλž΅
      - name: Setup dotnet
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: 6.0.x
      # μƒλž΅

Conclusion

.NET 6 (으)둜 λŒ€μƒ ν”„λ ˆμž„μ›Œν¬ λ³€κ²½λ§ŒμœΌλ‘œ μ„±λŠ₯ν–₯상이 μžˆλ‹€κ³  ν•˜λ‹ˆ, μ‹œκ°„λ˜μ‹€ λ•Œ λ³€κ²½ν•΄λ³΄μ‹œλ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.

μ‹€μ œ μž‘μ„±λœ .NET 6 (으)둜 λ³€κ²½μž‘μ—…μ„ ν™•μΈν•˜κ³  μ‹ΆμœΌμ‹œλ©΄ GitHub: bbonkr/bing-images feature/dotnet6 branch μ—μ„œ μ½”λ“œ 변경사항을 μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

GitHub Repository


Profile picture

Pon Cheol Ku (ꡬ본철)

Software developer

Other sites

If does not find interesting topic, you might visit other site on below link.

Β© 2024, Built with Gatsby