简体   繁体   English

NuGet Package 具有多个目标依赖项

[英]NuGet Package with multiple target dependencies

Some NuGet packages have some target dependencies.一些 NuGet 包有一些目标依赖关系。 For example, the package Microsoft.Extensions.Logging.Console version 3.1.3:例如,package Microsoft.Extensions.Logging.Console 版本 3.1.3:

https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/3.1.3 https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/3.1.3

.NETCoreApp 3.1
    Microsoft.Extensions.Configuration.Abstractions (>= 3.1.3)
    Microsoft.Extensions.Logging (>= 3.1.3)
    Microsoft.Extensions.Logging.Configuration (>= 3.1.3)
.NETStandard 2.0
    Microsoft.Extensions.Configuration.Abstractions (>= 3.1.3)
    Microsoft.Extensions.Logging (>= 3.1.3)
    Microsoft.Extensions.Logging.Configuration (>= 3.1.3)

.NET 3.1 is compatible with .NET Standard 2.0, .NET 3.1 与 .NET 标准 2.0 兼容,

  • Why .NET 3.1 target dependencies is specify?为什么指定 .NET 3.1 目标依赖项?

.NET Core 2.* is compatible with .NET Standard 2.0. .NET Core 2.* 与 .NET 标准 2.0 兼容。

  • Can I use this package in .NET Core 2.* application?我可以在 .NET Core 2.* 应用程序中使用这个 package 吗?
  • Same question to .NET 4.7 and .NET 4.8?与 .NET 4.7 和 .NET 4.8 相同的问题?

Regarding .NETStandard 2.0 : According to this docu , all your mentioned target frameworks (.NET Framework 4.7 and 4.8, .NET Core 2.* and 3.*) are supported.关于.NETStandard 2.0 :根据此文档,支持您提到的所有目标框架(.NET Framework 4.7 和 4.8、.NET Core 2.* 和 3.*)。

I don't know about .NETCoreApp 5.0 , but for the moment, I'd ignore it.我不知道.NETCoreApp 5.0 ,但目前我会忽略它。 Maybe it's related to the fact that .NET 5 will be the successor of .NET Core 3.1 which utilizes .NET Standard 2.1.也许这与 .NET 5 将是 .NET Core 3.1 的继任者有关,后者使用 .NET 标准 2.1。 Lets see when the final release comes closer... at the moment, it is not even listed in the corresponding docu .让我们看看最终版本何时临近……目前,它甚至没有在相应的文档中列出。

Edit:编辑:

Microsoft has published documentation summarizing this: Microsoft 已发布文档对此进行了总结:


It isn't obvious for the package Microsoft.Extensions.Logging.Console. package Microsoft.Extensions.Logging.Console 并不明显。 Let's look at Microsoft.Extensions.Identity.Core: https://www.nuget.org/packages/Microsoft.Extensions.Identity.Core/3.1.5再来看看Microsoft.Extensions.Identity.Core: https://www.nuget.org/packages/Microsoft.Extensions.Identity.Core/3.1.5

See the csproj :请参阅csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Description>ASP.NET Core Identity is the membership system for building ASP.NET Core...</Description>
    <TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
    ...
  </PropertyGroup>
  ...
</Project>

This package target .NET Standard 2.0 and .NET Core 3.1 (value of the Azure DevOps variable $(DefaultNetCoreTargetFramework)). This package target .NET Standard 2.0 and .NET Core 3.1 (value of the Azure DevOps variable $(DefaultNetCoreTargetFramework)). The compile result in release is: release 中的编译结果为:

  • bin/release/netstandard2.0/Microsoft.Extensions.Logging.Console.dll bin/release/netstandard2.0/Microsoft.Extensions.Logging.Console.dll
  • bin/release/netcoreapp3.1/Microsoft.Extensions.Logging.Console.dll bin/release/netcoreapp3.1/Microsoft.Extensions.Logging.Console.dll

Two distinct dll are generated, but with what difference?生成了两个不同的 dll,但有什么区别?

See the file PasswordHasher.cs :请参阅文件PasswordHasher.cs

public class PasswordHasher<TUser> : IPasswordHasher<TUser> where TUser : class
{
    ...
#if NETSTANDARD2_0
    // Compares two byte arrays for equality. The method is specifically written so that the loop is not optimized.
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
    private static bool ByteArraysEqual(byte[] a, byte[] b)
    {
        if (a == null && b == null)
        {
            return true;
        }
        if (a == null || b == null || a.Length != b.Length)
        {
            return false;
        }
        var areSame = true;
        for (var i = 0; i < a.Length; i++)
        {
            areSame &= (a[i] == b[i]);
        }
        return areSame;
    }
#endif

    private static bool VerifyHashedPasswordV3(byte[] hashedPassword, string password, out int iterCount)
    {
        ...
        // Hash the incoming password and verify it
        byte[] actualSubkey = KeyDerivation.Pbkdf2(password, salt, prf, iterCount, subkeyLength);
#if NETSTANDARD2_0
        return ByteArraysEqual(actualSubkey, expectedSubkey);
#elif NETCOREAPP
        return CryptographicOperations.FixedTimeEquals(actualSubkey, expectedSubkey);
#else
#error Update target frameworks
#endif
    }
}

For .NET Core 3.1, the method CryptographicOperations.FixedTimeEquals is used.对于 .NET Core 3.1,使用CryptographicOperations.FixedTimeEquals方法。 But this method don't exists in .NET Standard 2.0.但是这种方法在 .NET Standard 2.0 中不存在。 Then a not optimized method ByteArraysEqual is added in Microsoft.Extensions.Identity.Core.dll to replace the absent method.然后在 Microsoft.Extensions.Identity.Core.dll 中添加了一个未优化的方法 ByteArraysEqual 来代替缺席的方法。

When you use the package NuGet Microsoft.Extensions.identity.Core to check a hash password in: .当您使用 package NuGet Microsoft.Extensions.identity.Core 检查 hash 密码时: .NET Core 3.1 projet, you use the platform specific method CryptographicOperations.FixedTimeEquals. .NET Core 3.1 项目,您使用平台特定方法 CryptographicOperations.FixedTimeEquals。 . . .NET Standard 2.0 compatible project, you use the not optimised method ByteArraysEqual. .NET 标准 2.0 兼容项目,您使用未优化的方法 ByteArraysEqual。

Questions were:问题是:

Why .NET Core 3.1 target dependencies is specify?为什么要指定 .NET Core 3.1 目标依赖项?

The .NET Standard 2.0 generated dll is compatible with .NET Core 3.1 and can be sufficient, but target a specific platform this allows to use specific platform component that be more optimized. .NET 标准 2.0 生成的 dll 与 .NET Core 3.1 兼容并且可能就足够了,但针对特定平台,这允许使用更优化的特定平台组件。

Can I use this package in .NET Core 2.* application?我可以在 .NET Core 2.* 应用程序中使用这个 package 吗? Same question to .NET 4.7 and .NET 4.8?与 .NET 4.7 和 .NET 4.8 相同的问题?

Yes, you can use this package in .NET Core 2.* because is compatible with .NET Standard 2.0, but you don't have the more optimized version.是的,您可以在 .NET Core 2.* 中使用此 package,因为它与 .NET 标准 2.0 兼容,但您没有更优化的版本。 Same answer to .NET Framework 4.7 and .NET Framework 4.8对 .NET 框架 4.7 和 .NET 框架 4.8 的相同答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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