如何在 VSCode 中包含 F# 的 Akka.net 框架

How to include Akka.net framework for F# in VSCode

我正在尝试使用我在 Internet 上找到的这个示例在 VSCode 中将 Akka.NET 用于 F#。

代码

// ActorSayHello.fsx
#time "on"
// #load "Bootstrap.fsx"

open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Akka.TestKit

// #Using Actor
// Actors are one of Akka's concurrent models.
// An Actor is a like a thread instance with a mailbox. 
// It can be created with system.ActorOf: use receive to get a message, and <! to send a message.
// This example is an EchoServer which can receive messages then print them.

let system = ActorSystem.Create("FSharp")

type EchoServer =
    inherit Actor

    override x.OnReceive message =
        match message with
        | :? string as msg -> printfn "Hello %s" msg
        | _ ->  failwith "unknown message"

let echoServer = system.ActorOf(Props(typedefof<EchoServer>, Array.empty))

echoServer <! "F#!"

system.Shutdown()

但是我收到了这个错误 ActorSayHello.fsx(6,6): error FS0039: The namespace or module 'Akka' is not defined.

我已经在 VScode 中配置了 ionide 插件并且能够 运行 F# 而无需 Akka.NET。我使用以下命令安装了 Akka.NET

dotnet add package Akka.FSharp --version 1.4.10

库已自动添加到 .fsproj 文件中。这是命令的输出

如有任何帮助,我们将不胜感激。谢谢!

由于您使用的是脚本文件,因此它不包含任何项目引用。 相反,您可以直接从脚本中使用 nuget 引用!

#r "nuget: Akka.FSharp" 
#r "nuget: Akka.TestKit" 

可能发生的另一个警告是 F# interactive 需要一个 --langversion:preview 标志才能使用 nuget 引用。在 VSCode settings.json 中设置 "FSharp.fsiExtraParameters": ["--langversion:preview"] 对我有用。

我必须更改以使其编译的最后一点是将 system.Shutdown() 替换为 system.Terminate() 因为 Shutdown 方法已被弃用和删除。

这是包含上述更改的完整列表:

// ActorSayHello.fsx
#time "on"
#r "nuget: Akka.FSharp" 
#r "nuget: Akka.TestKit" 
// #load "Bootstrap.fsx"

open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Akka.TestKit

// #Using Actor
// Actors are one of Akka's concurrent models.
// An Actor is a like a thread instance with a mailbox. 
// It can be created with system.ActorOf: use receive to get a message, and <! to send a message.
// This example is an EchoServer which can receive messages then print them.

let system = ActorSystem.Create("FSharp")

type EchoServer =
    inherit Actor

    override x.OnReceive message =
        match message with
        | :? string as msg -> printfn "Hello %s" msg
        | _ ->  failwith "unknown message"

let echoServer = system.ActorOf(Props(typedefof<EchoServer>, Array.empty))

echoServer <! "F#!"

system.Terminate()

结果在我的机器上是这样的:

Real: 00:00:00.000, ЦП: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
namespace FSI_0012.Project

Hello F#!
Real: 00:00:00.007, ЦП: 00:00:00.000, GC gen0: 1, gen1: 0, gen2: 0
val system : ActorSystem = akka://FSharp
type EchoServer =
  class
    inherit Actor
    override OnReceive : message:obj -> unit
  end
val echoServer : IActorRef = [akka://FSharp/user/$a#989929929]
val it : Threading.Tasks.Task