コード例 #1
0
        public async Task <bool> Publish(IPublication publication, string publisherName)
        {
            while (!ReliableDictionariesInitialized)
            {
                await Task.Delay(1000);
            }

            bool success;

            try
            {
                ///Could be sole Task running in RunAsync, while reading from a queue...
                List <Task> tasks = new List <Task>();
                short       key   = (short)publication.Topic;

                var enumerableSubscribersCache = await RegisteredSubscribersCache.GetEnumerableDictionaryAsync();

                if (enumerableSubscribersCache.ContainsKey(key))
                {
                    var registeredSubscribers = enumerableSubscribersCache[key];

                    foreach (var subscriberName in registeredSubscribers)
                    {
                        var notifySubscriberClient = NotifySubscriberClient.CreateClient(subscriberName);
                        var task = notifySubscriberClient.Notify(publication.Message, publisherName);
                        tasks.Add(task);
                    }
                }
                ///////////

                Task.WaitAll(tasks.ToArray());
                success = true;

                Logger.LogInformation($"{baseLogString} Publish => SUCCESSFULL publication. PublicationType: {publication.GetType()}, MessageType: {publication.Message.GetType()}, Topic: {publication.Topic}");
            }
            catch (Exception e)
            {
                string errorMessage = $"{baseLogString} Publish => Exception caught.";
                Logger.LogError(errorMessage, e);
                success = false;
            }

            return(success);
        }