首页 > 解决方案 > 如何在 akka.net 中获得第一或监护演员?

问题描述

编辑:执行摘要:哔哔声是在哪里定义的?我在互联网上的 Akka.net 代码中看到它,但我的构建没有找到它。我必须导入、使用、链接、做、贿赂或杀死谁或什么?

应该非常容易。在 Akka.net 中迈出第一步,该示例未构建。这是从 [入门示例][1] 复制的

[1]:https ://getakka.net/articles/intro/tutorial-1.html 。它不会构建,因为未定义“Sys”。这个明显的基本步骤在他们的网站上没有描述,我已经放弃了tweak-n-try。

以下是所有代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAkka
{
    class Program
    {
        public class PrintMyActorRefActor : UntypedActor
        {
            protected override void OnReceive(object message)
            {
                switch (message)
                {
                    case "printit":
                        IActorRef secondRef = Context.ActorOf(Props.Empty, "second-actor");
                        Console.WriteLine($"Second: {secondRef}");
                        break;
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var firstRef = Sys.ActorOf(Props.Create<PrintMyActorRefActor>(), "first-actor");
            Console.WriteLine($"First: {firstRef}");
            firstRef.Tell("printit", ActorRefs.NoSender);
            Console.ReadKey();
        }
    }
}

标签: akka.net

解决方案


这是您的代码的工作版本:

using System;
using Akka.Actor;


namespace SysInAkkaNet
{
    class Program
    {
        public class PrintMyActorRefActor : UntypedActor
        {
            protected override void OnReceive(object message)
            {

                switch (message)
                {
                    case "printit":
                        IActorRef secondRef = Context.ActorOf(Props.Empty, "second-actor");
                        Console.WriteLine($"Second: {secondRef}");
                        break;
                }
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            using (var actorSystem = ActorSystem.Create("MyActorSystem"))
            {
                var firstRef = actorSystem.ActorOf(Props.Create<PrintMyActorRefActor>(), "first-actor");
                Console.WriteLine($"First: {firstRef}");
                firstRef.Tell("printit", ActorRefs.NoSender);
                Console.ReadKey();
            }
        }    
    }
}

您需要创建一个演员系统来放置您的演员。并且您需要添加对 Akka NuGet 包的引用和相应的using Akka.Actor;语句。

我知道 Akka.TestKit 有一个 property Sys,它为您提供对给定测试创建的 actor 系统的引用。

除此之外,我无法回答为什么您所指的文档会显示这些“Sys.ActorOf(...)”示例(带有大写 S),表明它是(可能是内置的)财产,所以我有点理解你的困惑。


推荐阅读