为什么我得到F#错误FS0039:命名空间或模块' Http'没有定义

时间:2017-09-18 10:44:28

标签: f# f#-interactive f#-data

在Visual Studio 2015和2017中,我尝试从FSharp Interactive中的几个F#示例中尝试Http类,并且我不断得到:

  

错误FS0039:命名空间或模块' Http'未定义

以下是样本:

open FSharp.Data
let response = Http.RequestString("http://api.themoviedb.org/3/search/movie", silentHttpErrors = true)

这显然是由于FSharp.Data的版本。有没有办法为FSharp Interactive指定正确的版本?什么版本的FSharp.Data包含Http模块?

1 个答案:

答案 0 :(得分:0)

根据评论,我整理了一个脚本来安装Paket,对其进行初始化,然后在脚本运行时选择性地安装依赖项。

/// install.paket.fsx
open System
open System.IO

printfn "Initialising..."
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
// invisibly run a command (paket.exe in this case)
let init paket =
    let psi = new System.Diagnostics.ProcessStartInfo(paket)
    psi.Arguments <- "init"
    psi.UseShellExecute <- false
    let p = System.Diagnostics.Process.Start(psi)
    p.WaitForExit()
    p.ExitCode

if not (File.Exists "paket.exe") then
    printfn "installing paket"
    let url = "http://fsprojects.github.io/Paket/stable"
    use wc = new Net.WebClient()
    let tmp = Path.GetTempFileName()
    let stable = wc.DownloadString(url)
    wc.DownloadFile(stable, tmp)
    File.Move(tmp,Path.GetFileName stable)
    printfn "paket installed"
    System.Threading.Thread.Sleep(100)
    printfn "initialising paket"
    init "paket.exe" |> ignore
    System.Threading.Thread.Sleep(200)
    printfn "paket initialised"
else
    printfn "paket already exists"

/// install.dependencies.fsx

open System.IO
printfn "Installing dependencies"
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#r "paket.exe"

open Paket
let dependencies = Paket.Dependencies.Locate(__SOURCE_DIRECTORY__)

printfn "%s" dependencies.DependenciesFile

if not (File.Exists "packages/Newtonsoft.Json/lib/net40/Newtonsoft.Json.dll") then
    printfn "installing nuget depenencies"
    // either use the dependencies.Install to add dependencies in the paket.dependencies file
    //dependencies.Install true |> ignore
    // or install them by name
    // I remove the existing versions
    dependencies.Remove "FSharp.Data"
    dependencies.Remove "Newtonsoft.Json 8.0.3"
    // then add them (because I'm pedantic about the way the dependencies file looks)
    dependencies.Add "FSharp.Data"
    dependencies.Add "Newtonsoft.Json 8.0.3"
    printfn "nuget depenencies installed"
else
    printfn "nuget depenencies already exist"

printfn "Dependencies installed"

注意使用8.0.3 for Newtonsoft.Json,最新版本带来了20多个附加依赖项,所以我找到了一个非常独立的旧版本。如果您想要最新版本,可以保留版本号。

然后,我在共享的utilities.fsx中使用这些脚本以获得可重用的功能

System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#load "install.paket.fsx"
#load "install.dependencies.fsx"

#r "packages/fsharp.data/lib/net40/fsharp.data.dll"
#r "packages/Newtonsoft.Json/lib/net40/Newtonsoft.Json.dll"

open FSharp.Data
open FSharp.Data.HtmlAttribute
open FSharp.Data.HtmlNode
open FSharp.Data.HttpRequestHeaders
open Newtonsoft.Json
open System.Net
open System.IO
// utilities like authentication, Http requests and JSON (de)serialization

最后,我通过加载实用程序来引用目标脚本中的所有内容:

System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#load "utilities.fsx"
open Utilities

负责所有依赖项。这些可以使用Alt + Enter组合键从Visual Studio运行,也可以使用fsi.exe MyScript.fsx

命令行运行