Blazor WASM:引用 System.ComponentModel.DataAnnotations 导致发布版本链接失败

Blazor WASM: Reference to System.ComponentModel.DataAnnotations causes linking failure on release build

我创建了一个新的 Blazor WebAssembly 项目,并刚刚在共享项目的模型中向 属性 添加了一个 [Display] 属性:

using System;
using System.ComponentModel.DataAnnotations;

public class WeatherForecast
{
    [Display(Name = "Temperature")]
    public int TemperatureC { get; set; }
    ...
}

这需要参考System.ComponentModel.DataAnnotations.dll。但是当我这样做然后在 Release 模式下按 Build 时,编译器会抛出这个错误:

5>C:\Users\Ali\.nuget\packages\microsoft.aspnetcore.components.webassembly.build.2.1\targets\Blazor.MonoRuntime.targets(326,5): error : Unhandled exception. Mono.Linker.MarkException: Error processing method: 'System.Void System.Configuration.ConfigurationErrorsException::.ctor(System.String,System.Exception,System.String,System.Int32)' in assembly: 'System.Configuration.dll'
5> ---> Mono.Cecil.ResolutionException: Failed to resolve System.Configuration.ConfigurationException
5>   at Mono.Linker.Steps.MarkStep.HandleUnresolvedType(TypeReference reference)
5>   at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference)
5>   at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference)
5>   at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
5>   at Mono.Linker.Steps.MarkStep.ProcessQueue()
5>   --- End of inner exception stack trace ---
5>   at Mono.Linker.Steps.MarkStep.ProcessQueue()
5>   at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
5>   at Mono.Linker.Steps.MarkStep.Process()
5>   at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
5>   at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
5>   at Mono.Linker.Pipeline.Process(LinkContext context)
5>   at Mono.Linker.Driver.Run(ILogger customLogger)
5>   at Mono.Linker.Driver.Execute(String[] args, ILogger customLogger)
5>   at Mono.Linker.Driver.Main(String[] args)
5>C:\Users\Ali\.nuget\packages\microsoft.aspnetcore.components.webassembly.build.2.1\targets\Blazor.MonoRuntime.targets(326,5): error : ILLink failed with exit code -532462766.

我知道如果我禁用链接功能我可以通过错误,但我不想这样做因为我想减少下载的大小。

我发现我必须使用同名的 Nuget 包而不是引用 System.ComponentModel.DataAnnotations.dll,并且当我使用 Nuget 包时,链接错误已消除。