.NET 6의 핫 다시 로드

안녕하세요. 오늘은 .NET 6에 추가된 간단하지만 유용한 Process Id 및 Path를 얻는 API 및 .NET 6의 핫 다시 로드 기능을 확인해보도록 하죠.

확인은 dotnet watch기능이 Visual Studio Code에서 잘 동작 하는지 확인하기 위해 Visual Studio Code로 진행하겠습니다.

먼저 적절하게 프로젝트명으로 디렉토리 생성 후 dotnet new consle로 콘솔 프로젝트를 생성 한 후 Visual Studio Code를 시작합니다.

mkdir Test1121
cd Test1121
dotnet new console 
code .

image

아직 .NET 6 기본 프로젝트 템플릿을 경험하지 못하신 분이라면 주석이 포함된 두 줄 짜리 황당한 Program.cs 코드를 보실 수 있습니다. 터미널에서 dotnet run을 통해 실행시킬 수 있습니다.

$ dotnet run
Hello, World!

image

기본 프로젝트로 정상적으로 잘 실행되는 것을 확인 했으니 다음처럼 소스 코드를 작성하고 dotnet watch를 실행해봅시다.

while (true)
{
    Console.WriteLine($"PID = {Environment.ProcessId}");
    Thread.Sleep(1000);
}
$ dotnet watch
watch : Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload. Press "Ctrl + R" to restart.
watch : Building...
  복원할 프로젝트를 확인하는 중...
  복원할 모든 프로젝트가 최신 상태입니다.
  Test1121 -> W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.dll
watch : Started
PID = 9204
PID = 9204
PID = 9204
PID = 9204
PID = 9204
PID = 9204

오호 핫 다시 로드가 잘 동작하는 것 처럼 보이는군요. 정말 그런지 소스 코드를 변경하고 저장해봅시다.

while (true)
{
    Console.WriteLine($"PID = {Environment.ProcessId}, Path = {Environment.ProcessPath}");
    Thread.Sleep(1000);
}
watch : File changed: W:\csharp-learning\Test1121\Program.cs.
PID = 9204
PID = 9204
watch : Hot reload of changes succeeded.
PID = 9204
PID = 9204
PID = 9204
PID = 9204

동작하는 것 처럼 보이지만 반영되지 않습니다. 이게 뭡니까? ^^;
사실 핫 다시 로드는 메인 코드의 수정은 반영되지 않습니다. 다음처럼 변경해 봅시다.

var test = new Test();

while (true)
{
    test.PrintOutput();
}

class Test
{
    private int _count;

    public void PrintOutput()
    {
        _count++;

        Console.WriteLine($"{_count}: PID = {Environment.ProcessId}, Path = {Environment.ProcessPath}");
        Thread.Sleep(1000);
    }
}

저장 후 다시 dotnet watch를 터미널에서 실행한 후 출력 부분의 텍스트를 고쳐보거나 Thread.Sleep()의 대기 ms를 변경해보세요. 잘 동작하는 것을 확인할 수 있습니다.

여기서 정말 중요한 건 핫 다시 로드의 동작으로 프로세스 ID가 변경되지 않는다는 점입니다.

!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
watch : File changed: W:\csharp-learning\Test1121\Program.cs.
watch : Hot reload of changes succeeded.
8: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
9: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
10: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
11: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
12: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
13: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
14: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
15: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
16: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
watch : File changed: W:\csharp-learning\Test1121\Program.cs.
watch : Hot reload of changes succeeded.
17: !!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
18: !!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
19: !!PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
watch : File changed: W:\csharp-learning\Test1121\Program.cs.
watch : Hot reload of changes succeeded.
20: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
21: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
22: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
23: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
24: PID = 2256, Path = W:\csharp-learning\Test1121\bin\Debug\net6.0\Test1121.exe
2개의 좋아요