从FSharp.Compiler.Service嵌入F#交互式示例Throwing Exception [英] Embedding F# Interactive Example Throwing Exception from FSharp.Compiler.Service

查看:103
本文介绍了从FSharp.Compiler.Service嵌入F#交互式示例Throwing Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 http://fsharp的Embedding F#Interactive示例. github.io/FSharp.Compiler.Service/interactive.html ,但以下行引发异常时出现问题:

I am working through the Embedding F# Interactive example from http://fsharp.github.io/FSharp.Compiler.Service/interactive.html but am having an issue with the following line throwing an exception:

let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream)

抛出的异常是:

"FSharp.Compiler.Service.dll中发生了'System.Exception'类型的未处理异常 附加信息:创建评估会话时出错:StopProcessing为空"

"An unhandled exception of type 'System.Exception' occurred in FSharp.Compiler.Service.dll Additional information: Error creating evaluation session: StopProcessing null"

我的项目从VS2015 Enterprise Update 1运行,设置为简单的F#控制台应用程序,目标F#运行时为F#4.0,目标框架为4.6.从nuget下载的FSharp.Compiler.Service的版本是2.0.0.2.

My project is being run from VS2015 Enterprise Update 1, setup as a simple F# console app, with Target F# runtime being F# 4.0 with the Target Framework as 4.6. The version of FSharp.Compiler.Service downloaded from nuget is 2.0.0.2.

我正在运行的Program.Fs文件代码在这里(示例的直接端口):

The Program.Fs file code I am running is here (a direct port of the example):

open System
open System.IO
open System.Text
open Microsoft.FSharp.Compiler.SourceCodeServices
open Microsoft.FSharp.Compiler.Interactive.Shell

[<EntryPoint>]
let main argv =

    let sbOut = new StringBuilder()
    let sbErr = new StringBuilder()
    let inStream = new StringReader("")
    let outStream = new StringWriter(sbOut)
    let errStream = new StringWriter(sbErr)

    // Build command line arguments & start FSI session
    let argv = [| "C:\\fsi.exe" |]
    let allArgs = Array.append argv [|"--noninteractive"|]

    let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
    let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream)

    /// Evaluate expression & return the result
    let evalExpression text =
        match fsiSession.EvalExpression(text) with
        | Some value -> printfn "%A" value.ReflectionValue
        | None -> printfn "Got no result!"

    evalExpression "42+1" // prints '43'

    Console.ReadLine() |> ignore

    0  // return integer

任何想法都会受到赞赏.

Any thoughts would be appreciated.

推荐答案

问题的关键位于编译器服务:有关FSharp.Core.dll的说明

如果进行动态编译和执行,则可能还需要 包括FSharp.Core.optdata和FSharp.Core.sigdata,请参见下文 指导.

If doing dynamic compilation and execution you may also need to include an FSharp.Core.optdata and FSharp.Core.sigdata, see below for guidance.

构建应用程序时,带有FSharp.Core.dll的输出目录缺少两个文件:

When you build the application the output directory with FSharp.Core.dll is missing two files:

FSharp.Core.optdata  
FSharp.Core.sigdata

您将需要找到这两个文件以及与FSharp.Core.dll一致的文件的正确版本,并将它们复制到构建输出目录中.

You will need to find these two files and the correct versions of the files that agree with FSharp.Core.dll and copy them to the build output directory.

如何使用Visual Studio查找文件

从菜单视图->解决方案资源管理器
展开项目
展开参考文献
右键单击FSharp.Core并选择属性.

From menu View -> Solution Explorer
Expand Project
Expand References
Right click FSharp.Core and select properties.

完整路径给出了至少应包含三个文件的目录. 使用文件浏览器在完整路径"中打开目录.

The Full Path gives the directory that should have at least the three files. Use a file explorer to open the directory in Full Path.

复制两个文件

FSharp.Core.optdata  
FSharp.Core.sigdata

到构建目录.

如何使用Visual Studio查找构建目录

从菜单视图->解决方案资源管理器
选择项目
按F4键打开属性"页面

From menu View -> Solution Explorer
Select Project
Press F4 to open the Properties Page

使用VS调试时,默认使用\bin\Debug的项目文件夹.

The Project Folder with \bin\Debug is the default when debugging with VS.

更改fsi.exe的位置

现在您已经拥有两个所需的文件,您还需要更改fsi.exe的位置

Now that you have the two needed files you will also need to change the location of fsi.exe

let argv = [| "C:\\fsi.exe" |]

因为我怀疑fsi.exe在您的C驱动器的根目录中.

because I doubt that fsi.exe is in the root of your C drive.

这篇关于从FSharp.Compiler.Service嵌入F#交互式示例Throwing Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆