c# - 运行 MSBuild 时发生内部故障 - 无法加载文件或程序集 System.Net.Primitives

标签 c# asp.net-core msbuild asp.net-core-5.0

使用 XUnit 和 Asp.Net Core 5 我有以下测试:

public class Tests : IClassFixture<ServiceProviderFixture> {

  private readonly ServiceProviderFixture _fixture;

  public IndicatorsTests(ServiceProviderFixture fixture) {
    _fixture = fixture;
  }

  [Fact]
  public async Task FirstTest() {
    IEnumerable<Decimal> values = await _fixture.Provider.GetService<ICsvImporter>().ImportAsync("Values.csv");
    Assert.Equal(1000, values.Count());
  }

}
当我运行测试时,我经常收到错误消息,但并非总是如此:
MSBUILD : error MSB1025: An internal failure occurred while running MSBuild.
Microsoft.Build.BackEnd.NodeFailedToLaunchException: Could not load file or assembly 'System.Net.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. A device attached to the system is not functioning.
 (0x8007001F)
 ---> System.IO.FileLoadException: Could not load file or assembly 'System.Net.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. A device attached to the system is not functioning.
 (0x8007001F)
File name: 'System.Net.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.Diagnostics.Process.OpenStream(Int32 fd, FileAccess access)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Microsoft.Build.BackEnd.NodeProviderOutOfProcBase.LaunchNode(String msbuildLocation, String commandLineArgs)
   --- End of inner exception stack trace ---
   at Microsoft.Build.CommandLine.MSBuildApp.BuildProject(String projectFile, String[] targets, String toolsVersion, Dictionary`2 globalProperties, Dictionary`2 restoreProperties, ILogger[] loggers, LoggerVerbosity verbosity, DistributedLoggerRecord[] distributedLoggerRecords, Int32 cpuCount, Boolean enableNodeReuse, TextWriter preprocessWriter, TextWriter targetsWriter, Boolean detailedSummary, ISet`1 warningsAsErrors, ISet`1 warningsAsMessages, Boolean enableRestore, ProfilerLogger profilerLogger, Boolean enableProfiler, Boolean interactive, Boolean isolateProjects, Boolean graphBuild, Boolean lowPriority, String[] inputResultsCaches, String outputResultsCache)
   at Microsoft.Build.CommandLine.MSBuildApp.Execute(String[] commandLine)
Unhandled exception. Microsoft.Build.BackEnd.NodeFailedToLaunchException: Could not load file or assembly 'System.Net.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. A device attached to the system is not functioning.
 (0x8007001F)
 ---> System.IO.FileLoadException: Could not load file or assembly 'System.Net.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. A device attached to the system is not functioning.
 (0x8007001F)
File name: 'System.Net.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.Diagnostics.Process.OpenStream(Int32 fd, FileAccess access)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Microsoft.Build.BackEnd.NodeProviderOutOfProcBase.LaunchNode(String msbuildLocation, String commandLineArgs)
   --- End of inner exception stack trace ---
   at Microsoft.Build.CommandLine.MSBuildApp.BuildProject(String projectFile, String[] targets, String toolsVersion, Dictionary`2 globalProperties, Dictionary`2 restoreProperties, ILogger[] loggers, LoggerVerbosity verbosity, DistributedLoggerRecord[] distributedLoggerRecords, Int32 cpuCount, Boolean enableNodeReuse, TextWriter preprocessWriter, TextWriter targetsWriter, Boolean detailedSummary, ISet`1 warningsAsErrors, ISet`1 warningsAsMessages, Boolean enableRestore, ProfilerLogger profilerLogger, Boolean enableProfiler, Boolean interactive, Boolean isolateProjects, Boolean graphBuild, Boolean lowPriority, String[] inputResultsCaches, String outputResultsCache)
   at Microsoft.Build.CommandLine.MSBuildApp.Execute(String[] commandLine)
   at Microsoft.Build.CommandLine.MSBuildApp.Main(String[] args)
ServiceProviderFixture类是:
public class ServiceProviderFixture : IDisposable {

  public IServiceProvider Provider { get; private set; }

  public ServiceProviderFixture() {

    IServiceCollection services = new ServiceCollection();

    services.AddSingleton<CsvImporterConfiguration>();

    services.AddTransient<ICsvImporter, CsvImporter>();

    Provider = services.BuildServiceProvider();

  }

  public void Dispose() { } // Dispose

}
CsvImporter类是:
public class CsvImporter : ICsvImporter {

  private readonly CsvImporterConfiguration _configuration;

  public CsvImporter(CsvImporterConfiguration configuration) {

    _configuration = configuration;

  }

  public CsvImporter(Action<CsvImporterConfiguration> configuration) {

    _configuration = new CsvImporterConfiguration();

    configuration(_configuration);

  } 

  public async Task<IEnumerable<Decimal>> ImportAsync(String path, CancellationToken token = default(CancellationToken)) {

    return await Task.Factory.StartNew(() => {

      using (StreamReader stream = new StreamReader(path)) {

        using (CsvReader csv = new CsvReader(stream, new CsvConfiguration(_configuration.Culture) { 
          Delimiter = _configuration.Delimiter
        })) {

        return csv.GetRecords<Decimal>();

        }

      }

      }, token);

    }

  }
我错过了什么吗?
也许从文件中读取?

最佳答案

这是 MacOS 上的一个已知问题。
安装防病毒软件时似乎最常发生。
https://github.com/mono/mono/issues/20878
我见过好几个这样的案例。我发现的唯一“修复”是卸载防病毒软件。在某些情况下,我们不得不重新安装 MacOS。
如果您使用的不是 MacOS,请告诉我们。

关于c# - 运行 MSBuild 时发生内部故障 - 无法加载文件或程序集 System.Net.Primitives,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67541011/

相关文章:

c# - 在 Xamarin 中安装 android v4 支持库后仍然出错

C# - 如何从 json 动态对象中检索数组?

asp.net-core - 从 Asp.Net Core WebAPI 返回 jpeg 图像

asp.net-core - 覆盖 asp.net core razor 页面中的 razor View

delphi - 如何使用 MSBuild 和 Delphi XE2 进行构建

visual-studio - 如何让 Visual Studio 在构建时转换特定的 T4 模板?

c# - 使用已编译的 Lambda 表达式而不是 Activator.CreateInstance 来初始化 SoapHttpClientProtocol 对象

c# - 如何从 Linq 中的逗号分隔字符串中选择标记和索引

asp.net-core - ASP.NET Core中的ServicePointManager

msbuild - Linux 上的 JetBrains Rider EAP - 解决方案加载失败 : MsBuild not found