我的actor系统在控制台应用程序上初始化。我配置一个演员呼叫BankAccrountActor,负责处理银行账户交易,如账户的信用卡/借记卡/账户余额。在信用/借记命令中,参与者将从账户和Sender.Tell()中获得信用/借记金额,余额为余额。从我的program.cs我打电话给BankAccountActor请求借记并与结果保持平衡。
问题来自BankAccountActor它调用Sender.Tell()并保持平衡。这里sender是IActorRef类型,结果是程序类,程序不是actor。怎么可能?
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; }
}
}
答案 0 :(得分:0)
它有效,因为在幕后,Ask
创建了一个仅用于接收一条消息的actor实例,即预期答案。