示例#1
0
        /// <summary>
        /// Publish a message.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="message"></param>
        /// <param name="brokerServiceName">The name of a SF Service of type <see cref="BrokerService"/>.</param>
        /// <returns></returns>
        public async Task PublishMessageAsync(StatefulServiceBase service, object message,
                                              Uri brokerServiceName = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (brokerServiceName == null)
            {
                brokerServiceName = await DiscoverBrokerServiceNameAsync();

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException(
                              "No brokerServiceName was provided or discovered in the current application.");
                }
            }

            var brokerService = await _brokerServiceLocator.GetBrokerServiceForMessageAsync(message, brokerServiceName);

            var wrapper = MessageWrapper.CreateMessageWrapper(message);
            await brokerService.PublishMessageAsync(wrapper);
        }
示例#2
0
        public static async Task PublishMessageToBrokerServiceAsync(this StatefulServiceBase service, object message,
                                                                    Uri brokerServiceName = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (brokerServiceName == null)
            {
                brokerServiceName =
                    await
                    DiscoverBrokerServiceNameAsync(
                        new Uri(service.Context.CodePackageActivationContext.ApplicationName));

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException(
                              "No brokerServiceName was provided or discovered in the current application.");
                }
            }

            var brokerService =
                await PublisherActorExtensions.GetBrokerServiceForMessageAsync(message, brokerServiceName);

            var wrapper = message.CreateMessageWrapper();
            await brokerService.PublishMessageAsync(wrapper);
        }
        /// <summary>
        /// Invokes RunAsync on the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task InvokeRunAsync(this StatefulServiceBase service, CancellationToken?cancellationToken = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected virtual Task RunAsync(CancellationToken cancellationToken)
            var method = FindMethodInfo(service, "RunAsync");
            var task   = Task.Run(() =>
            {
                Task inner = null;
                try
                {
                    inner = (Task)method.Invoke(service, new object[] { cancellationToken ?? CancellationToken.None });
                }
                catch (TargetInvocationException ex)
                {
                    //If an OperationCanceledException escapes from RunAsync(CancellationToken) and
                    //Service Fabric runtime has requested cancellation by signaling cancellationToken
                    //passed to RunAsync(CancellationToken), Service Fabric runtime handles this
                    //exception and considers it as graceful completion of RunAsync(CancellationToken).
                    if (!(ex.InnerException is OperationCanceledException))
                    {
                        //otherwise, we don't know what happened...
                        throw;
                    }
                    inner = Task.FromResult(true);
                }
                return(inner);
            });

            return(task);
        }
 /// <summary>
 /// Publish a message.
 /// </summary>
 /// <param name="service"></param>
 /// <param name="message"></param>
 /// <param name="brokerServiceName">The name of a SF Service of type <see cref="BrokerService"/>.</param>
 /// <returns></returns>
 public async Task PublishMessageAsync(StatefulServiceBase service, object message,
                                       Uri brokerServiceName = null)
 {
     if (service == null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     await PublishMessageAsync(message, brokerServiceName);
 }
示例#5
0
        /// <summary>
        /// Sets the partition info of the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="partition">partition to set</param>
        /// <param name="service"></param>
        /// <returns></returns>
        public static void SetPartition(this StatefulServiceBase service, IStatefulServicePartition partition)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected IStatefulServicePartition Partition { get; private set; }
            var propertyInfo = typeof(StatefulServiceBase).GetProperty("Partition", BindingFlags.Instance | BindingFlags.NonPublic);

            propertyInfo?.SetValue(service, partition, BindingFlags.Instance | BindingFlags.NonPublic, null, null, CultureInfo.InvariantCulture);
        }
示例#6
0
        /// <summary>
        /// Invokes RunAsync on the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task InvokeRunAsync(this StatefulServiceBase service, CancellationToken?cancellationToken = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected virtual Task RunAsync(CancellationToken cancellationToken)
            var method = FindMethodInfo(service, "RunAsync");

            return((Task)method.Invoke(service, new object[] { cancellationToken ?? CancellationToken.None }));
        }
 /// <summary>
 /// Gets the Partition info for the provided StatefulServiceBase instance.
 /// </summary>
 /// <param name="serviceBase"></param>
 /// <returns></returns>
 private IStatefulServicePartition GetServicePartition(StatefulServiceBase serviceBase)
 {
     if (serviceBase == null)
     {
         throw new ArgumentNullException(nameof(serviceBase));
     }
     return((IStatefulServicePartition)serviceBase
            .GetType()
            .GetProperty("Partition", BindingFlags.Instance | BindingFlags.NonPublic)?
            .GetValue(serviceBase) ?? throw new ArgumentNullException($"Unable to find partition information for service: {serviceBase}"));
 }
 /// <summary>
 /// Gets the Partition info for the provided StatefulServiceBase instance.
 /// </summary>
 /// <param name="serviceBase"></param>
 /// <returns></returns>
 private static IStatefulServicePartition GetServicePartition(this StatefulServiceBase serviceBase)
 {
     if (serviceBase == null)
     {
         throw new ArgumentNullException(nameof(serviceBase));
     }
     return((IStatefulServicePartition)serviceBase
            .GetType()
            .GetProperty("Partition", BindingFlags.Instance | BindingFlags.NonPublic)
            .GetValue(serviceBase));
 }
示例#9
0
        public MockStatefulServiceReplica(
            StatefulServiceBase serviceReplica)
        {
            this.serviceReplica = serviceReplica;
            Injector.InjectProperty(this.serviceReplica, "Partition", new MockStatefulServicePartition(), true);

            if (this.serviceReplica is IStatefulService statefulService)
            {
                statefulService.GetEventSource(); // Provoke event source initialization
            }
        }
 /// <summary>
 /// Registers a service as a subscriber for messages of type <paramref name="messageType"/>.
 /// </summary>
 /// <param name="service">The service registering itself as a subscriber for messages of type <paramref name="messageType"/></param>
 /// <param name="messageType">The type of message to register for (each message type has its own <see cref="IBrokerActor"/> instance)</param>
 /// <param name="listenerName">(optional) The name of the listener that is used to communicate with the service</param>
 /// <returns></returns>
 public static Task RegisterMessageTypeAsync(this StatefulServiceBase service, Type messageType, string listenerName = null)
 {
     if (service == null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     if (messageType == null)
     {
         throw new ArgumentNullException(nameof(messageType));
     }
     return(RegisterMessageTypeAsync(service.Context, service.GetServicePartition().PartitionInfo, messageType, listenerName));
 }
 /// <summary>
 /// Unregisters a service as a subscriber for messages of type <paramref name="messageType"/>.
 /// </summary>
 /// <param name="service">The service unregistering itself as a subscriber for messages of type <paramref name="messageType"/></param>
 /// <param name="messageType">The type of message to unregister for (each message type has its own <see cref="IBrokerActor"/> instance)</param>
 /// <param name="flushQueue">Publish any remaining messages.</param>
 /// <returns></returns>
 public static Task UnregisterMessageTypeAsync(this StatefulServiceBase service, Type messageType, bool flushQueue)
 {
     if (service == null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     if (messageType == null)
     {
         throw new ArgumentNullException(nameof(messageType));
     }
     return(UnregisterMessageTypeAsync(service.Context, service.GetServicePartition().PartitionInfo, messageType, flushQueue));
 }
示例#12
0
        /// <summary>
        /// Invokes OnOpenAsync on the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="replicaOpenMode"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task InvokeOnOpenAsync(this StatefulServiceBase service,
                                             ReplicaOpenMode replicaOpenMode     = ReplicaOpenMode.Existing,
                                             CancellationToken?cancellationToken = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected virtual Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
            var method = FindMethodInfo(service, "OnOpenAsync");

            return((Task)method.Invoke(service, new object[] { replicaOpenMode, cancellationToken ?? CancellationToken.None }));
        }
        public void ServiceException(StatefulServiceBase service, Exception exception)
        {
            if (!IsEnabled())
            {
                return;
            }

            ServiceException(
                service.Context.ServiceName.ToString(),
                service.Context.ReplicaOrInstanceId.ToString(),
                service.Context.PartitionId.ToString(),
                exception.ToString());
        }
        /// <summary>
        /// Deserializes the provided <paramref name="message"/> Payload into an intance of type <typeparam name="TResult"></typeparam>
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        public static TResult Deserialize <TResult>(this StatefulServiceBase service, MessageWrapper message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (string.IsNullOrWhiteSpace(message.Payload))
            {
                throw new ArgumentNullException(nameof(message.Payload));
            }

            var payload = JsonConvert.DeserializeObject <TResult>(message.Payload);

            return(payload);
        }
        /// <summary>
        /// Deserializes the provided <paramref name="messageWrapper"/> Payload into an instance of type <typeparam name="TResult"></typeparam>
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        public static TResult Deserialize <TResult>(this StatefulServiceBase service, MessageWrapper messageWrapper)
        {
            if (messageWrapper == null)
            {
                throw new ArgumentNullException(nameof(messageWrapper));
            }
            if (string.IsNullOrWhiteSpace(messageWrapper.Payload))
            {
                throw new ArgumentNullException(nameof(messageWrapper.Payload));
            }

            var payload = messageWrapper.CreateMessage <TResult>();

            return(payload);
        }
        public void ServiceError(StatefulServiceBase service, string message, params object[] args)
        {
            if (!IsEnabled())
            {
                return;
            }

            var finalMessage = string.Format(message, args);

            ServiceError(
                service.Context.ServiceName.ToString(),
                service.Context.ReplicaOrInstanceId.ToString(),
                service.Context.PartitionId.ToString(),
                finalMessage);
        }
 public void ServiceMessage(StatefulServiceBase service, string message, params object[] args)
 {
     if (this.IsEnabled())
     {
         string finalMessage = string.Format(message, args);
         ServiceMessage(
             service.Context.ServiceName.ToString(),
             service.Context.ServiceTypeName,
             service.Context.ReplicaOrInstanceId,
             service.Context.PartitionId,
             service.Context.CodePackageActivationContext.ApplicationName,
             service.Context.CodePackageActivationContext.ApplicationTypeName,
             service.Context.NodeContext.NodeName,
             finalMessage);
     }
 }
        /// <summary>
        /// Unregisters a Service as a subscriber for messages of type <paramref name="messageType"/> using a <see cref="IRelayBrokerActor"/> approach.
        /// </summary>
        /// <param name="service">The service registering itself as a subscriber for messages of type <paramref name="messageType"/></param>
        /// <param name="messageType">The type of message to unregister for (each message type has its own <see cref="IBrokerActor"/> instance)</param>
        /// <param name="relayBrokerActorId">The ID of the relay broker to unregister with.</param>
        /// <param name="sourceBrokerActorId">(optional) The ID of the source <see cref="IBrokerActor"/> that was used as the source for the <paramref name="relayBrokerActorId"/>
        /// If not specified, the default <see cref="IBrokerActor"/> for the message type <paramref name="messageType"/> will be used.</param>
        /// <param name="flushQueue">Publish any remaining messages.</param>
        /// <returns></returns>
        public static Task UnregisterMessageTypeWithRelayBrokerAsync(this StatefulServiceBase service, Type messageType, ActorId relayBrokerActorId, ActorId sourceBrokerActorId, bool flushQueue)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }
            if (relayBrokerActorId == null)
            {
                throw new ArgumentNullException(nameof(relayBrokerActorId));
            }

            return(UnregisterMessageTypeWithRelayBrokerAsync(service.Context, service.GetServicePartition().PartitionInfo, messageType, relayBrokerActorId, sourceBrokerActorId, flushQueue));
        }
示例#19
0
        public static async Task PublishMessageAsync(this StatefulServiceBase service, object message,
                                                     string applicationName = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (string.IsNullOrWhiteSpace(applicationName))
            {
                applicationName = service.Context.CodePackageActivationContext.ApplicationName;
            }

            var brokerActor = GetBrokerActorForMessage(applicationName, message);
            var wrapper     = message.CreateMessageWrapper();
            await brokerActor.PublishMessageAsync(wrapper);
        }
示例#20
0
        /// <summary>
        /// Invokes CreateServiceReplicaListeners on the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        public static IEnumerable <ServiceReplicaListener> InvokeCreateServiceReplicaListeners(this StatefulServiceBase service)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected virtual IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
            var method = FindMethodInfo(service, "CreateServiceReplicaListeners");

            return((IEnumerable <ServiceReplicaListener>)method.Invoke(service, null));
        }
示例#21
0
 public StatefulServiceLogger(StatefulServiceBase service)
 {
     _service = service;
 }
 /// <summary>
 /// Gets a reference to a StatefulService.
 /// </summary>
 /// <param name="service">The service.</param>
 /// <returns><see cref="ServiceReference"/></returns>
 public static ServiceReference GetServiceReference(this StatefulServiceBase service)
 {
     return(CreateServiceReference(service.Context, GetServicePartition(service).PartitionInfo));
 }
 public static ConfigurationSection ReadCustomConfigurationSection(this StatefulServiceBase service, string customConfigurationSectionName) =>
 ReadCustomConfigurationSection(service.Context.CodePackageActivationContext, customConfigurationSectionName);