Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support managed package assets for C++ UWP projects #5292

Open
JamesNK opened this issue May 29, 2017 · 11 comments
Open

Support managed package assets for C++ UWP projects #5292

JamesNK opened this issue May 29, 2017 · 11 comments

Comments

@JamesNK
Copy link

JamesNK commented May 29, 2017

Copied from JamesNK/Newtonsoft.Json#1285 by @mathisloge


Das Paket "Newtonsoft.Json 10.0.2" konnte nicht installiert werden. Sie versuchen, dieses Paket in ein Projekt zu installieren, das sich auf "native,Version=v0.0" bezieht, das Paket enthält aber keine Assemblyverweise oder Inhaltsdateien, die mit diesem Framework kompatibel sind. Wenden Sie sich an den Paketersteller, um weitere Informationen zu erhalten.

The above error message comes, when trying to install this package with nuget and a windows uwp app.
Using Visual Studio 2017.

@emgarten
Copy link
Member

It looks like this is from a UWP C++ project using packages.config. Install will look in the native/ folder for assemblies which doesn't exist in Newtonsoft.Json.

Managed C++ scenarios are not currently supported in NuGet.

@mishra14
Copy link
Contributor

tagging @anangaur and @karann-msft for clarity on the need for this.

@emgarten emgarten changed the title Newtonsoft.Json - not compatible with framework(Windows UWP) Support managed package assets for C++ UWP projects Nov 10, 2017
@emgarten emgarten added the C++ label Nov 10, 2017
@karann-msft
Copy link
Contributor

@mathisloge can you provide the repro steps for that error. What type of project were you trying to install the package to?

@mathisloge
Copy link

@karann-msft
I´ve just created a new Windows UWP app and installed the "Newtonsoft.Json 10.0.2".
I´m trying to reproduce these errors shortly.

@Danielku15
Copy link

Danielku15 commented Jan 22, 2018

As all C++/CLI issues were closed as duplicates of this one, I assume it's fine to continue the general "C++/CLI project-support-discussion" here. Not only C++ UWP projects might need .net NuGet packages, we also have certain managed wrappers for our own C++ code base which need references to libraries from NuGet (e.g. NLog for logging or Newtonsoft.Json for some serializations).

We just lately moved to the new-style csprojs for our solution and it became now even harder referencing NuGet packages from C++ projects. With the old csprojs you at least have the DLLs in the packages folder and you could reference manually the assemblies from there (not perfect but a workaround as long as you are careful with your versioning). But now as all NuGet packages are caches in %USERPROFILE% setting up the hint paths is really tricky as you manually need to edit your vcxprojs.

It would be great if Managed C++ projects can finally install .net packages. I would not mind manually adding a special override property like <NuGetFramework>net471</NuGetFramework> to my project files which tell NuGet to ignore the automatic detection and use the given classifier.

Reproduction Steps:

  1. Create a new C++ Project (e.g. Windows Console Application ).
  2. Enable CLR support in the project settings (optionally set the .net Target Framework version to something newer like v4.7.1)
  3. Use the Visual Studio Manage NuGet Packages or the Package Manager Console to install a .net package like EntityFramework

The output will be:

Install-Package : Could not install package 'EntityFramework 6.2.0'. You are trying to install this package into a project that targets
'native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more
information, contact the package author.

@LokiMidgard
Copy link

I have exact the same problem. Thank you @Danielku15 for the tip with the other c# project.

@mkdotcom
Copy link

The issue is still there.

@aelij
Copy link

aelij commented Feb 14, 2019

I've managed to sort of make PackageReference work in C++/CLI projects with a few simple modifications:

Add these targets:

<Target Name="AddNetFxTfnToTargetFrameworks" AfterTargets="_GetRestoreTargetFrameworksOutput">
<ItemGroup>
  <_RestoreTargetFrameworksOutputFiltered Include="$(NuGetTargetMoniker)" />
</ItemGroup>
</Target>
<Target Name="AddNetFxTfnToPackageReferences" AfterTargets="_GenerateProjectRestoreGraphPerFramework">
<ItemGroup>
  <_RestoreGraphEntryCrossJoin Include="@(_RestoreGraphEntry)">
    <OriginalId>%(PackageReference.Identity)</OriginalId>
    <IsManaged>%(PackageReference.IsManaged)</IsManaged>
    <TargetFrameworks>$(NuGetTargetMoniker)</TargetFrameworks>
  </_RestoreGraphEntryCrossJoin>
  <_RestoreGraphEntryDependencies Include="@(_RestoreGraphEntryCrossJoin)" Condition=" '%(Type)' == 'Dependency' " />
  <_RestoreGraphEntryPackageOnly Include="@(_RestoreGraphEntryDependencies)" Condition=" '%(OriginalId)' == '%(Id)' and '%(IsManaged)' == 'true' " />
  <_RestoreGraphEntry Remove="@(_RestoreGraphEntryPackageOnly)" />
  <_RestoreGraphEntry Include="@(_RestoreGraphEntryPackageOnly)" />
</ItemGroup>
</Target>

NuGetTargetMoniker is the .NET target framework. What these targets do is add it to the list of restore frameworks (which by default only contains Native in C++/CLI) and also set the target framework for each package reference marked with IsManaged.

Usage:

<ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" IsManaged="true" />
</ItemGroup>

Caveats:

  • Doesn't work with the NuGet UI
  • Requires a separate restore step (msbuild /t:restore) before opening the project
  • Requires reloading the project each time the packages change

I've also put the package references in a separated Packages.props file and imported it into the .vcxproj so we wouldn't have to unload it to add a package.

I think this is something that could be easily made a fully supported scenario.

@nkolev92 nkolev92 removed the C++ label Jan 6, 2020
@23W
Copy link

23W commented Feb 7, 2020

I've managed to sort of make PackageReference work in C++/CLI projects with a few simple modifications:

It really works, great thank you @aelij
Just interesting when C++\CLI (mixed assemblies) will be supported by Nuget?

@nref
Copy link

nref commented Mar 21, 2020

@aelij Thank you for the solution. When I run msbuild /t:restore I get error MSB4057: The target "Restore" does not exist in the project. Could you provide an example Restore target?

@aelij
Copy link

aelij commented Mar 24, 2020

@slater1 The Restore target is built-in, so I don't know why it's not working of you. Haven't used this solution in a while, so might not be compatible with every version.

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

No branches or pull requests