C# 运行MSBuild时发生内部故障-无法加载文件或程序集System.Net.Primitives
使用XUnit和Asp.Net Core 5,我进行了以下测试:C# 运行MSBuild时发生内部故障-无法加载文件或程序集System.Net.Primitives,c#,asp.net-core,msbuild,asp.net-core-5.0,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 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());
}
}
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
}
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上的一个已知问题 这似乎在安装防病毒软件时最常见 我见过几个这样的案例。我找到的唯一“修复”方法是卸载防病毒软件。在某些情况下,我们不得不重新安装MacOS
如果您不在MacOS上,请告诉我们。尝试使用查找程序集无法加载的原因。您是否在Windows上运行此程序?是否可以发布您的.csproj?我想看看您的TargetFramework设置为什么。您的问题中缺少一些内容,无法轻松重现您的体验。出于好奇,您使用的是什么csv软件包?你有没有考虑过Josh Close的CsvHelper,它非常快!
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);
}
}