问题
In Visual Studio 2015 and 2017, I'm trying out the Http class from several F# examples in FSharp Interactive, and I keep getting:
error FS0039: The namespace or module 'Http' is not defined
Here's the sample:
open FSharp.Data
let response = Http.RequestString("http://api.themoviedb.org/3/search/movie", silentHttpErrors = true)
This is clearly due to the version of FSharp.Data. Is there a way to specify the correct version for FSharp Interactive? What version of FSharp.Data contains the Http module?
回答1:
Based on the comments, I put together a script to install Paket, intitialise it and then optionally install the dependencies whenever the script was run.
/// 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"
Note the use of 8.0.3 for Newtonsoft.Json, The latest version brings over 20 additional dependencies, so I've found a nice old version that's very self contained. You may leave the version number out if you want the latest.
Then, I use these scripts in a shared utilities.fsx for reusable functionality
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
Finally, I reference the whole lot in my target script by just loading utilities:
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#load "utilities.fsx"
open Utilities
which takes care of all the dependencies. These can be run from Visual studio with the Alt
+ Enter
key combination or from the command line with fsi.exe MyScript.fsx
来源:https://stackoverflow.com/questions/46277357/why-am-i-getting-f-error-fs0039-the-namespace-or-module-http-is-not-defined