다음을 통해 공유


다른 런타임에 대해 패키지 유효성 검사

NuGet 패키지에서 다른 런타임에 대해 다른 구현 어셈블리를 사용하도록 선택할 수 있습니다. 이 경우 이러한 어셈블리가 서로 호환되고 컴파일 시간 어셈블리와 호환되는지 확인해야 합니다.

예를 들어 다음 시나리오를 고려해 보세요. 당신은 Unix와 Windows API에 대한 interop 호출이 포함된 라이브러리를 작업하고 있습니다. 다음 코드를 작성했습니다.

#if Unix
    public static void Open(string path, bool securityDescriptor)
    {
        // Call Unix specific stuff.
    }
#else
    public static void Open(string path)
    {
        // Call Windows specific stuff.
    }
#endif

결과 패키지 구조는 다음과 같습니다.

lib/net6.0/A.dll
runtimes/unix/lib/net6.0/A.dll

lib\net6.0\A.dll 는 기본 운영 체제에 관계없이 컴파일 시간에 항상 사용됩니다. lib\net6.0\A.dll 는 런타임 시 Unix가 아닌 시스템에도 사용됩니다. 그러나 runtimes\unix\lib\net6.0\A.dll Unix 시스템의 경우 런타임에 사용됩니다.

이 프로젝트를 압축하려고 하면 다음 오류가 발생합니다.

D:\demo>dotnet pack
Microsoft (R) Build Engine version 17.0.0-preview-21460-01+8f208e609 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  PackageValidationThrough -> D:\demo\bin\Debug\net6.0\PackageValidationThrough.dll
  Successfully created package 'D:\demo\bin\Debug\PackageValidationThrough.1.0.0.nupkg'.
C:\Program Files\dotnet\sdk\6.0.100-rc.1.21463.6\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Compatibility.Common.targets(32,5): error CP0002: Member 'A.B.Open(string)' exists on lib/net6.0/PackageValidationThrough.dll but not on runtimes/unix/lib/net6.0/PackageValidationThrough.dll [D:\demo\PackageValidationThrough.csproj]
C:\Program Files\dotnet\sdk\6.0.100-rc.1.21463.6\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Compatibility.Common.targets(32,5): error CP0002: Member 'A.B.Open(string, bool)' exists on runtimes/unix/lib/net6.0/PackageValidationThrough.dll but not on lib/net6.0/PackageValidationThrough.dll [D:\demo\PackageValidationThrough.csproj]

MultipleRuntimes

실수를 깨닫고 Unix 런타임에도 추가 A.B.Open(string) 합니다.

#if Unix
    public static void Open(string path, bool securityDescriptor)
    {
        // Call Unix specific stuff.
    }

    public static void Open(string path)
    {
        throw new PlatformNotSupportedException();
    }
#else
    public static void Open(string path)
    {
        // Call Windows specific stuff.
    }

    public static void Open(string path, bool securityDescriptor)
    {
        throw new PlatformNotSupportedException();
    }
#endif

프로젝트를 다시 압축하려고 하면 성공합니다.

다중 런타임 성공

strict 모드

프로젝트 파일에서 속성을 설정하여 이 유효성 검사기에 대해 strict 모드EnableStrictModeForCompatibleTfms 사용하도록 설정할 수 있습니다. 엄격한 모드를 사용하도록 설정하면 일부 규칙이 변경되고, 다른 규칙은 차이를 가져올 때 실행됩니다. 이는 비교하는 양측이 표면적 및 ID에서 엄격하게 동일하도록 하려는 경우에 유용합니다. 자세한 내용은 Strict 모드를 참조하세요.