【问题标题】:Akka.NET: can't send a list message to other ActorSystemAkka.NET:无法向其他 ActorSystem 发送列表消息
【发布时间】:2017-04-23 12:16:45
【问题描述】:

我是 Akka.NET 的新手。我最近开始做我的大学项目,遇到了一个奇怪的问题。 我在两个不同的桌面应用程序中有两个 ActorSystems。在第一个 ActorSystem 中,我使用 Akka.Remote 在两个 Actor 之间发送消息 ZippedAddressListMessage。

public class ZippedAddressListMessage
    {
        public ZippedAddressListMessage(List<string> list)
        {
            this.Values = list.AsReadOnly();
        }

        public IReadOnlyCollection<string> Values { get; private set; }

    }

这工作正常,但是当我尝试将此消息发送到其他 ActorSystem 时,我得到了大量的错误列表: screenshot #1 of the console window with the error

尽管如此,如果我发送一条仅包含一个整数的简单消息,则一切正常。可能存在序列化问题,但我无法弄清楚它必须如何解决。 我到处搜索,但仍然没有找到答案。拜托,你能解释一下如何解决这个问题吗?

来自第一个 ActorSystem 的演员:

    using Akka.Actor;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ChatMessages;
    using System.Diagnostics;
    using Akka.Serialization;
    using Akka.Actor.Internal;
    using Akka.Remote;

namespace Agent
{
    public class ActorHelper: ReceiveActor 
    {
        int N; 
        int fromId;
        int count;
        IActorRef chiefAgent;
        List<recordItem> agentList; 
    public ActorHelper()
    {
        count = 0;
        agentList = new List<recordItem>();

        Receive<CreateHelpersMessage>(msg =>
        {
            chiefAgent = Sender;
            fromId = msg.fromID;
            N = msg.N;

            for (int i = 0; i < msg.N; i++)
            {
                Process.Start("C:\\Users\\Artemij\\Source\\Repos\\Client\\AgentHelper\\AgentHelper\\bin\\Debug\\AgentHelper.exe",
                                "akka.tcp://Agent@localhost:8000/user/AgentActor/ActorHelper" + " " + i);
            }

        });


        Receive<NewAgentHelperMessage>(msg =>
        {
            Console.WriteLine(msg.name + " " + Sender.Path.ToString() + " || " + (count+1) + "/" + N);
            agentList.Add(new recordItem(fromId + count, msg.name, Sender));
            count++;

            Context.Watch(Sender);

            if (count == N)
            {
                chiefAgent.Tell(new AddressListMessage(agentList), Self);

            }


        });

        Receive<AddressListMessage>(msg =>
        {
            Console.WriteLine("All is ready");
            List<string> temp = new List<string>();
            foreach (recordItem i in msg.Values)
            {
                temp.Add(i.ID + " " + i.name + " " + Serialization.SerializedActorPath(i.address));
            }

            foreach (recordItem i in agentList)
            {
                Console.WriteLine(i.address);

                //PROBLEM HERE!
                i.address.Tell(new ZippedAddressListMessage(temp), Self);

            }



        });




    }

}

}

来自其他 ActorSystem 的演员:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Akka.Actor;
using ChatMessages;
using System.Diagnostics;

namespace AgentHelper
{

class AgentHelperActor: ReceiveActor
{
    int priority;
    ActorSelection seniorAgentActor;       
    List<recordItem> fullList;


    public AgentHelperActor(string addressSenior, string rank)
    {
        fullList = new List<recordItem>();

        seniorAgentActor = Context.ActorSelection(addressSenior);
        Int32.TryParse(rank, out priority);

        seniorAgentActor.Tell(new NewAgentHelperMessage("agent"+priority, ""), Self);

        //RECEIVING A LIST!!
        Receive<ZippedAddressListMessage>(msg =>
        {
            Console.WriteLine("The entire list");


        });


        Receive<NewAgentHelperMessage>(msg =>
        {
            Console.WriteLine(msg.name);

        });


    }


    public void updateList(IReadOnlyCollection<recordItem> list)
    {
        fullList = new List<recordItem>(list);
        foreach (recordItem i in fullList)
        {
            Console.WriteLine(i.ToString());
        }
    }


}

}

更新: 这是开头错误的屏幕截图。 screenshot #2 of the console window with the error 它写道 System.String[] 的格式与 JSON 序列化不兼容。

【问题讨论】:

    标签: c# visual-studio akka akka.net akka-remote-actor


    【解决方案1】:

    问题解决了!问题与序列化程序有关:我使用的是 Newtonsoft.Json 序列化程序。在向远程 ActorSystem 发送消息时,它不能序列化列表。 Newtonsoft 解释说 System.String[] 是不兼容的类型。

    解决办法是安装Hyperion序列化器: http://getakka.net/docs/Serialization#how-to-setup-wire-as-default-serializer

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2016-08-09
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2015-08-25
      相关资源
      最近更新 更多