Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 4개 있습니다.)
.NET Framework: 1081. Self-Contained/SingleFile 유형의 .NET Core/5+ 실행 파일을 임베딩한다면?
; https://www.sysnet.pe.kr/2/0/12733

.NET Framework: 2066. C# - PublishSingleFile과 관련된 옵션
; https://www.sysnet.pe.kr/2/0/13159

.NET Framework: 2067. C# - PublishSingleFile 적용 시 native/managed 모듈 통합 옵션
; https://www.sysnet.pe.kr/2/0/13160

.NET Framework: 2068. C# - PublishSingleFile로 배포한 이미지의 역어셈블 가능 여부 (난독화 필요성)
; https://www.sysnet.pe.kr/2/0/13161




C# - PublishSingleFile 적용 시 native/managed 모듈 통합 옵션

지난 글에 설명한,

C# - PublishSingleFile과 관련된 옵션
; https://www.sysnet.pe.kr/2/0/13159

PublishSingleFile 옵션을 .NET 5 프로젝트에 적용하면,

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>

        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
        <PublishSingleFile>true</PublishSingleFile>

    </PropertyGroup>

</Project>

배포 빌드(dotnet publish) 시,

E:\net5_pubone_sample> dir /b .\bin\Debug\net5.0\win-x64\publish
clrcompression.dll
clrjit.dll
coreclr.dll
mscordaccore.dll
net5_pubone_sample.exe
net5_pubone_sample.pdb

이렇게 닷넷 5 런타임의 native 모듈들이 (닷넷 6와는 달리) 함께 생성됩니다. 이러한 native 모듈을 단일 모듈에 합치고 싶다면 IncludeNativeLibrariesForSelfExtract 옵션을 주면 됩니다.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>

        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
        <PublishSingleFile>true</PublishSingleFile>

        <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    </PropertyGroup>

</Project>

이후 다시 배포 빌드(dotnet publish)하면 EXE와 PDB 파일만 생성됩니다. 이하 DebugType, SelfContained, PublishReadyToRun, PublishTrimmed 옵션은 모두 동일하게 적용됩니다. 단지, EnableCompressionInSingleFile 옵션은 .NET 6 전용이므로 .NET 5 프로젝트에는 사용할 수 없습니다.




참고로, IncludeNativeLibrariesForSelfExtract 옵션의 숨겨진 역할이 하나 더 있습니다. 닷넷 5는, 실행 파일에 native module을 포함하긴 하지만, IncludeNativeLibrariesForSelfExtract 옵션이 의미하는 바에 따라 실행 시점에 해당 native 모듈을 추출해 로컬 파일로 저장합니다.

제가 만든 예제 프로젝트의 경우 아래의 경로에 파일이 추출돼 실행 시 해당 바이너리가 로드됩니다.

%TEMP%\.net\net5_pubone_sample\0rfKH9kewXipt7yvr_cSn+1Wk35Knq8=

* 이 경로는 DOTNET_BUNDLE_EXTRACT_BASE_DIR 환경 변수로 재정의할 수 있습니다.

재미있는 건, IncludeNativeLibrariesForSelfExtract 옵션이 닷넷 6 프로젝트에서는 아예 동작을 안 한다는 점입니다. 그 옵션을 false로 설정하고 닷넷 6 프로젝트를 publish하면 설정하지 않았던 때와 동일하게 단일 파일로 생성됩니다. 더더욱 재미있는 점은, 닷넷 6의 경우 닷넷 런타임의 native 코드를 완벽하게 실행 모듈 하나에 병합한다는 점입니다.

그래서, .NET 6에서는 IncludeNativeLibrariesForSelfExtract (값에 상관없이) "%TEMP%\.net\...[생략]..." 디렉터리가 생성되지 않습니다. 즉, "SelfExtract" 시킬 필요가 없는 것입니다.

이와 비슷한 옵션으로 IncludeAllContentForSelfExtract가 있습니다. 옵션에 "All"이 의미하는 것처럼, 이것은 IncludeNativeLibrariesForSelfExtract 옵션까지도 기본적으로 true로 설정한 빌드를 합니다. 따라서 닷넷 5 프로젝트에서 true로 설정하고 빌드하면,

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>

        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
        <PublishSingleFile>true</PublishSingleFile>

        <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
        <DebugType>embedded</DebugType>

        <PublishTrimmed>true</PublishTrimmed>
    </PropertyGroup>

</Project>

생성한 단일 배포 파일은 실행 시 "%TEMP%\.net\...[생략]..." 디렉터리에 닷넷 BCL에 해당했던 managed 모듈과 native 모듈을 모두 추출한 다음 실행 모듈은 그것들을 로드해서 사용합니다.

반면, 닷넷 6 프로젝트에서는 어차피 IncludeNativeLibrariesForSelfExtract 옵션은 동작하지 않으므로 IncludeAllContentForSelfExtract 옵션을 true로 설정한 경우, managed 모듈들만 "%TEMP%\.net\...[생략]..." 디렉터리에 추출하고 로딩해 사용합니다.




이러한 점 때문에 재미있는 효과가 하나 있습니다. 즉, .NET 6에서 다음과 같이 설정한 프로젝트를 배포하는 경우,

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <PublishSingleFile>true</PublishSingleFile>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
        <DebugType>embedded</DebugType>
        <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
    </PropertyGroup>

</Project>

이 실행 파일을 Process Explorer 등을 통해 로드된 모듈 목록을 확인하면,

net_single_file_2.png

과연 이 프로세스가 닷넷 managed인지, C/C++로 만든 Native 실행 프로세스인지 구분할 수가 없습니다. 닷넷 런타임에 해당하는 clrjit.dll같은 모듈들이 실행 모듈 자체에 완벽하게 녹아들었고 managed DLL 조차도 메모리 내에 압축 해제한 후 매핑을 시키기 때문입니다.

그나마 추측해 볼 수 있다면, 위의 화면에서 EXE 모듈이 2개가 나오는 것으로 알 수 있는 정도입니다. 하지만 이것조차도 PublishReadyToRun 옵션을 주면 EXE 모듈이 하나만 나오므로 기대할 수 없습니다.

혹은, PE 포맷의 Section에 "CLR_UEF" 항목을 보고 알 수도 있습니다.

net_single_file_0.png

아니면, "dotnet-dump ps" 명령어를 이용하면 됩니다.




IncludeAllContentForSelfExtract 옵션은 IncludeNativeLibrariesForSelfExtract 옵션이 true일 것을 강요하므로, 만약 다음과 같이 강제로 어긋나게 설정하면,

<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<IncludeNativeLibrariesForSelfExtract>false</IncludeNativeLibrariesForSelfExtract>

빌드 시 이런 오류가 발생합니다.

C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(895,5): error NETSDK1143:
Including all content in a single file bundle also includes native libraries. If IncludeAllContentForSelfExtract is true, IncludeNativeLibrariesForSelfExtract must not be false. [E:\net6_pubone_sample\net6_pubone_sample.csproj]




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 11/11/2022]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 76  77  78  79  80  81  82  83  [84]  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11837정성태3/6/201939819기타: 74. 도서: 시작하세요! C# 7.3 프로그래밍 [10]
11836정성태3/5/201923399오류 유형: 525. Visual Studio 2019 Preview 4/RC - C# 8.0 Missing compiler required member 'System.Range..ctor' [1]
11835정성태3/5/201921867.NET Framework: 810. C# 8.0의 Index/Range 연산자를 .NET Framework에서 사용하는 방법 및 비동기 스트림의 컴파일 방법 [3]파일 다운로드1
11834정성태3/4/201920668개발 환경 구성: 432. Visual Studio 없이 최신 C# (8.0) 컴파일러를 사용하는 방법
11833정성태3/4/201921212개발 환경 구성: 431. Visual Studio 2019 - CMake를 이용한 공유/실행(so/out) 리눅스 프로젝트 설정파일 다운로드1
11832정성태3/4/201917124오류 유형: 524. Visual Studio CMake - rsync: connection unexpectedly closed
11831정성태3/4/201916957오류 유형: 523. Visual Studio 2019 - 새 창으로 뜬 윈도우를 닫을 때 비정상 종료
11830정성태2/26/201916643오류 유형: 522. 이벤트 로그 - Error opening event log file State. Log will not be processed. Return code from OpenEventLog is 87.
11829정성태2/26/201918330개발 환경 구성: 430. 마이크로소프트의 CoreCLR 프로파일러 예제 빌드 방법 - 리눅스 환경 [1]
11828정성태2/26/201926263개발 환경 구성: 429. Component Services 관리자의 RuntimeBroker 설정이 2개 있는 경우 [8]
11827정성태2/26/201919196오류 유형: 521. Visual Studio - Could not start the 'rsync' command on the remote host, please install it using your system package manager.
11826정성태2/26/201919372오류 유형: 520. 우분투에 .NET Core SDK 설치 시 패키지 의존성 오류
11825정성태2/25/201924642개발 환경 구성: 428. Visual Studio 2019 - CMake를 이용한 리눅스 빌드 환경 설정 [1]
11824정성태2/25/201919068오류 유형: 519. The SNMP Service encountered an error while accessing the registry key SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration. [1]
11823정성태2/21/201920655오류 유형: 518. IIS 관리 콘솔이 뜨지 않는 문제
11822정성태2/20/201919047오류 유형: 517. docker에 설치한 MongoDB 서버로 연결이 안 되는 경우
11821정성태2/20/201919793오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201922845오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201922114Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
11818정성태2/20/201920195VS.NET IDE: 131. Visual Studio 2019 Preview의 닷넷 프로젝트 빌드가 20초 이상 걸리는 경우 [2]
11817정성태2/17/201916534오류 유형: 514. WinDbg Preview 실행 오류 - Error : DbgX.dll : WindowsDebugger.WindowsDebuggerException: Could not load dbgeng.dll
11816정성태2/17/201920021Windows: 157. 윈도우 스토어 앱(Microsoft Store App)을 명령행에서 직접 실행하는 방법
11815정성태2/14/201918263오류 유형: 513. Visual Studio 2019 - VSIX 설치 시 "The extension cannot be installed to this product due to prerequisites that cannot be resolved." 오류 발생
11814정성태2/12/201917076오류 유형: 512. VM(가상 머신)의 NT 서비스들이 자동 시작되지 않는 문제
11813정성태2/12/201918413.NET Framework: 809. C# - ("Save File Dialog" 등의) 대화 창에 확장 속성을 보이는 방법
11812정성태2/11/201915709오류 유형: 511. Windows Server 2003 VM 부팅 후 로그인 시점에 0xC0000005 BSOD 발생
... 76  77  78  79  80  81  82  83  [84]  85  86  87  88  89  90  ...