2

I have a Visual Studio solution with multiple projects. There is one single csproj for every version of .NET Framework (+ .NET Core) I want to support, and I want to keep it that way. They all generate one DLL each. All DLLs are named the same but are slightly different.

I've recently started generating NuGet packages out of these DLLs using a nuspec file and the command nuget pack. So far, I've generated one NuGet package for each DLL.

Would it be possible to generate one single NuGet package, that contains all the DLLs, and when a user installs the package, installs the right DLL?

I tried the following in my nuspec file:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    ...
    <dependencies>
      <group targetFramework=".NETCoreApp3.1" />
      <group targetFramework=".NETFramework4.0" />
    </dependencies>
  </metadata>
  <files>
    <file src="bin\Company.Product.dll" target="lib\net40" />
    <file src="..\CompanyProductCore\bin\Company.Product.dll" target="lib\netcoreapp3.1" />
  </files>
</package>

When I look in the generated NuGet package, there are the two expected subfolders in the lib folder, and they each contain a DLL which looks right.

However, when I test the package and try to install it in another Visual Studio solution on a .NET Core App project, I get the warning:

Package X was restored using '.NETFramework,Version=v4.6.1...4.6.2...4.7...4.7.1...4.7.2...4.8 instead of the project target framework '.NetCoreApp,Version=3.1'. This package may not be fully compatible with your project.

which is something that did not appear when I packed it separately. So it would seem that it didn't detect that there was a .NET Core-specific DLL given.

Is what I want to do possible, and if so, how?

Helena
  • 1,041
  • 2
  • 12
  • 24
  • Ah, why do you come up with your own names for the target frameworks and still expect it to work? Try using the strings given in the documentation. – TomTom Mar 20 '20 at 11:27
  • Why not use `dotnet pack`, and let the command auto-generate the correct nuspec automatically? – zivkan Mar 20 '20 at 16:52
  • TomTom I didn't know I was supposed to write it differently, I must have misinterpreted some example I saw somewhere. As mentioned in the answer given by weichchm I tried to change it but it didn't help. – Helena Mar 23 '20 at 06:35
  • zivkan dotnet pack does seem like a good choice for .NET Core projects, but not necessarily .NET Framework projects, if I understood it correctly. But I'll try to see if I can get it to work. – Helena Mar 23 '20 at 06:53
  • zivkan I tried it and my .NET Core project successfully ran the command, but my .NET Framework 4.0 project gives me the error message that the Target "pack" doesn't exist in the project, and I'm unable to find what such a target tag should contain. – Helena Mar 23 '20 at 07:06

1 Answers1

1

Your target frameworks should be net40 and netcoreapp3.1.

<dependencies>
      <group targetFramework="netcoreapp3.1" />
      <group targetFramework="net40" />
</dependencies>

https://learn.microsoft.com/en-us/dotnet/standard/frameworks

weichch
  • 9,306
  • 1
  • 13
  • 25
  • Thank you! This was probably an issue, so I changed it, but changing this had no effect on the original problem. – Helena Mar 23 '20 at 06:21
  • @Helena Interestingly I tried your nuspec file and installed the generated nuget package into a .net core 3.1 app, and it worked. If you view the content of the nuspec file *inside* the generated nupkg file, does it contain multiple targets? – weichch Mar 23 '20 at 09:56
  • It does, but for some reason that nuspec file has them on the original format that I posted in my question. – Helena Mar 23 '20 at 10:10
  • @Helena Mine is also like what you posted (which is new to me as well), but it works for me, weird. – weichch Mar 23 '20 at 10:15
  • Very interesting. Thanks for testing. If I unpack the NuGet package with 7-zip and add a reference to the DLL that is present in the lib folder directly, instead of adding the package with NuGet package manager, I get no erors. – Helena Mar 23 '20 at 10:28
  • When I choose to install the package, interestingly the only folder created in C:\Users\myuser\.nuget\packages\company.product\\lib is the net40 folder, and the nuspec file there only contains 4.0. – Helena Mar 23 '20 at 11:08
  • I was gonna say, did you make any package which targets only net40 before? Maybe the package is cached. – weichch Mar 23 '20 at 11:10
  • Yeah I did, I realized that a couple of minutes ago myself. I tried clearing out everything related to this in C:\Users\myuser\.nuget\packages but unfortunately that didn't help. Also tried renaming the DLLs, and deleting the package entirely before repacking. Also tried installing the package in VS2017 instead of 2019 where I was before, same error. – Helena Mar 23 '20 at 11:12
  • Oh wow, now I tried renaming the actual NuGet package as well, and NOW it works! Seems to be some caching involved somewhere, but I don't know where… – Helena Mar 23 '20 at 11:17
  • That’s what I thought. And I don’t know where either. I made it correctly targeting two frameworks, then tried really hard to break it. However it always restored the working version. Maybe try set a different version number? – weichch Mar 23 '20 at 11:21