简体   繁体   中英

Can I use .NET Standard to wrap .NET Core NuGet packages and make them available in classic .NET?

I am going through this blog post:

https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx

.. and its source code:

https://github.com/ScarlettCode/Example

To my understanding, it wraps a .NET Core NuGet package (Microsoft.EntityFrameworkCore) in a .NET Standard library and makes it available for a classic .NET 4.6.1 application:

<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />

Am I correct? Could that be a technique to wrap .NET Core NuGet packages in a .NET standard library can call them in classic .NET Framework 4.6.1 applications?

Can I use .NET Standard to wrap .NET Core NuGet packages and make them available in classic .NET?

Well, almost, but not quite.

To my understanding, it wraps a .NET Core NuGet package (Microsoft.EntityFrameworkCore) in a .NET Standard library and makes it available for a classic .NET 4.6.1 application.

This is where you've misunderstood. The package reference is for version 2.0.0 of Microsoft.EntityFrameworkCore.InMemory. We might consider this package to be "part of .NET Core" but in fact at a binary level the package targets .NETStandard 2.0.

How do I know the package targets .NET Standard 2.0? I establish that by looking at the Dependencies section of its NuGet page or by consulting Fuget .

.NET 4.6.1* and .NET Core 2.0 both support .NET Standard 2.0 , that is to say both of those frameworks (and newer versions) can reference .NET Standard 2.0 libraries.

So let's go back to your original question:

Can I use .NET Standard to wrap .NET Core NuGet packages and make them available in classic .NET?

No, but you can package your code up in libraries which target .NET Standard, and use those libraries from .NET Core projects AND .NET Framework projects; and likewise you can reference Nuget packages in .NET Framework projects provided they target .NET Standard even if the package has 'Core' in the name and you think of it as being part of .NET Core . This is the beauty of .NET Standard.

The key takeaway here is to pay close attention to the Dependencies section of Nuget description pages!


*Note that whilst .NET 4.6.1 officially supports .NET Standard 2.0, Microsoft recommend using 4.7.2 or higher. See my answer here for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM