Search code examples
f#f#-interactivef#-3.0microsoft-data-sqlclient

NullReferenceException in F# interactive


I'm using microsoft.data.sqlclient library. I’m trying to execute this simple code in F# interactive:

#r @"C:\Users\micha\.nuget\packages\microsoft.data.sqlclient\5.1.0\ref\net6.0\Microsoft.Data.SqlClient.dll"

open Microsoft.Data.SqlClient

let conn = new SqlConnection("CONN_STR")
conn.Open()
let state = conn.State

Everything blowns up at the last step:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Data.SqlClient.SqlConnection.get_State() in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\ref\Microsoft.Data.SqlClient.cs:line 889
   at <StartupCode$FSI_0006>.$FSI_0006.main@() in C:\some\path\stdin:line 3
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Stopped due to error

Everything is working if I just execute the same code as normal console app

Why I'm getting this error when I'm doing that in F# interactive?


Solution

  • I stop getting exceptions after making two changes:

    Reference the package like this:

    #r "nuget: Microsoft.Data.SqlClient"
    

    Switch FSI to use .Net Core (Tools|Options|F# Tools|F# Interactive -> set "Use .NET Core Scripting" to True)

    I have no idea why.