How to resolve assemblies with PlatformNotSupportedException?

Viewed 7199

I'm a mathematician that's fallen in love with F#. .NET assemblies, however, cause me grief. I don't understand how they all entangle and resolve. So I'm trying to run an example from Infer.Net and when I try to translate it into a script, I run into the following error when running it into FSI in visual studio 2017:

Binding session to 'C:\Users\jdks\.nuget\packages\system.codedom\4.4.0\lib\netstandard2.0\System.CodeDom.dll'...
> System.PlatformNotSupportedException: Current platform is not supported by the current compiler choice Auto. Try a different one. ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
   at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
   at Microsoft.ML.Probabilistic.Compiler.CodeCompiler.CompileWithCodeDom(List`1 filenames, List`1 sources, ICollection`1 referencedAssemblies)
   at Microsoft.ML.Probabilistic.Compiler.CodeCompiler.Compile(List`1 filenames, List`1 sources, ICollection`1 referencedAssemblies)
   --- End of inner exception stack trace ---
   at Microsoft.ML.Probabilistic.Compiler.CodeCompiler.Compile(List`1 filenames, List`1 sources, ICollection`1 referencedAssemblies)
   at Microsoft.ML.Probabilistic.Compiler.CodeCompiler.WriteAndCompile(List`1 typeDeclarations)
   at Microsoft.ML.Probabilistic.Compiler.ModelCompiler.CompileWithoutParams[T](List`1 itds)
   at Microsoft.ML.Probabilistic.Models.InferenceEngine.Compile()
   at Microsoft.ML.Probabilistic.Models.InferenceEngine.GetCompiledInferenceAlgorithm(Boolean inferOnlySpecifiedVars, IVariable var)
   at Microsoft.ML.Probabilistic.Models.InferenceEngine.Infer[TReturn](IVariable var)
   at <StartupCode$FSI_0003>.$FSI_0003.main@() in C:\Temp\Script1.fsx:line 24
Stopped due to error

Here's the script:

#r "netstandard.dll"
#r @"C:\Users\jdks\.nuget\packages\system.codedom\4.4.0\lib\netstandard2.0\System.CodeDom.dll"
#r @"C:\Users\jdks\.nuget\packages\microsoft.ml.probabilistic\0.3.1810.501\lib\netstandard2.0\Microsoft.ML.Probabilistic.dll"
#r @"C:\Users\jdks\.nuget\packages\microsoft.ml.probabilistic.compiler\0.3.1810.501\lib\netstandard2.0\Microsoft.ML.Probabilistic.Compiler.dll"
#r @"C:\Users\jdks\.nuget\packages\microsoft.ml.probabilistic.learners\0.3.1810.501\lib\netstandard2.0\Microsoft.ML.Probabilistic.Learners.dll"
#r @"C:\Users\jdks\.nuget\packages\microsoft.ml.probabilistic.visualizers.windows\0.3.1810.501\lib\net461\Microsoft.ML.Probabilistic.Compiler.Visualizers.Windows.dll"
#r @"C:\Users\jdks\Proto\infer-master\src\FSharpWrapper\bin\Debug\netstandard2.0\Microsoft.ML.Probabilistic.FSharp.dll"

open System
open Microsoft.ML.Probabilistic
open Microsoft.ML.Probabilistic.Distributions
open Microsoft.ML.Probabilistic.Models

let firstCoin = Variable.Bernoulli(0.5)  
let secondCoin = Variable.Bernoulli(0.5)
let bothHeads = firstCoin &&& secondCoin
let engine = new InferenceEngine()

// this is the line that fails
let bothHeadsPost = engine.Infer<Bernoulli>(bothHeads)

printfn "Both heads posterior = %A" bothHeadsPost
bothHeads.ObservedValue <- false

As you can see, I've used the nuget packages for Infer.Net but downloaded the git repo and compiled it to get the FSharpWrapper.dll. Since I can compile, my guess is that the problem is assembly-related, but haven't the first idea how to troubleshoot. What should I do here?

Relevant Info:
- From the Infer.Net github repo: The core packages target .NET Standard 2.0, making them usable from any project that targets .NET framework version 4.6.1 or .NET Core 2.1

Bonus Question:
What does it mean when it says Binding session to ...? I can't find any good info in Expert F# 4.0 or online and it looks suspicious.

1 Answers

There are two reasons for this:

  1. The statement in the repository about this package comes with a big asterisk. .NET Standard 2.0 and .NET Framework 4.7 or lower are fraught with issues. For all intents and purposes, this package should only be used with .NET Core or .NET Framework 4.7.1+; otherwise, you can expect to see more problems along this line. The package authors should likely multitarget to netstandard2.0 and net461, which would resolve that class of issues.
  2. F# Interactive does not properly support .NET Standard components yet. This is something we're working on, but it's a difficult problem to solve and I don't expect proper support to stabilize for at least a few months.

I recommend using this package with a .NET Core console app that you run (either manually, or with the dotnet-watch-run tool).

Related