解析 F# 程序集引用

Resolving F# assembly references

在 Rider 和 Visual Studio 中,我在让 F# 识别 Newtonsoft.Json 依赖项时遇到问题。

我导入了 #r "Newtonsoft.Json",并使用 NuGet 将其添加到我的项目中,但这并不影响突出显示的错误:

程序集引用 "Newtonsoft.Json" 未找到或无效。

我只是第一次尝试 F#,所以这可能是一个非常简单的问题,但我无法在网上的任何地方找到对它的参考。

#r 正在 F# 脚本文件中使用 .fsx

要使其正常工作,您应该像这样将相对或绝对路径传递给 dll:

#r @"C:\Users\USER_NAME\.nuget\packages\newtonsoft.json.0.2\lib\net45\Newtonsoft.Json.dll"
//or
#r @"..\packages\Newtonsoft\Newtonsoft.Json.dll"
//or
#r "Newtonsoft.Json.dll" //assumes that dll and .fsx file are in the same folder

您还可以创建常用的 F# 项目文件 .fsproj(Rider 和 VisualStudio 可以提供帮助),它使用常用的 .fs 文件。

如果这些 .fs 文件是 .fsprojCompile 部分)的一部分,您可以像往常一样使用 nuget 中的命名空间(open 指令)而无需 #r dll链接。

查看 this 在 VS 中创建项目的教程

所以,最终我找到了这个问题的答案,由 Microsoft 发布。

An editor that supports F# Compiler Services will not be aware of the namespaces and assemblies that Azure Functions automatically includes. As such, it can be useful to include a prelude that helps the editor find the assemblies you are using, and to explicitly open namespaces.

例如:

#if !COMPILED
#I "../../bin/Binaries/WebJobs.Script.Host"
#r "Microsoft.Azure.WebJobs.Host.dll"
#endif

open System
open Microsoft.Azure.WebJobs.Host

let Run(blob: string, output: byref<string>, log: TraceWriter) =
...

所以就我而言:

#if !COMPILED
#I "../bin"
#r "Newtonsoft.Json.dll"
#endif