【问题标题】:Dotnet build fails when project includes foreign language resources当项目包含外语资源时,Dotnet 构建失败
【发布时间】:2018-08-31 23:36:11
【问题描述】:

我正在使用 Visual Studio 15.6.3、dotnet SDK 2.1.102。

最近,(可能在更新后)我发现了一个 dotnet 构建错误,该错误显示在包含外语资源的项目中。错误信息如下:

C:\Program Files\dotnet\sdk\2.1.102\Microsoft.Common.CurrentVersion.targets(3570,5): error MSB4062: The "Microsoft.Build.Tasks.AL" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.  Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

我可以提供重现此错误的步骤:

  1. 在 Visual Studio 中创建一个 C# 控制台应用,将其命名为 DotNetBugTest。
  2. 编辑项目属性,在构建下,取消选中“首选 32 位”(控制台应用程序工件)。
  3. 在项目的根级别添加资源,命名为Messages.resx。
  4. 添加一个字符串资源,将其命名为A,并为其指定文本English。
  5. 在项目的根级别添加另一个资源,命名为Messages.fr-CA.resx。
  6. 添加相同的字符串资源,将其命名为A,并将文本命名为French。
  7. 现在在Program 类中,添加以下内容:

    class Program
    {
        static void Main(string[] args)
        {
            var rm = new ResourceManager("DotNetBugTest.Messages", typeof(Program).Assembly);
    
            var en = CultureInfo.CreateSpecificCulture("en-US"); 
            var fr = CultureInfo.CreateSpecificCulture("fr-CA"); 
    
            Console.WriteLine($@"en={rm.GetString("A", en)}");
            Console.WriteLine($@"fr={rm.GetString("A", fr)}");
    
            Console.ReadKey();
        }
    }
    

这在 Visual Studio 中运行良好。输出:

en=English
fr=French

但是如果你尝试从命令行编译它:

dotnet build --configuration Debug

你得到了错误。

有人知道如何解决这个问题吗?甚至建议去哪里看?

【问题讨论】:

  • 根据您的说法,听起来您的项目使用的是完整的 .NET Framework,而不是 .NET Core,因此您确实应该使用 MSbuild。 dotnet 不支持 msbuild 所做的一切。
  • 仅供参考,我最近偶然发现了这个错误。有一个完全不相关的“警告”,似乎并没有阻止 MSBuild 的构建。我引用了一个较旧的 Newtonsoft.Json 包,我修复了它(然后删除了错误)。后来我被允许通过 dotnet CLI 构建

标签: c# .net build resources target-sdk


【解决方案1】:

看来你必须使用MSBuild来构建这个项目。

另见这篇文章: https://blog.codeinside.eu/2019/11/30/build-dotnetcore-apps-with-msbuild-to-avoid-strange-netframeworkbased-errors/

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多