your programing

csproj를 사용하여 .NET Core 클래스 라이브러리를 어떻게 다중 대상으로 지정합니까?

lovepro 2020. 10. 14. 08:18
반응형

csproj를 사용하여 .NET Core 클래스 라이브러리를 어떻게 다중 대상으로 지정합니까?


.NET Core가 여전히 project.json형식을 사용하는 경우 여러 프레임 워크 (예 : net451, netcoreapp1.0)를 대상으로 하는 클래스 라이브러리를 빌드 할 수 있습니다.

이제 공식 프로젝트 형식이 csprojMSBuild를 사용하고 있으므로 대상으로 지정할 여러 프레임 워크를 어떻게 지정합니까? 내가 VS2017에서 프로젝트 설정에서이를 찾기 위해 노력하고 있어요,하지만 난 (그것도 내가 다른 모든 .NET 프레임 워크 버전 나열하지 않습니다 만 .NET 코어 프레임 워크에서 하나의 프레임 워크를 대상으로 할 수 있어요 않는 설치를) :

여기에 이미지 설명 입력


수동으로 편집하여 프로젝트 파일 및 추가 필요 기본에 TargetFramework을 기본적으로 변경 TargetFrameworks . 그런 다음 모니 커 에 대해 ; 분리 기호.

또한 수동으로 또는 VS Nuget 패키지 관리자를 사용하여 Nuget 패키지 참조를 조건부 ItemGroup에 넣을 수 있습니다.

.csproj는 다음과 같습니다.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net452'">
    <PackageReference Include="Microsoft.Azure.DocumentDB">
      <Version>1.12.0</Version>
    </PackageReference>
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
    <PackageReference Include="Microsoft.Azure.DocumentDB.Core">
    <Version>1.1.0</Version>
    </PackageReference>
  </ItemGroup>
</Project>

문서 누락으로 인해 요즘 내가 수행하는 또 다른 해결 방법은 VS2015에서 프로젝트를 만들고 사용 가능한 문서와 인텔리 젠스를 사용하여 project.json을 만든 다음 VS2017에서 솔루션을 열고 기본 제공 업그레이드를 사용하는 것입니다. 그런 다음 csproj 파일을 살펴보고 해당 구성을 수행하는 방법을 알아 봅니다.

모니 커 없이 더 난해한 타겟을 다중 타겟팅 :

마이크로 소프트 :

PCL은 권장되지 않습니다 +

PCL이 지원되지만 패키지 작성자는 대신 netstandard를 지원해야합니다. .NET Platform Standard는 PCL의 진화이며 portable-a + b + c 모니 커와 같은 정적에 연결되지 않은 단일 모니 커를 사용하여 플랫폼 간 이진 이식성을 나타냅니다.

당신이 휴대용 프로필을 대상으로 할 경우는 미리 정의되어 있지 않은 별명 도 할 수없는 추론 휴대용 프로필 정도 TargetFrameworkIdentifier, TargetFrameworkVersion하고 TargetFrameworkProfile. 또한 컴파일러 상수는 자동으로 정의되지 않습니다. 마지막으로 기본적으로 제공되지 않는 모든 어셈블리 참조를 추가해야합니다.

아래의이 예제는 dynamic키워드 를 사용한 프로젝트에서 가져온 것이므로 Microsoft.CSharp어셈블리가 추가로 필요 하므로 다른 대상에 대한 참조를 확인할 수 있습니다.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.5;net40;portable40-net45+sl5+win8+wp8</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8+wp8'">
    <TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile>Profile158</TargetFrameworkProfile>
    <DefineConstants>$(DefineConstants);PORTABLE158</DefineConstants>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='netstandard1.5'">
    <PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
    <PackageReference Include="System.ComponentModel" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='net40'">
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8+wp8'">
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Windows" />
  </ItemGroup>
</Project>

.csproj이 파일을 수동으로 편집 하고 속성을 설정할 수 있습니다 TargetFrameworks(아님 TargetFramework).

<TargetFrameworks>net451;netstandard1.4</TargetFrameworks>

예를 들면 다음을 참조하십시오 EFCore.csproj. https://github.com/aspnet/EntityFrameworkCore/blob/951e4826a38ad5499b9b3ec6645e47c825fa842a/src/EFCore/EFCore.csproj


실제로 클래스 라이브러리 (.NET Core)를 선택했습니다.

라이브러리가 여러 플랫폼 대상에서 작동해야하는 경우 원하는 프로젝트 템플릿이 아닙니다. 이 프로젝트 템플릿을 사용하면 .NETCore를 대상으로하는 프로젝트에서만 라이브러리를 사용할 수 있습니다. PCL 라이브러리 접근 방식은 폐기되었으므로 이제 .NETStandard를 선택해야합니다.

You do so by starting the project with the "Class Library (.NET Standard)" project template. You now have the option of picking the .NETStandard version. The current compatibility grid is here.

Hopefully they'll keep that linked article updated. This is in flux, .NETStandard 2.0 was nailed down but does not ship yet. Targeted for Q2 of 2017, end of spring probably, it currently shows as 97% done. I overheard the designers saying that using 1.5 or 1.6 is not recommended, not compatible enough with 2.0


I did a beginner's guide to multi-targeting net framework and netcore.

The simplest approach is to get a netcore or netstandard target working first. Then edit the csproj file and go through these steps for the other targets.

  1. Learn about conditional sections in your csproj file, so that you can declare different dependencies for each target. Create conditional sections for each target.
  2. Add <Reference />s for System.* dlls for any netframework targets just by reading what the build error messages say is missing.
  3. Deal with NuGet dependencies <PackageReference />s in the cases where they are not the same for each target. The easiest trick is to temporarily revert to single targetting so that the GUI will handle the Nuget references correctly for you.
  4. Deal with code that doesn’t compile on all targets, by learning a creative variety of techniques, workarounds and timesavers.
  5. Know when to cut your losses when the cost of adding more targets is too high.

참고 URL : https://stackoverflow.com/questions/42747977/how-do-you-multi-target-a-net-core-class-library-with-csproj

반응형