Nuget support across projects being built with different .NET Platform (i.e. .NET 2, .NET 4, etc.)

StackOverflow https://stackoverflow.com/questions/16824426

  •  30-05-2022
  •  | 
  •  

Question

The problem:

I'll try to keep it simple.

  1. We have two solutions that are identical but one builds in .NET 2.0 and the other builds in .NET 4.0
  2. The solutions both hold one project each which are identical but one builds in .NET 2.0 and .NET 4.0.
  3. Both projects reference the same files
  4. The purpose of the approach is that we need to build a helper library but need to support multiple platforms (.NET 2.0. .NET 4.0, etc.)
  5. Solutions, Projects and source files all reside in the same folder.

Our goal:

We wan to use Nuget for NLog. However, we want to ensure that the .NET 2.0 project only binds to the Nuget NLog 2.0 library and the .NET 4.0 project only binds to the Nuget NLog 4.0 library.

Is there away in Nuget to support this? i.e. configuration, etc.

Thanks.

Was it helpful?

Solution

NLog package contains assemblies for .NET 2.0 and .NET 4.0 platforms. You should put the assemblies in the same package just like this and state NLog as dependency. When you add your package to a project, NuGet will get the correct assemblies both for NLog and your package.

OTHER TIPS

As Ufuk said, NLog package already contains assemblies for .NET 2.0 and 4.0.

If you had just one solution:

  • Install NLog. The correct framework will be referenced automatically.
  • Run NuGet Spec command.
  • Edit the created nuspec (possibly by using NuGet Package Explorer).
  • Run NuGet Pack. Dependencies on installed packages will be added automatically, including NLog.

However, you have two solutions and you want to create a single package. In this case:

  • Create a folder with the following structure (NLog assemblies must not be included):

    PACKAGE_NAME
    |  lib
    |  |  net40
    |  |  |  (insert here your .NET 4.0 assemblies)
    |  |  net45
    |  |  |  (insert here your .NET 4.5 assemblies)
    
  • Go to the root of this folder.

  • Run NuGet Spec command.
  • Rename Package.nuspec to PACKAGE_NAME.nuspec.
  • Edit the PACKAGE_NAME.nuspec (possibly by using NuGet Package Explorer) with your package data and include the following lines inside metadata:

    <dependencies>
        <dependency id="NLog" version="REPLACE_WITH_VERSION" />
    </dependencies>
    
  • Run NuGet Pack command. Your package will be created (you can check if it is ok with NuGet Package Explorer).

Now, everyone who installs your package will have your assemblies referenced correctly.

If you need more details, check this link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top