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

2023-12-11

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

Code

// 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#。我使用以下命令安装了 Akka.NET

dotnet add package Akka.FSharp --version 1.4.10

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

Command Output

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


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

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

另一个可能发生的警告是 F# 交互需要--langversion:preview标记为能够使用 nuget 引用。环境"FSharp.fsiExtraParameters": ["--langversion:preview"]在 VSCode 中settings.json为我成功了。

我必须更改以使其编译的最后一点是替换system.Shutdown() with 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 VSCode 中包含 F# 的 Akka.net 框架 的相关文章

  • 如何在 F# 中将对象转换为泛型类型列表

    在下面的代码片段中 我的目的是将 System Object 可能是 FSharpList 转换为它所持有的任何泛型类型的列表 match o with list lt gt gt addChildList o gt list lt gt
  • F# 类型提供程序与 Lisp 宏

    我一直在阅读有关 F 3 0 类型提供程序的内容 例如here http msdn microsoft com en us library hh156509 aspx 并且它们似乎基于一种编译时代码生成 在这方面我想知道它们与 Lisp 宏
  • 该表达式的类型为 int,但此处与 unit 类型一起使用

    我试图在 F 中获得与此 vb net 代码完全相同的 非功能性的 Function FastPow ByVal num As Double ByVal exp As Integer As Double Dim res As Double
  • 从 C# 调用高阶 F# 函数

    给定 F 高阶函数 在参数中采用函数 let ApplyOn2 f int gt int f 2 和 C 函数 public static int Increment int a return a 我怎么打电话ApplyOn2 with I
  • 将 F# 类型保存到数据库

    A lot http gorodinski com blog 2013 02 17 domain driven design with fsharp and eventstore f 文章数推荐 http fsharpforfunandpr
  • F# 命名约定

    F 是否有 官方 命名 大小写约定 我总是怀疑是否使用 C 风格 Class MyFunctionName or Module my function name 在 F 中 您应该混合 BCL 类和 F 库类 它们具有不同的大小写 并且代码
  • F# 尝试处理未处理的异常

    在下面的代码中 我想读取一个文件并返回所有行 如果存在 IO 错误 我希望程序退出并将错误消息打印到控制台 但程序仍然遇到未处理的异常 对此的最佳实践是什么 我想我不需要Some None因为无论如何我都希望程序在错误时退出 谢谢 let
  • 如何编写一个计算表达式生成器来累积值并允许标准语言构造?

    我有一个计算表达式生成器 可以随时生成值 并且有许多自定义操作 但是 它不允许标准 F 语言构造 并且我在弄清楚如何添加此支持方面遇到了很多麻烦 举一个独立的例子 下面是一个非常简单且毫无意义的构建 F 列表的计算表达式 type Item
  • 如何从引用的表达式匹配中获取模块、函数等的 F# 名称

    我继续开发 F 引用表达式的打印机 它不一定是完美的 但我想看看有什么可能 中的活跃模式Microsoft FSharp Quotations Patterns and Microsoft FSharp Quotations Derived
  • 使用不区分大小写的比较从集合中减去记录

    我有一组记录 type Person Name string Age int let oldPeople set Name The Doctor Age 1500 Name Yoda Age 900 与上面的硬编码示例不同 这组数据实际上来
  • F# 和 MEF:导出函数

    因此 我试图在 F 控制台应用程序中运行这个简单的测试 open System Reflection open System ComponentModel Composition open System ComponentModel Com
  • F# 引用的另一个限制?

    今天早些时候 我遇到了 F 引用的限制 并在这里提出了一个问题 F 引号 变量可能会转义作用域 https stackoverflow com questions 6414185 f quotations variable may esca
  • true 和布尔列表 f# 的长度

    直接使用递归 写一个函数truesAndLength bool list gt int int那 返回列表的长度 在该对的第一个组件中 以及列表的数量 列表中正确的元素 在第二个组件中 你的函数必须只迭代 遍历列表的元素一次 请勿使用 Li
  • 如何从 C# 可移植类库 (PCL) 添加对 F# 可移植库的引用

    我有一个项目 其中包含两个 F 项目和一个 C 项目 我想在其中编写一些 XUnit 测试 FS PL F 3 1 3 3 1 0 可移植库 FS PL Legacy F 31 2 3 5 1 可移植库 旧版 测试 C NET 4 5 Wi
  • F# 使用 while 循环

    我有一个数据读取器 我想从中返回行集合 在阅读了一天的书籍后 我无法找到在 f 中执行此操作的最佳方法 我可以在 F 中以正常的 C 方式进行操作 但这不是我使用 f 的原因 这就是我想要实现的目标 let values while rea
  • Async.AwaitTask 在 f# 中如何工作?

    我知道 f 和 c 异步模型之间的主要区别在于 在 f 中 除非您调用 Async RunSynchronously 之类的内容 否则异步执行不会开始 在 C 中 当方法返回任务时 通常 并非总是 立即在后台线程中开始执行 Async Aw
  • 将可区分的联合传递给 InlineData 属性

    我正在尝试对一个解析器进行单元测试 该解析器解析字符串并返回相应的抽象语法树 表示为可区分的联合 我认为使用 Xunit Extensions 属性会非常紧凑InlineData将所有测试用例堆叠在一起
  • 什么时候需要使用 new 来初始化 F# 类型?

    给定一个类 例如 type MyClass member this Greet x printfn Hello s x 使用初始化实例是否合适 let x new MyClass 或没有new 另外 什么时候使用new构造函数比 a 更有用
  • F#:Microsoft.FSharp.Data.TypeProviders 是否需要配置文件 47?

    这是后续我昨天的帖子 https stackoverflow com questions 30399773 f fsc error fs2024 static linking may not use assembly that target
  • 专家 f# 脚本编译奇怪

    第 209 210 页有一个扩展示例 见下文 我使用的是 F 4 5 总之 我不明白的是 如果我单独键入每个语句 则会有一个声明引发错误 如果我立即提交整个脚本 以及引发错误的声明之后的函数 则一切正常 那么 当我批量提交所有语句时 交互中

随机推荐