private static Actor Start(ConstructorInfo ctor, IAddress address, Definition definition, ILogger logger) { Actor actor = null; object[] args = null; Exception cause = null; for (var times = 1; times <= 2; ++times) { try { if (times == 1) { args = definition.InternalParameters().ToArray(); } actor = (Actor)ctor.Invoke(definition.InternalParameters().ToArray()); actor.LifeCycle.SendStart(actor); cause = null; return(actor); } catch (Exception ex) { cause = ex.InnerException ?? ex; if (times == 1) { args = Unfold(args); } } } if (cause != null) { logger.Log("ActorFactory: failed actor creation. " + "This is sometimes cause by the constructor parameter types not matching " + "the types in the Definition.parameters(). Often it is caused by a " + "failure in the actor constructor. We have attempted to uncover " + "the root cause here, but that may not be available in some cases.\n" + "The root cause may be: " + cause.Message + "\n" + "See stacktrace for more information. We strongly recommend reviewing your " + "constructor for possible failures in dependencies that it creates.", cause); throw new ArgumentException($"ActorFactory failed actor creation for: {address}"); } return(actor); }
internal static Actor ActorFor( Stage stage, Actor?parent, Definition definition, IAddress address, IMailbox mailbox, ISupervisor?supervisor, ILogger logger) { var environment = new Environment( stage, address, definition, parent, mailbox, supervisor, ActorLoggerAdapter.From(logger, address, definition.Type !)); ThreadLocalEnvironment.Value = environment; Actor?actor = null; var definitionParameterCount = definition.InternalParameters().Count(); if (definitionParameterCount == 0) { actor = (Actor)Activator.CreateInstance(definition.Type); actor.LifeCycle.SendStart(actor); } else { foreach (var ctor in definition.Type !.GetConstructors()) { if (ctor.GetParameters().Length != definitionParameterCount) { continue; } actor = Start(ctor, address, definition, logger); if (actor != null) { break; } } } if (actor == null) { throw new MissingMethodException("No constructor matches the given number of parameters."); } if (parent != null) { parent.LifeCycle.Environment.AddChild(actor); } return(actor); }
internal static Actor ActorFor( Stage stage, Actor?parent, Definition definition, IAddress address, IMailbox mailbox, ISupervisor?supervisor, ILogger logger) { var environment = new Environment( stage, address, definition, parent, mailbox, supervisor, ActorLoggerAdapter.From(logger, address, definition.Type !)); ThreadLocalEnvironment.Value = environment; Actor?actor = null; var definitionParameterCount = definition.InternalParameters().Count(); if (definitionParameterCount == 0) { if (definition.Type != null) { actor = (Actor)Activator.CreateInstance(definition.Type) !; actor.LifeCycle.SendStart(actor); } } else { foreach (var ctor in definition.Type !.GetConstructors()) { var expectedConstructorParameters = definition.InternalParameters().ToList(); var actualParametersInfosOfConstructor = ctor.GetParameters(); if (actualParametersInfosOfConstructor.Length != definitionParameterCount) { continue; } var differentParameterType = false; for (var i = 0; i < actualParametersInfosOfConstructor.Length; i++) { if (actualParametersInfosOfConstructor[i].ParameterType != expectedConstructorParameters[i].GetType() && !actualParametersInfosOfConstructor[i].ParameterType.IsAssignableFrom(expectedConstructorParameters[i].GetType())) { differentParameterType = true; break; } } if (differentParameterType) { continue; } actor = Start(ctor, address, definition, logger); if (actor != null) { break; } } } if (actor == null) { throw new MissingMethodException("No constructor matches the given number of parameters."); } if (parent != null) { parent.LifeCycle.Environment.AddChild(actor); } return(actor); }
internal static Actor ActorFor( Stage stage, Actor parent, Definition definition, IAddress address, IMailbox mailbox, ISupervisor supervisor, ILogger logger) { var environment = new Environment( stage, address, definition, parent, mailbox, supervisor, logger); ThreadLocalEnvironment.Value = environment; Actor actor = null; int definitionParameterCount = definition.InternalParameters().Count; if (definitionParameterCount == 0) { actor = (Actor)Activator.CreateInstance(definition.Type); } else { foreach (var ctor in definition.Type.GetConstructors()) { if (ctor.GetParameters().Length != definitionParameterCount) { continue; } try { actor = (Actor)ctor.Invoke(definition.InternalParameters().ToArray()); actor.LifeCycle.SendStart(actor); } catch (Exception ex) { var cause = ex.InnerException ?? ex; logger.Log("ActorFactory: failed actor creation. " + "This is sometimes cause be the constructor parameter types not matching " + "the types in the Definition.parameters(). Often it is caused by a " + "failure in the actor constructor. We have attempted to uncover " + "the root cause here, but that may not be available in some cases.\n" + "The root cause may be: " + cause.Message + "\n" + "See stacktrace for more information. We strongly recommend reviewing your " + "constructor for possible failures in dependencies that it creates.", cause); } break; } } if (actor == null) { throw new ArgumentException("No constructor matches the given number of parameters."); } if (parent != null) { parent.LifeCycle.Environment.AddChild(actor); } return(actor); }
internal static Actor ActorFor( Stage stage, Actor parent, Definition definition, Address address, IMailbox mailbox, ISupervisor supervisor, ILogger logger) { var environment = new Environment( stage, address, definition, parent, mailbox, supervisor, logger); ThreadLocalEnvironment.Value = environment; Actor actor = null; int definitionParameterCount = definition.InternalParameters().Count; if (definitionParameterCount == 0) { actor = (Actor)Activator.CreateInstance(definition.Type); } else { foreach (var ctor in definition.Type.GetConstructors()) { if (ctor.GetParameters().Length != definitionParameterCount) { continue; } try { actor = (Actor)ctor.Invoke(definition.InternalParameters().ToArray()); actor.LifeCycle.SendStart(actor); } catch (Exception ex) { logger.Log($"vlingo-net/actors: ActorFactory: failed because: {ex.Message}", ex); Console.WriteLine(ex.StackTrace); } break; } } if (actor == null) { throw new ArgumentException("No constructor matches the given number of parameters."); } if (parent != null) { parent.LifeCycle.Environment.AddChild(actor); } return(actor); }