Exemple #1
0
 static internal void ProcessEventInternal(CachingMessage message)
 {
     if (message.From != ServerController.GetExecutingServerName() &&
         !(string.IsNullOrEmpty(message.Message)) && !(string.IsNullOrEmpty(message.Data)))
     {
         var cachingProvider = CachingProvider.Instance();
         if (cachingProvider is ServiceBusCachingProvider)
         {
             (cachingProvider as ServiceBusCachingProvider).ProcessMessage(message.Message, message.Data);
         }
     }
 }
        private async Task SendEventHubNotificationAsync(string message, string data)
        {
            // On installation and upgrades, don't send cache syncs to avoid installation failures
            if (HttpContext.Current != null && IsUpgradeRequest(HttpContext.Current.Request))
            {
                return;
            }

            if (CachingConfig != null && CachingConfig.CacheExclusions.Any(x => x.Message == message && x.Data == data))
            {
                Logger.Debug($"Message excluded by caching exclusions (message: {message}; data: {data}");
                return;
            }

            // Check if there is more than servers than self
            if (!bool.TryParse(ConfigurationManager.AppSettings["ServiceBusCachingProvider.Debug"], out var isDebug) && !isDebug)
            {
                var self = ServerController.GetServers().Single(s => s.ServerName == Globals.ServerName && s.IISAppName == Globals.IISAppName);
                if (ServerController.GetEnabledServers().Where(s => !(s.ServerName == self.ServerName &&
                                                                      s.IISAppName == self.IISAppName) &&
                                                               (s.ServerGroup == self.ServerGroup || string.IsNullOrEmpty(self.ServerGroup)) &&
                                                               !string.IsNullOrEmpty(s.Url))
                    .Count() == 0)
                {
                    return;
                }
            }

            var client      = ServiceBus.GetTopicClientFromPool();
            var serverName  = ServerController.GetExecutingServerName();
            var messageData = new CachingMessage
            {
                From    = serverName,
                Message = message,
                Data    = data
            };
            var messageBody = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(messageData)).ConfigureAwait(false);

            if (client == null)
            {
                Logger.Warn($"Service bus disabled: {messageBody}");
            }
            else
            {
                Logger.Debug($"Sending message: {messageBody}");
                await client.SendAsync(new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(messageBody))).ConfigureAwait(false);
            }
        }