AdvApp
AdvApp

Reputation: 1170

System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2

Is the Microsoft.VisualBasic assembly not compatible with ASP.NET Core2?

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:

Exception System.TypeLoadException Could not load type 'Microsoft.VisualBasic.FileIO.TextFieldParser' from assembly 'Microsoft.VisualBasic, Version=10.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

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.

Upvotes: 2

Views: 817

Answers (1)

AdvApp
AdvApp

Reputation: 1170

[This isn't exactly an answer but it is a solution that works in place of the above assembly.]

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
{
    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; }
    }

Upvotes: 5

Related Questions