Microsoft MVP성태의 닷넷 이야기
VS.NET IDE: 156. Visual Studio - "Migrate packages.config to PackageReference" [링크 복사], [링크+제목 복사]
조회: 9020
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

Visual Studio - "Migrate packages.config to PackageReference"

예전에 packages.config 대신 PackageReference를 쓰는 것이 왜 좋은지에 대한 이유를 설명한 적이 있습니다.

Visual Studio 2017 - NuGet 패키지를 직접 참조하는 PackageReference 지원
; https://www.sysnet.pe.kr/2/0/11281

Migrate from packages.config to PackageReference
; https://docs.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference#benefits-of-using-packagereference

  • 별도 파일인 packages.config을 필요로 하지 않고 project 파일 내에 참조 관리를 통합
  • packages.config 파일을 열어 보면, (예를 들어, Newtonsoft.Json을 참조한 경우) 다른 여러 <package />들이 함께 보이는 반면, PackageReference로 하면 프로젝트가 직접적으로 참조한 패키지를 쉽게 구분
  • packages.config으로 관리하는 경우 설치한 패키지들이 비주얼 스튜디오의 솔루션이 위치한 하위 packages 폴더에 위치하지만, PackageReference로 하면 시스템 전역의 NuGet 패키지 저장소에 있으므로 디스크 공간도 절약
  • packages.config의 경우 제한된 구문의 <package /> 항목인 반면, msbuild는 project 파일의 좀 더 다양한 컴파일 조건을 적용할 수 있음
  • 이후 PackageReference의 기능은 계속 개발을 하지만, packages.config은 개발 중지 상태됨

저 글을 쓸 때만 해도 .NET Core와 UWP 프로젝트에서만 PackageReference를 지원했는데, 어느 순간 제한적이지만 .NET Framework 프로젝트에도 적용이 되었습니다.

Package references (PackageReference) in project files - Project type support
; https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#project-type-support

By default, PackageReference is used for .NET Core projects, .NET Standard projects, and UWP projects targeting Windows 10 Build 15063 (Creators Update) and later, with the exception of C++ UWP projects. .NET Framework projects support PackageReference, but currently default to packages.config. To use PackageReference, migrate the dependencies from packages.config into your project file, then remove packages.config.


하지만 기본값은 packages.config을 사용하는 것이기 때문에, 개발자가 직접 "Migrate packages.config to PackageReference..." 메뉴를 선택해 PackageReference로 변경해야 합니다.

vs_package_ref_1.png

혹은, 비주얼 스튜디오의 옵션 설정("Package Management" / "Default package management format")을 통해,

vs_package_ref_2.png

기본 참조 모드를 변경할 수 있습니다.




그런데 ASP.NET 프로젝트의 경우 "Migrate packages.config to PackageReference..." 메뉴를 이용하면 이런 오류가 발생합니다.

Operation failed

Project is not eligible for migration. Either the project is not packages.config based or doesn't support PackageReference yet. Visit https://docs.microsoft.com/en-us/nuget/reference/migrate-packages-config-to-package-reference for more information.

이에 대해서는 이미 문서에서 잘 설명하고 있습니다.

  • Visual Studio 2015를 포함한 이전 버전에서는 PackageReference 미지원
  • ASP.NET 프로젝트의 경우 migration 지원이 안 됨.
  • 몇몇 패키지들은 PackageReference와 호환이 안 됨

그렇다고 해서 ASP.NET 프로젝트가 PackageReference를 아예 사용할 수 없는 것은 아닙니다. 가령, packages.config에 다음과 같이 log4net을 참조하고 있는 경우,

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="log4net" version="2.0.12" targetFramework="net48" />
</packages>

1) 수작업으로 그냥 저 항목을 삭제하고 2) csproj 파일 내의 Reference 노드를 PackageReference 노드로 교체하면 됩니다.

<!--Reference Include="log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
    <HintPath>..\..\..\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath>
</Reference-->

<PackageReference Include="log4net" Version="1.2.11" />

(물론, PackageReference와 호환되지 않은 패키지는 바꿔서는 안 됩니다.)




참고로, packages.config을 사용하면 해당 프로젝트를 다른 PC에 복사해 명령행으로 빌드하는 경우 반드시 "packages" 폴더가 존재해야 합니다.

예를 들어, 간단하게 log4net 2.0.5 버전을 packages.config에 포함한 프로젝트가 있는 경우,

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="log4net" version="2.0.5" targetFramework="net40" />
</packages>

단순히 프로젝트 파일(csproj)과 소스 코드, packages.config 파일만 다른 PC에 복사해 "msbuild ConsoleApp1.csproj /target:Rebuild"를 수행하면 다음과 같은 식의 오류가 발생합니다.

Program.cs(1,7): error CS0246: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj]

Build FAILED.

"C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj" (Rebuild target) (1) ->
(ResolveAssemblyReferences target) ->
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2081,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj]

"C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj" (Rebuild target) (1) ->
(CoreCompile target) ->
  Program.cs(1,7): error CS0246: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj]

    1 Warning(s)
    1 Error(s)

따라서 "nuget restore"처럼 참조 패키지를 복원하는 과정이 필요하며 msbuild로는 이렇게 처리할 수 있습니다.

C:\temp\ConsoleApp1\ConsoleApp1> msbuild ConsoleApp1.csproj -t:restore -p:RestorePackagesConfig=true /p:SolutionDir=C:\temp\ConsoleApp1

혹은 그냥 속 편하게 개발자 PC의 "packages" 폴더까지 모두 복사해도 됩니다.




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







[최초 등록일: ]
[최종 수정일: 12/15/2020]

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

비밀번호

댓글 작성자
 




... 16  17  18  19  20  21  22  23  [24]  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13019정성태3/30/20226389.NET Framework: 1186. Win32 Message를 Code로부터 메시지 이름 자체를 구하고 싶다면?파일 다운로드1
13018정성태3/29/20226918.NET Framework: 1185. C# - Unsafe.AsPointer가 반환한 포인터는 pinning 상태일까요? [5]
13017정성태3/28/20226767.NET Framework: 1184. C# - GC Heap에 위치한 참조 개체의 주소를 알아내는 방법 - 두 번째 이야기 [3]
13016정성태3/27/20227585.NET Framework: 1183. C# 11에 추가된 ref 필드의 (우회) 구현 방법파일 다운로드1
13015정성태3/26/20228943.NET Framework: 1182. C# 11 - ref struct에 ref 필드를 허용 [1]
13014정성태3/23/20227567VC++: 155. CComPtr/CComQIPtr과 Conformance mode 옵션의 충돌 [1]
13013정성태3/22/20225884개발 환경 구성: 641. WSL 우분투 인스턴스에 파이썬 2.7 개발 환경 구성하는 방법
13012정성태3/21/20225230오류 유형: 803. C# - Local '...' or its members cannot have their address taken and be used inside an anonymous method or lambda expression
13011정성태3/21/20226685오류 유형: 802. 윈도우 운영체제에서 웹캠 카메라 인식이 안 되는 경우
13010정성태3/21/20225649오류 유형: 801. Oracle.ManagedDataAccess.Core - GetTypes 호출 시 "Could not load file or assembly 'System.DirectoryServices.Protocols...'" 오류
13009정성태3/20/20227191개발 환경 구성: 640. docker - ibmcom/db2 컨테이너 실행
13008정성태3/19/20226459VS.NET IDE: 176. 비주얼 스튜디오 - 솔루션 탐색기에서 프로젝트를 선택할 때 csproj 파일이 열리지 않도록 만드는 방법
13007정성태3/18/20226145.NET Framework: 1181. C# - Oracle.ManagedDataAccess의 Pool 및 그것의 연결 개체 수를 알아내는 방법파일 다운로드1
13006정성태3/17/20227161.NET Framework: 1180. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 remuxing.c 예제 포팅
13005정성태3/17/20226061오류 유형: 800. C# - System.InvalidOperationException: Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.
13004정성태3/16/20226088디버깅 기술: 182. windbg - 닷넷 메모리 덤프에서 AppDomain에 걸친 정적(static) 필드 값을 조사하는 방법
13003정성태3/15/20226227.NET Framework: 1179. C# - (.NET Framework를 위한) Oracle.ManagedDataAccess 패키지의 성능 카운터 설정 방법
13002정성태3/14/20226978.NET Framework: 1178. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 http_multiclient.c 예제 포팅
13001정성태3/13/20227362.NET Framework: 1177. C# - 닷넷에서 허용하는 메서드의 매개변수와 호출 인자의 최대 수
13000정성태3/12/20226941.NET Framework: 1176. C# - Oracle.ManagedDataAccess.Core의 성능 카운터 설정 방법
12999정성태3/10/20226436.NET Framework: 1175. Visual Studio - 프로젝트 또는 솔루션의 Clean 작업 시 응용 프로그램에서 생성한 파일을 함께 삭제파일 다운로드1
12998정성태3/10/20226048.NET Framework: 1174. C# - ELEMENT_TYPE_FNPTR 유형의 사용 예
12997정성태3/10/202210357오류 유형: 799. Oracle.ManagedDataAccess - "ORA-01882: timezone region not found" 오류가 발생하는 이유
12996정성태3/9/202215571VS.NET IDE: 175. Visual Studio - 인텔리센스에서 오버로드 메서드를 키보드로 선택하는 방법
12995정성태3/8/20227880.NET Framework: 1173. .NET에서 Producer/Consumer를 구현한 BlockingCollection<T>
12994정성태3/8/20227181오류 유형: 798. WinDbg - Failed to load data access module, 0x80004002
... 16  17  18  19  20  21  22  23  [24]  25  26  27  28  29  30  ...