Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing dependencies for a .NET Standard class library?

I have converted some of my class libraries to .NET Standard with Visual Studio 2017.

This was easy, add a .NET Standard class library project in place of the original project and add all the files in there. The .csproj file even looks like a nuspec file now with package information and such. Inside the project options there was a checkbox for "Generate NuGet package on build", which I checked. Easy peasy.

However, .NET Framework consumers of my class library now gets a ton of dependencies, I counted at least 20 other nuget packages that were added, most of which was completely unecessary for my library. In other words, was "easy peasy too easy?"

Is this just a byproduct of me using .NET Standard as the only build output and I should add back a .NET Framework library as well?

Packages such as the following will be added to a project that consumes my library, even though they are completely unnecessary:

  • System.Security.Cryptography.*
  • System.Xml.*
  • System.IO.*

etc. there's plenty of packages being added. My library does "glorified" array analysis and doesn't require much at all.

The Visual Studio project is configured to target .NET Standard 1.0 and the only reference visible is the "NETStandardLibrary" so it's not like I added all of those myself.

I've inspected the package and it doesn't seem to list all of those either.

Can I add only the packages I need and still target .NET Standard 1.0?

My class library is open source here: https://github.com/lassevk/DiffLib
The nuget package is here: http://www.nuget.org/packages/difflib/2017.4.24.2347

like image 556
Lasse V. Karlsen Avatar asked May 16 '17 07:05

Lasse V. Karlsen


People also ask

What are .NET dependencies?

Diamond dependenciesIt's a common situation for a . NET project to have multiple versions of a package in its dependency tree. For example, an app depends on two NuGet packages, each of which depends on different versions of the same package. A diamond dependency now exists in the app's dependency graph.

Can I use .NET standard library in .NET framework?

NET Standard 2.0. . NET Standard 2.0 is supported by all modern platforms and is the recommended way to support multiple platforms with one target.

What is .NET standard class library?

. NET standard is the set of API that is available on all . NET implementations, It will create some type of uniformness, a portability that supports.Net Core, Xamarin and . Net Framework. Basically, It is the set of Base class libraries (BCL) that support a wide range of technologies like .

What is difference between .NET Standard and .NET framework?

. Net Core does not support desktop application development and it rather focuses on the web, windows mobile, and windows store. . Net Framework is used for the development of both desktop and web applications as well as it supports windows forms and WPF applications.


1 Answers

This is quite a complex situation at the moment:

Can I add only the packages I need and still target .NET Standard 1.0?

Yes you can do it, but this is no longer recommended. In essence, .NET Standard is a specification that is made up of the packages referencing it. The supported way is to reference NETStandard.Library since it guarantees to bring you all needed compilation references and logic that you need in order to build correctly.

Beginning with the upcoming netstandard2.0, NETStandard.Library will be a flat package without dependencies and the individual packages will be removed from the dependency tree if your project or any other project references them. Also, NETStandard.Library will not be published as a dependency - so if you build a netstandard2.0 library, the resulting NuGet package will have no dependencies. (NETStandard.Library.NETFramework is required to be installed when using it in .net framework projects - NuGet is supposed to do this automatically).

That being said, if you really want to do it, you can set

<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>

in the csproj file and then add items like <PackageReference Include="System.[Something]" Version="4.3.0" /> for everything you need.

like image 93
Martin Ullrich Avatar answered Jan 03 '23 22:01

Martin Ullrich