예제 #1
0
        /// <summary>
        /// Registers this Actor as a subscriber for messages of type <paramref name="messageType"/> with the <see cref="BrokerService"/>.
        /// </summary>
        /// <returns></returns>
        public async Task RegisterMessageTypeAsync(StatefulService service, Type messageType,
                                                   Uri brokerServiceName = null, string listenerName = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }
            if (brokerServiceName == null)
            {
                brokerServiceName = await _brokerServiceLocator.LocateAsync();

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException(
                              "No brokerServiceName was provided or discovered in the current application.");
                }
            }
            var brokerService =
                await _brokerServiceLocator.GetBrokerServiceForMessageAsync(messageType.Name, brokerServiceName);

            var serviceReference = CreateServiceReference(service.Context, GetServicePartition(service).PartitionInfo, listenerName);
            await brokerService.RegisterServiceSubscriberAsync(serviceReference, messageType.FullName);
        }
예제 #2
0
        /// <summary>
        /// This is the main entry point for your service instance.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            var brokerServiceName = await _brokerServiceLocator.LocateAsync();

            //subscribe to messages by their type name:
            string setting = GetConfigurationValue(Context, Messagesettings, "MessageTypeCount");

            if (string.IsNullOrWhiteSpace(setting) || !int.TryParse(setting, out _messageTypeCount))
            {
                return;
            }
            int amount;

            setting = GetConfigurationValue(Context, Messagesettings, "Amount");
            if (string.IsNullOrWhiteSpace(setting) || !int.TryParse(setting, out amount))
            {
                return;
            }
            _messagesExpectedCount = amount;

            bool useConcurrentBroker = false;

            setting = GetConfigurationValue(Context, Messagesettings, "UseConcurrentBroker");
            if (!string.IsNullOrWhiteSpace(setting))
            {
                bool.TryParse(setting, out useConcurrentBroker);
            }

            for (int i = 0; i < _messageTypeCount; i++)
            {
                string messageTypeName = $"DataContract{i}";
                await SubscribeAsync(messageTypeName, useConcurrentBroker);

                ServiceEventSource.Current.ServiceMessage(this,
                                                          $"Subscribing to {amount} instances of Message Type {messageTypeName}.");
            }

            Reset();

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
                }
                catch (OperationCanceledException)
                {
                }
            }

            ServiceEventSource.Current.ServiceMessage(this,
                                                      $"Instance {Context.InstanceId} stopping. Total counts:{string.Join(", ", _messagesReceived.Select(m => $"Message Type '{m.Key}' - {m.Value.Count}"))}.");
        }
        /// <summary>
        /// Unregisters this Actor as a subscriber for messages of type <paramref name="messageType"/> with the <see cref="BrokerService"/>.
        /// </summary>
        /// <returns></returns>
        public async Task UnregisterMessageTypeAsync(StatefulService service, Type messageType, bool flushQueue,
                                                     Uri brokerServiceName = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }
            if (brokerServiceName == null)
            {
                brokerServiceName = await _brokerServiceLocator.LocateAsync();

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException(
                              "No brokerServiceName was provided or discovered in the current application.");
                }
            }
            var brokerService =
                await _brokerServiceLocator.GetBrokerServiceForMessageAsync(messageType.Name, brokerServiceName);

            var serviceReference = CreateServiceReference(service);
            await brokerService.UnregisterServiceSubscriberAsync(serviceReference, messageType.FullName, flushQueue);
        }
        /// <summary>
        /// Publish a message.
        /// </summary>
        /// <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(object message, Uri brokerServiceName = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (brokerServiceName == null)
            {
                brokerServiceName = await _brokerServiceLocator.LocateAsync();

                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);
        }