Exemplo n.º 1
0
        /// <summary>
        /// Create virtual actor, return current instance or create one
        /// </summary>
        /// <typeparam name="T">actor interface</typeparam>
        /// <param name="context">context</param>
        /// <param name="actorKey">actor key</param>
        /// <returns>actor proxy interface</returns>
        public async Task <T> CreateProxyAsync <T>(IWorkContext context, ActorKey actorKey) where T : IActor
        {
            Verify.Assert(IsRunning, _disposedTestText);

            Verify.IsNotNull(nameof(context), context);
            Verify.IsNotNull(nameof(context), context);
            Verify.IsNotNull(nameof(actorKey), actorKey);
            context = context.WithTag(_tag);

            Type actorType = typeof(T);

            // Lookup instance of actor (type + actorKey)
            IActorRegistration actorRegistration = _actorRepository.Lookup(actorType, actorKey);

            if (actorRegistration != null)
            {
                return(actorRegistration.GetInstance <T>());
            }

            // Create actor
            IActor actorObject = _typeManager.Create <T>(context, actorKey, this);

            IActorBase actorBase = actorObject as IActorBase;

            if (actorBase == null)
            {
                var ex = new ArgumentException($"Actor {actorObject.GetType().FullName} does not implement IActorBase");
                Configuration.WorkContext.EventLog.Error(context, "Cannot create", ex);
                throw ex;
            }

            T actorInterface = ActorProxy <T> .Create(context, actorBase, this);

            actorRegistration = new ActorRegistration(typeof(T), actorKey, actorBase, actorInterface);

            await _actorRepository.SetAsync(context, actorRegistration);

            // Create proxy for interface
            return(actorRegistration.GetInstance <T>());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create proxy to actor, return current instance or create one
        /// </summary>
        /// <typeparam name="T">actor interface</typeparam>
        /// <param name="context">context</param>
        /// <param name="actorKey">actor key</param>
        /// <returns>actor proxy interface</returns>
        public async Task <T> CreateProxy <T>(ActorKey actorKey) where T : IActor
        {
            Verify.Assert(IsRunning, _disposedTestText);

            actorKey.Verify(nameof(actorKey)).IsNotNull();

            Type actorType = typeof(T);

            // Lookup instance of actor (type + actorKey)
            IActorRegistration?actorRegistration = _actorRepository.Lookup(actorType, actorKey);

            if (actorRegistration != null)
            {
                return(actorRegistration.GetInstance <T>());
            }

            // Create actor
            IActor actorObject = _typeManager.Create <T>(Configuration.WorkContext.With(_tag), actorKey, this);

            IActorBase?actorBase = actorObject as IActorBase;

            if (actorBase == null)
            {
                var ex = new ArgumentException($"Actor {actorObject.GetType().FullName} does not implement IActorBase");
                Configuration.WorkContext.Telemetry.Error(Configuration.WorkContext.With(_tag), "Cannot create", ex);
                throw ex;
            }

            // Create proxy
            T actorInterface = ActorProxy <T> .Create(Configuration.WorkContext.With(_tag), actorBase, this);

            actorRegistration = new ActorRegistration(typeof(T), actorKey, actorBase, actorInterface);

            await _actorRepository.Set(Configuration.WorkContext.With(_tag), actorRegistration).ConfigureAwait(false);

            // Create proxy for interface
            return(actorRegistration.GetInstance <T>());
        }