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)
12046정성태10/29/201918256오류 유형: 577. windbg - The call to LoadLibrary(...\sos.dll) failed, Win32 error 0n193
12045정성태10/27/201918695오류 유형: 576. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 - 두 번째 이야기
12044정성태10/27/201917837오류 유형: 575. mstest.exe - System.Resources.MissingSatelliteAssemblyException: The satellite assembly named "Microsoft.VisualStudio.ProductKeyDialog.resources.dll, ..."
12043정성태10/27/201919883오류 유형: 574. Windows 10 설치 시 오류 - 0xC1900101 - 0x4001E
12042정성태10/26/201918897오류 유형: 573. OneDrive 하위에 위치한 Documents, Desktop 폴더에 대한 권한 변경 시 "Unable to display current owner"
12041정성태10/23/201920453오류 유형: 572. mstest.exe - The load test results database could not be opened.
12040정성태10/23/201921068오류 유형: 571. Unhandled Exception: System.Net.Mail.SmtpException: Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied
12039정성태10/22/201917886스크립트: 16. cmd.exe의 for 문에서는 ERRORLEVEL이 설정되지 않는 문제
12038정성태10/17/201918491오류 유형: 570. SQL Server 2019 RC1 - SQL Client Connectivity SDK 설치 오류
12037정성태10/15/201926074.NET Framework: 867. C# - Encoding.Default 값을 바꿀 수 있을까요?파일 다운로드1
12036정성태10/14/201927360.NET Framework: 866. C# - 고성능이 필요한 환경에서 GC가 발생하지 않는 네이티브 힙 사용파일 다운로드1
12035정성태10/13/201920702개발 환경 구성: 461. C# 8.0의 #nulable 관련 특성을 .NET Framework 프로젝트에서 사용하는 방법 [2]파일 다운로드1
12034정성태10/12/201920288개발 환경 구성: 460. .NET Core 환경에서 (프로젝트가 아닌) C# 코드 파일을 입력으로 컴파일하는 방법 [1]
12033정성태10/11/201924684개발 환경 구성: 459. .NET Framework 프로젝트에서 C# 8.0/9.0 컴파일러를 사용하는 방법
12032정성태10/8/201920273.NET Framework: 865. .NET Core 2.2/3.0 웹 프로젝트를 IIS에서 호스팅(Inproc, out-of-proc)하는 방법 - AspNetCoreModuleV2 소개
12031정성태10/7/201918305오류 유형: 569. Azure Site Extension 업그레이드 시 "System.IO.IOException: There is not enough space on the disk" 예외 발생
12030정성태10/5/201925358.NET Framework: 864. .NET Conf 2019 Korea - "닷넷 17년의 변화 정리 및 닷넷 코어 3.0" 발표 자료 [1]파일 다운로드1
12029정성태9/27/201925519제니퍼 .NET: 29. Jennifersoft provides a trial promotion on its APM solution such as JENNIFER, PHP, and .NET in 2019 and shares the examples of their application.
12028정성태9/26/201921176.NET Framework: 863. C# - Thread.Suspend 호출 시 응용 프로그램 hang 현상을 해결하기 위한 시도파일 다운로드1
12027정성태9/26/201915748오류 유형: 568. Consider app.config remapping of assembly "..." from Version "..." [...] to Version "..." [...] to solve conflict and get rid of warning.
12026정성태9/26/201922100.NET Framework: 862. C# - Active Directory의 LDAP 경로 및 정보 조회
12025정성태9/25/201920398제니퍼 .NET: 28. APM 솔루션 제니퍼, PHP, .NET 무료 사용 프로모션 2019 및 적용 사례 (8) [1]
12024정성태9/20/201922356.NET Framework: 861. HttpClient와 HttpClientHandler의 관계 [2]
12023정성태9/18/201922888.NET Framework: 860. ServicePointManager.DefaultConnectionLimit와 HttpClient의 관계파일 다운로드1
12022정성태9/12/201926329개발 환경 구성: 458. C# 8.0 (Preview) 신규 문법을 위한 개발 환경 구성 [3]
12021정성태9/12/201942447도서: 시작하세요! C# 8.0 프로그래밍 [4]
... 76  [77]  78  79  80  81  82  83  84  85  86  87  88  89  90  ...