【问题标题】:Why am I getting F# error FS0039: The namespace or module 'Http' is not defined为什么我收到 F# 错误 FS0039:未定义命名空间或模块“Http”
【发布时间】:2017-09-18 10:44:28
【问题描述】:

在 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 模块?

【问题讨论】:

  • 你引用过这个库吗?
  • 我对@9​​87654322@ 的使用是否引用了过期版本?如何从 FSharp 交互中引用最新的 FSharp.Data.dll
  • 您需要通过 nuget 或 paket 安装 FSharp.Data。它将进入您的项目/解决方案文件夹中的包子文件夹中。然后要么在 fsproj 文件中引用(对于 fs 文件),要么在 fsx 文件中通过 #r path/to/Fsharp/Data/Dll 引用。之后,您可以打开它。您可能缺少这些步骤之一。由于旧的 (v 3) 类型提供程序,有一个全局 FSharp.Data 命名空间。你可能只是打开它。但你实际上需要 nuget 这个包。必要时在 F# 聊天中联系我或 slack。

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


【解决方案1】:

基于 cmets,我编写了一个脚本来安装 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"

注意 Newtonsoft.Json 使用 8.0.3,最新版本带来了 20 多个额外的依赖项,所以我找到了一个很好的旧版本,它非常独立。如果你想要最新的,你可以留下版本号。

然后,我在共享的 utility.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 从命令行运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多