Missing namespace while running F# script from command line

天大地大妈咪最大 提交于 2019-12-13 05:19:08

问题


I am trying to follow this linke http://msdn.microsoft.com/en-us/library/hh361033.aspx to run the a database query via SqlDataConnection from a F# script which is triggered via a command line fsx.exe.

While I run the following script lines from VS2012 interactive window there was no problem to open all the namespaces.

#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'

however if I run the same script file from the command line window using FSI.exe I got an error

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

Line 9 is referring to the Microsoft.FSharp.Linq namespace.

Q1: what am I missing here?

Also when I ran the script in VS2012 interactive window I can see all Reference information/added pathes/binding information and Val information. However if I ran the script from the command line there is nothing being printed out to help me debug.

Q2: is it possible to have the same information printed on the VS2012 interactive window to be printed on command line window?

regards casbby


回答1:


The Linq namespace is primarily defined in System.Core.dll. I'm guessing that in the VS interactive window this implicitly imported and hence your script works and that it's not implicitly exported in FSI. Either way you should be able to fix this by adding the following to the script

#r "System.Core.dll"



回答2:


I somehow stumbled upon the root cause of this problem: the reason the linq library is not defined is when I run the fsi.exe from command line it loaded the F# V2.0 core library.

Because when I tried to execute fsi.exe myScript.fsx from the command line, there is no output for Val, Reference and load information printed out to stdout, I tried to go into the fsi session directly by run fsi.exe without the script name and pasted the script content line by line. That is how I found the problem:

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'

>

I then realized there are actually 2 installation of FSharp on my PC:

C:\Program Files (x86)\Microsoft F#\v4.0 (This is what I have in the PATH by following a F# blog post probably is out of date) and

C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0 (This is what VS2012 uses)

One key difference between these two locations is the fsi.exe.config file. The correct fsi has all the version redirect config, looks like this:

<?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>

I hope these information can be useful to others.

However I am still not able to figure out how to make the fsi.exe to print out the Val, Reference, load and etc while running a script file. Any thought?



来源:https://stackoverflow.com/questions/20951480/missing-namespace-while-running-f-script-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!