从命令行运行F#脚本时缺少命名空间

时间:2014-01-06 13:54:10

标签: command-line f#-interactive

我正在尝试按照此linke http://msdn.microsoft.com/en-us/library/hh361033.aspx从F#脚本通过SqlDataConnection运行数据库查询,该脚本通过命令行fsx.exe触发。

当我从VS2012交互式窗口运行以下脚本行时,打开所有命名空间没有问题。

#r "FSharp.Core.dll"
#r "System.Data.dll"
#r "FSharp.Data.TypeProviders.dll"
#r "System.Data.Linq.dll"


open Microsoft.FSharp.Linq
open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders

--> Referenced 'C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\3.0\Runtime\v4.0\FSharp.Core.dll'


--> Referenced 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Data.dll'


--> Referenced 'C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\3.0\Runtime\v4.0\Type Providers\FSharp.Data.TypeProviders.dll'


--> Referenced 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Data.Linq.dll'

但是,如果我使用FSI.exe从命令行窗口运行相同的脚本文件,则会出现错误

Script4.fsx(9,23): error FS0039: The namespace 'Linq' is not defined.  

第9行指的是Microsoft.FSharp.Linq命名空间。

Q1:我在这里错过了什么?

当我在VS2012交互式窗口中运行脚本时,我可以看到所有内容 参考信息/添加的路径/绑定信息和Val信息。但是,如果我从命令行运行脚本,则没有任何内容可以帮助我调试。

Q2:是否可以在VS2012交互式窗口上打印相同的信息,以便在命令行窗口上打印?

问候 casbby

2 个答案:

答案 0 :(得分:1)

Linq命名空间主要在System.Core.dll中定义。我猜测在VS交互式窗口中这是隐式导入的,因此你的脚本可以工作,并且它不会在FSI中隐式导出。无论哪种方式,您都应该通过在脚本中添加以下内容来解决此问题

#r "System.Core.dll"

答案 1 :(得分:0)

我偶然发现了这个问题的根本原因:未定义linq库的原因是当我从命令行运行fsi.exe时它加载了F#V2.0核心库。

因为当我尝试从命令行执行fsi.exe myScript.fsx时,没有输出Val,Reference和打印到stdout的加载信息,我试图通过运行fsi.exe直接进入fsi会话脚本名称并逐行粘贴脚本内容。这就是我发现问题的方法:

C:\Users\casbby>fsi

Microsoft (R) F# 2.0 Interactive build 4.0.40219.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> #r "FSharp.Core.dll";;

--> Referenced 'C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\2.0
\Runtime\v4.0\FSharp.Core.dll'

>

然后我意识到我的PC上实际上安装了2个FSharp:

C:\ Program Files(x86)\ Microsoft F#\ v4.0 (这是我在PATH中通过跟随F#博客文章可能已经过时了) 和

C:\ Program Files(x86)\ Microsoft SDKs \ F#\ 3.0 \ Framework \ v4.0 (这是VS2012使用的)

这两个位置之间的一个关键区别是fsi.exe.config文件。正确的fsi具有所有版本重定向配置,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="true" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity
          name="FSharp.Core"
          publicKeyToken="b03f5f7f11d50a3a"
          culture="neutral"/>
        <bindingRedirect
          oldVersion="2.0.0.0"
          newVersion="4.3.0.0"/>
        <bindingRedirect
          oldVersion="4.0.0.0"
          newVersion="4.3.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我希望这些信息对其他人有用。

但是我仍然无法弄清楚如何在运行脚本文件时使fsi.exe打印出Val,Reference,load等。有什么想法?

相关问题