简体   繁体   中英

Akka.net Sender On non-actor context

My actor system is initialized on console application. I configure one actor call BankAccrountActor which is responsible for handle bank account transaction like credit/debit/account balance of an Account. On credit/debit command the actor will credit/debit amount from account and Sender.Tell() with remaining balance. From my program.cs I call BankAccountActor with ask for debit and get balance with result.

Problem for me is from BankAccountActor it calls Sender.Tell() with balance. Here sender is IActorRef types and result is coming to program class and program is not an actor. How it be possible?

namespace BankAccount
{
class Program
{
    static void Main(string[] args)
    {
        ActorSystem actorSystem = ActorSystem.Create("BankAccountSystem");
        Props bankAccountAccProps = Props.Create<BankAccountActor>();
        IActorRef bankAccActor = actorSystem.ActorOf(bankAccountAccProps, "abc");

        var remainBalance = bankAccActor.Ask<decimal>(new DebitAccMessage(100)).Result;
    }
}
internal class BankAccountActor : ReceiveActor
{
    private decimal _accBalance = 100;
    public BankAccountActor()
    {
        Receive<DebitAccMessage>(message => Debit(message));
        Receive<CreditAccMessage>(message => Credit(message));
    }
    public void Debit(DebitAccMessage debitMessage)
    {
        _accBalance += debitMessage.Amount;

        Sender.Tell(_accBalance);
    }
    public void Credit(CreditAccMessage debitMessage)
    {
        _accBalance -= debitMessage.Amount;

        Sender.Tell(_accBalance);
    }
}
internal class CreditAccMessage
{
    public CreditAccMessage(decimal amount)
    {
        Amount = amount;
    }
    public decimal Amount { get; private set; }
}
internal class DebitAccMessage
{
    public DebitAccMessage(decimal amount)
    {
        Amount = amount;
    }
    public decimal Amount { get; private set; }
}

}

它的工作原理是因为在引擎盖下, Ask创建了一个仅用于接收一条消息的actor实例,即预期的答案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM