static void Deactivate(KeyValuePair <ActorId, ActorInfo> pair, ConcurrentDictionary <ActorId, ActorInfo> instances)
        {
            MethodInfo initializeInstance   = m_InitializeInstanceDefinition.MakeGenericMethod(pair.Value.ActorImplementationType, typeof(IActor));
            ChannelFactory <IActor> factory = initializeInstance.Invoke(null, new object[] { null, new ProxyMessageInterceptor(pair.Value.ActorId), ActorProxy.ProxyBinding, ActorProxy.ActorBinding }) as ChannelFactory <IActor>;
            IActor proxy = new ActorChannelInvoker <IActor>().Install(factory, pair.Value.ActorId);

            proxy.DeactivateAsync().Wait();
            factory.Close();
        }
        public static I Create <I>(ActorId actorId, string applicationName = null, string serviceName = null) where I : class, IActor
        {
            Debug.Assert(FabricRuntime.Actors.ContainsKey(applicationName));

            Type actorType = FabricRuntime.Actors[applicationName].SingleOrDefault(type => type.GetInterfaces().Where(interfaceType => ((IsInfrastructureEndpoint(interfaceType) == false) && (interfaceType == typeof(I)))).Any());

            Debug.Assert(actorType != null);
            Debug.Assert(actorType.BaseType != null);

            bool actorExists = actorType.GetCustomAttributes <ApplicationManifestAttribute>().Where(attribute => attribute.ApplicationName.Equals(applicationName)).Any(attribute => attribute.ServiceName.Equals(serviceName));

            Debug.Assert(actorExists);
            if (!actorExists)
            {
                throw new InvalidOperationException("Service does not exist.");
            }

            IServiceBehavior actorBehavior = null;

            if (actorType.BaseType.Equals(typeof(StatelessActor)))
            {
                actorBehavior = new StatelessActorBehavior();
            }
            else if (actorType.BaseType.IsGenericType)
            {
                Type genericTypeDef = actorType.BaseType.GetGenericTypeDefinition();
                if (genericTypeDef.Equals(typeof(StatefulActor <>)))
                {
                    actorBehavior = new StatefulActorBehavior();
                }
            }

            if (actorBehavior == null)
            {
                throw new InvalidOperationException("Validation failed.");
            }

            if ((ServiceTestBase.ActorMocks != null) && (ServiceTestBase.ActorMocks.ContainsKey(actorType)) && (ServiceTestBase.ActorMocks[actorType] != null))
            {
                if (ServiceTestBase.ActorMocks[actorType] is Moq.Mock)
                {
                    return(((Moq.Mock)ServiceTestBase.ActorMocks[actorType]).Object as I);
                }
                else
                {
                    return(ServiceTestBase.ActorMocks[actorType] as I);
                }
            }

            try
            {
                actorId.ApplicationName    = applicationName;
                actorId.ActorInterfaceName = typeof(I).FullName;

                MethodInfo         initializeInstance = m_InitializeInstanceDefinition.MakeGenericMethod(actorType, typeof(I));
                ChannelFactory <I> factory            = initializeInstance.Invoke(null, new object[] { actorBehavior, new ProxyMessageInterceptor(actorId), m_ProxyBinding, m_ActorBinding }) as ChannelFactory <I>;
                I channel = new ActorChannelInvoker <I>().Install(factory, actorId);
                channel.ActivateAsync().Wait();
                return(channel);
            }
            catch (TargetInvocationException exception)
            {
                throw exception.InnerException;
            }
        }