遇到akka.net問題。我需要訪問我已經用特定名稱創建的演員。我可以從IActorContext中檢索actor,但是我很難從ActorSystem中訪問它。Akka.net與ActorSelection的死鎖問題
我已經創建了一個名爲GetOrCreateActor的方法,它嘗試使用ActorSelection獲取actor。如果它不存在,catch創建一個名字爲新的actor。如果它確實存在,我希望它返回參考。但是,它永遠不會從'.Result'返回。假設這可能是某種死鎖問題。
public static IActorRef GetOrCreateActor<T>(this ActorSystem actorSystem, string actorPath, string name = null) where T : ActorBase
{
try
{
return actorSystem.ActorSelection(actorPath).ResolveOne(TimeSpan.FromSeconds(1)).Result;
}
catch
{
return actorSystem.ActorOf(actorSystem.DI().Props<T>(), name);
}
}
編輯 我一直在努力,包括下面的調用代碼的簡化版本。
演員系統是在IOC容器使用AutoFac創建(ExampleActor是我試圖訪問ReceiveActor):然後
containerBuilder.RegisterAssemblyTypes(typeof(ExampleActor).Assembly).Where(x => x.Name.EndsWith("Actor"));
var lazyContainer = new Lazy<IContainer>(() => containerBuilder.Build());
containerBuilder.Register(c =>
{
var system = ActorSystem.Create("ExampleActorSystem");
new AutoFacDependencyResolver(lazyContainer.Value, system);
return system;
}).As<ActorSystem>().SingleInstance();
return lazyContainer.Value;
ActorSystem注入另一個類,在那裏我稱之爲GetOrCreateActor方法(通過Execute方法):
public class ExampleCommand : IExampleCommand
{
private readonly ActorSystem _actorSystem;
public ExampleCommand(ActorSystem actorSystem)
{
_actorSystem = actorSystem;
}
public void Execute()
{
SendMessage();
}
private void SendMessage()
{
string message = new Message();
_actorSystem.GetOrCreateActor<ExampleActor>("akka://ExampleActorSystem/user/ExampleActor", "ExampleActor").Tell(message);
}
}
上述命令將從一個RESTful端點被稱爲
public ExampleGetModule(IExampleCommand exampleCommand)
{
Get["/api/createExample"] = parameters =>
{
exampleCommand.Execute();
};
}
你能提供更多的信息嗎?例如:死鎖發生時如何調用該方法?它是調用'GetOrCreateActor'的'async'方法嗎? – easuter
我已編輯該問題以提供更多信息。 – Kelly