Search code examples
f#f#-datafslab

Meaning of CsvProvider error "The given key was not present in the dictionary" when trying to load sample file?


I am having trouble loading the csv files with FSharp.Data csv provider provided by fslab, including the sample adwords.csv file.

What does this error below mean? Also, when I hover over the code in the Visual studio editor it mentions that "The given key was not present in the dictionary"

Problem example:

#load "packages/FsLab/FsLab.fsx"

open System.IO
open FSharp.Data

"adwords.csv"
|> File.ReadAllLines

let test = CsvProvider<"adwords.csv">.GetSample()

The output:

>
val it : string [] =
 [|"Criteria ID,Name,Canonical Name,Parent ID,Country Code,Target Type,Status";
    "1000010,Abu Dhabi,"Abu Dhabi,Abu Dhabi,United Arab Emirates",9041082,AE,City,Active";
"1000011,Ajman,"Ajman,Ajman,United Arab Emirates",9047096,AE,City,Active";
"1000012,Al Ain,"Al Ain,Abu Dhabi,United Arab Emirates",9041082,AE,City,Active";
"1000013,Dubai,"Dubai,Dubai,United Arab Emirates",9041083,AE,City,Active";
"2004,Afghanistan,Afghanistan,,AF,Country,Active"|]
>
>System.MethodAccessException: Attempt by method '<StartupCode$FSI_0007>.$FSI_0007.main@()' to access method 'FSharp.Data.Runtime.CsvFile`1<System.__Canon>.Create(System.Func`3<System.Object,System.String[],System.__Canon>,
at <StartupCode$FSI_0007>.$FSI_0007.main@() in C:\test.fsx:line 11
Stopped due to error

I ran into this problem with my own files, so I grabbed this sample file from here: https://raw.githubusercontent.com/fsharp/FSharp.Data/master/tests/FSharp.Data.Tests/Data/Adwords.csv

Debug info:

  • If I delete the FSharp.Data library folder (v 2.3.0) and replace with version 2.2.5 it works correctly with no error.
  • If I don't use the FsLab.fsx script and instead use

    #I "packages/FSharp.Data/lib/net40 #r "FSharp.Data.dll" then everything works.

  • The path to the FsLab.fsx script is correct, it runs when I send the line to fsi.
  • The F# version is 14.0.23413.0.
  • The version of FSharp.Data downloaded by FSlab is FSharp.Data.2.3.0.
  • I have no other references in the .fsx script.
  • I am using Visual Studio Community edition 14.0.24720.00 Update 1.
  • .NET version 4.6.01038
  • I am realizing now that I am not getting the popup asking if I want to allow the .dll like I think I used to get when I used this before.

Solution

  • There is some issue with the installation of FSharp.Data currently bundled with FsLab (as of June 2016). This issue is with version 2.3.0. If you instead use FSharp.Data 2.2.5 the code works as expected.

    Delete the packages/FSharp.Data folder and replace with version 2.2.5. I did it from an old installation but you could do it from Nuget