System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2 [英] System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2
问题描述
Microsoft.VisualBasic程序集与ASP.NET Core2不兼容吗?
Is the Microsoft.VisualBasic assembly not compatible with ASP.NET Core2?
我有一个C#类库,它提供了一种读取CSV文件的方法,因此我选择使用Microsoft.VisualBasic.FileIO.TextFieldParser来读取该文件.在WPF应用中引用该库方法时,效果很好.但是,在ASP.NET Core2 Web服务中,尽管编译没有错误,但在运行时会引发异常:
I have a C# class library that provides a method for reading a CSV file and I opted to use the Microsoft.VisualBasic.FileIO.TextFieldParser to read the file. The library method works great when referenced in a WPF app. However, in a ASP.NET Core2 web service, though it compiles without error, it throws an exception at runtime:
例外 System.TypeLoadException无法从程序集"Microsoft.VisualBasic,版本= 10.0.3.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"中加载类型"Microsoft.VisualBasic.FileIO.TextFieldParser".
Exception System.TypeLoadException Could not load type 'Microsoft.VisualBasic.FileIO.TextFieldParser' from assembly 'Microsoft.VisualBasic, Version=10.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
我怀疑这是由于与核心服务的某些不兼容.我确实尝试将编译器标志从NETCOREAPP2更改为NET461,但是Web服务仍然会引发异常.
I suspect this is because of some incompatibility with the service being Core. I did try changing the compiler flags from NETCOREAPP2 to NET461 but the web service still throws the exception.
推荐答案
[这不完全是一个答案,但它是代替上面的程序集的一种解决方案.]
[This isn't exactly an answer but it is a solution that works in place of the above assembly.]
我构建了此类,以替换Microsoft.VisualBasic中FileIO.TextFieldParser提供的功能,并使其与API兼容.下面仅提供了我需要的功能,因此可以根据需要进行扩展.
I built this class to replace the functionality provided by the FileIO.TextFieldParser in Microsoft.VisualBasic and to be API compliant with it. The below only provides the functionality I needed so expand as desired.
public class TextFieldParser : StreamReader, IDisposable
{
int iToken = 1;
bool quoted = false;
char[] delimiters;
string curLine;
public TextFieldParser(string path) : base(path) { }
public TextFieldParser(Stream stream) : base(stream) { }
public string[] ReadFields()
{
curLine = ReadLine();
return GetFields();
}
public void SetDelimiters(string delim)
{
delimiters = delim.ToCharArray();
}
public string[] GetFields()
{
if (delimiters == null || delimiters.Length == 0)
throw new Exception($"{GetType().Name} requires delimiters be defined to identify fields.");
if (!hasFieldsEnclosedInQuotes)
{
return curLine.Split(delimiters);
}
else
{
var token = (char)iToken;
var sb = new StringBuilder();
// Go through the string and change delimiters to token
// ignoring them if within quotes if indicated
for (int c = 0; c < curLine.Length; c++)
{
var qc = curLine[c];
if (hasFieldsEnclosedInQuotes && qc == '"')
{
quoted = !quoted;
continue;
}
else if (!quoted)
{
// Replace the delimiters with token
for (int d = 0; d < delimiters.Length; d++)
{
if (qc == delimiters[d])
{
qc = token;
break;
}
}
}
sb.Append(qc);
}
return sb.ToString().Split(token);
}
}
private bool hasFieldsEnclosedInQuotes = false;
public bool HasFieldsEnclosedInQuotes
{
get { return hasFieldsEnclosedInQuotes; }
set { hasFieldsEnclosedInQuotes = value; }
}
public bool EndOfData
{
get { return EndOfStream; }
}
这篇关于System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!