internal static I Create <I>(string applicationName, string serviceName, ProxyRemotingInvoker invoker, ProxyRemotingInterceptor remotingInterceptor) where I : class, IService
        {
            Debug.Assert(FabricRuntime.Services != null, "Use ServiceProxy only within Service Fabric");
            Debug.Assert(FabricRuntime.Services.ContainsKey(applicationName));

            Type serviceType = FabricRuntime.Services[applicationName].SingleOrDefault(type => type.GetInterfaces().Where(interfaceType => interfaceType == typeof(I)).Any());

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

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

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

            IServiceBehavior serviceBehavior = null;

            if (serviceType.IsSubclassOf(typeof(StatelessService)))
            {
                serviceBehavior = new StatelessServiceBehavior();
            }
            else
            {
                throw new InvalidOperationException("Validation failed.");
            }

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

            try
            {
                ServiceContext context = new ServiceContext {
                    ServiceName = new Uri("fabric:/" + applicationName + "/" + serviceName)
                };
                MethodInfo         initializeInstance = m_InitializeInstanceDefinition.MakeGenericMethod(serviceType, typeof(I));
                ChannelFactory <I> factory            = initializeInstance.Invoke(null, new object[] { serviceBehavior, new ProxyMessageInterceptor(context, remotingInterceptor), m_ProxyBinding, m_ServiceBinding }) as ChannelFactory <I>;
                I channel = new ServiceChannelInvoker <I>().Install(factory, invoker);
                return(channel);
            }
            catch (TargetInvocationException exception)
            {
                throw exception.InnerException;
            }
        }
Exemple #2
0
        internal static I Create <I>(ActorId actorId, string applicationName, string serviceName, ProxyRemotingInvoker invoker, ProxyRemotingInterceptor remotingInterceptor) where I : class, IActor
        {
            Debug.Assert(FabricRuntime.Actors.ContainsKey(applicationName), "Invalid application name '" + applicationName + "' in service Uri.");

            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, "Invalid actor name '" + serviceName + "' in service Uri.");
            if (!actorExists)
            {
                throw new InvalidOperationException("Service does not exist.");
            }

            IServiceBehavior          actorBehavior   = null;
            StatePersistenceAttribute persistenceMode = actorType.GetCustomAttribute <StatePersistenceAttribute>();

            if (persistenceMode == null)
            {
                throw new InvalidOperationException("Validation failed. Actor " + actorType.Name + " is missing a StatePersistenceAttribute.");
            }
            else
            {
                actorBehavior = new StatefulActorBehavior();
            }

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

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

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

                ServiceContext context = new ServiceContext {
                    ServiceName = new Uri("fabric:/" + applicationName + "/" + serviceName)
                };
                MethodInfo         initializeInstance = m_InitializeInstanceDefinition.MakeGenericMethod(actorType, typeof(I));
                ChannelFactory <I> factory            = initializeInstance.Invoke(null, new object[] { actorBehavior, new ProxyMessageInterceptor(actorId, context, remotingInterceptor), m_ProxyBinding, m_ActorBinding }) as ChannelFactory <I>;
                I channel = new ActorChannelInvoker <I>().Install(factory, actorId, invoker);
                channel.ActivateAsync().Wait();
                return(channel);
            }
            catch (TargetInvocationException exception)
            {
                throw exception.InnerException;
            }
        }
Exemple #3
0
 public ProxyMessageInterceptor(ActorId actorId, ServiceContext context, ProxyRemotingInterceptor remotingInterceptor)
 {
     ActorId             = actorId;
     Context             = context;
     RemotingInterceptor = remotingInterceptor;
 }