Skip to content

Instantly share code, notes, and snippets.

@blackdwarf
Created November 17, 2016 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackdwarf/7cc92fed1627e2abd7db8cf7301dc308 to your computer and use it in GitHub Desktop.
Save blackdwarf/7cc92fed1627e2abd7db8cf7301dc308 to your computer and use it in GitHub Desktop.
An example csproj project with <PackageTargetFallback> element

This element allows you to specify a set of target frameworks that will be used as fallback when restoring packages. In project.json it was represented as an imports statement inside the frameworks or tools node. The semantics of <PackageTargetFallback> are the same as imports in project.json.

This element is an MSBuild property. That means that it needs to be placed inside <PropertyGroup> element, either a new one or an existing one in your project file:

<PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>

The below bigger sample is a sample console application that uses the fallback element. The project in question is a default template that dotnet new creates in Preview 3 version of the CLI. You can see how it is placed within the csproj file. Observe that I've placed the element as part of an existing <PropertyGroup> that already exists in the template.

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
  </PropertyGroup>
  
  <ItemGroup>
    <Compile Include="**\*.cs" />
    <EmbeddedResource Include="**\*.resx" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App">
      <Version>1.0.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

If the syntax or notions of "property groups" and "item groups" are unfamiliar to you, here are some links with great explanations:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment