4

Using XUnit and Asp.Net Core 5 I have the following test:

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());
  }

}

When I run the test I often get the error, but not always:

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)

The ServiceProviderFixture class is:

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

}

And CsvImporter class is:

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);

    }

  }

Am I missing something?

Maybe with reading from a file?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • Try to use [`FUSLOGVW.exe`](https://stackoverflow.com/a/1674318/2557855) to find why the assembly failed to load – Artur May 20 '21 at 15:05
  • Are you running this on Windows? – Andy May 20 '21 at 15:38
  • 1
    Can you please post your .csproj? I'd like to see what your TargetFramework is set to. – RobV8R May 20 '21 at 21:59
  • there's a couple of missing things in your question, couldn't easilly reproduce what you are experiencing. By curiosity, what csv package are you using ? Have you considered CsvHelper from Josh Close, it is pretty fast ! – Carl Verret May 26 '21 at 13:09

1 Answers1

1

This is a known issue on MacOS.

It seems to occur most often when anti-virus software is installed.

https://github.com/mono/mono/issues/20878

I've seen several cases of this. The only "fix" I've found is to uninstall the anti-virus software. In some cases, we had to reinstall MacOS.

If you're not on MacOS, please let us know.

RobV8R
  • 1,036
  • 8
  • 16