0

I have a few projects, which could be a console application, Web Application, WebAPI, MVC etc) and a normal Class Library (not Core/.Net 6) which contains Newtonsoft.Json dll (latest version).

Each of the applications have their own version of Newtonsoft.Json dll that has been added manually for that project. In doing this it means an older app could have used version 1.0 the next application could have used v 2.0 and later apps could have used a much later version etc

I would like to share this class library through a Nuget package but i have added the latest version of Newtonsoft.Json dll into my own class library.

When i create a .nuspec file i would like to exclude this file but using this code

<files>
    <file src="bin\Release\Newtonsoft.dll" target="\" />
</files>

within the .nuspec file doesnt seem to work, reading https://learn.microsoft.com/en-us/nuget/reference/nuspec suggests to use the exclude but i cant get the syntax right.

What am i missing here?

Computer
  • 2,149
  • 7
  • 34
  • 71
  • You shouldn't add newtonsoft.dll into your own package. Instead add a dependency on a low version of newtonsoft to your nuget package. – Palle Due Jan 05 '23 at 13:30
  • Sounds like misconception - from the one side you want to share your library, from the other you want to exclude the library's dependency. It can lead to missing dependencies, because CLR would not know which package is really needed. – Xeo Jan 05 '23 at 13:33
  • The reason why i want to exclude Newton is so the existing functionality doesnt break and the app continues to use whatever it was set with but when time comes to upgrade that application we could do so. @PalleDue the reason why i used the latest version is so the more later versions which dont use v1 for example would continue to work as there maybe breaking changes (if this is what you mean by adding a low dependency?) – Computer Jan 05 '23 at 13:38

1 Answers1

1

In your nuspec include this:

<dependencies>
    <dependency id="Newtonsoft.Json" version="[3.5,14)" />
</dependencies>

Where 3.5 is the oldest version you support and 14 is the newest you do not support. Then you can leave out newtonsoft.dll from you own package and the project you install the nuget into will be able to determine which newtonsoft to use.

Palle Due
  • 5,929
  • 4
  • 17
  • 32