首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VSCode中包含用于F#的Akka.net框架

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

Stack Overflow用户
提问于 2020-09-09 03:50:24
回答 1查看 1.2K关注 0票数 4

我正在尝试使用我在互联网上找到的这个例子在VSCode中为F#使用Akka.NET。

代码

代码语言:javascript
复制
// 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插件,并且可以在没有Akka.NET的情况下运行F#。

dotnet add package Akka.FSharp --version 1.4.10

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

任何帮助都将不胜感激。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-09 04:22:18

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

代码语言:javascript
复制
#r "nuget: Akka.FSharp" 
#r "nuget: Akka.TestKit" 

另一个可能发生的警告是,nuget interactive需要一个--langversion:preview标志才能使用nuget引用。在VSCode settings.json中设置"FSharp.fsiExtraParameters": ["--langversion:preview"]为我做了这件事。

为了编译它,我必须更改的最后一点是用system.Terminate()替换system.Shutdown(),因为Shutdown方法已被弃用并删除。

以下是上面提到的更改的完整清单:

代码语言:javascript
复制
// 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()

在我的机器上,结果如下所示:

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63800694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档