예제 #1
0
		/// <summary>
		/// Refreshes <see cref="EventBlackListProcessing"/> and every item currently in <see cref="CachedChecks"/>.
		/// </summary>
		protected virtual void RefreshCachedChecks()
		{
			// First refresh the EventBlackListProcessing property
			bool isblackListRequired;
			if (!ConfigurationManager.TryGetSetting("Cqrs.MessageBus.BlackListProcessing", out isblackListRequired))
				isblackListRequired = true;
			EventBlackListProcessing = isblackListRequired;

			// Now in a dictionary safe way check each key for a value.
			IList<string> keys = CachedChecks.Keys.ToList();
			foreach (string configurationKey in keys)
			{
				Tuple<bool, DateTime> pair = CachedChecks[configurationKey];
				bool value;
				// If we can't a value or there is no specific setting, remove it from the cache
				if (!ConfigurationManager.TryGetSetting(configurationKey, out value))
					CachedChecks.Remove(configurationKey);
				// Refresh the value and reset it's expiry if the value has changed
				else if (pair.Item1 != value)
					CachedChecks[configurationKey] = new Tuple<bool, DateTime>(value, DateTime.UtcNow);
				// Check it's age - by adding 20 minutes from being obtained or refreshed and if it's older than now remove it
				else if (pair.Item2.AddMinutes(20) < DateTime.UtcNow)
					CachedChecks.Remove(configurationKey);
			}

			// Now in a dictionary safe way check each key for a value.
			keys = NullableCachedChecks.Keys.ToList();
			foreach (string configurationKey in keys)
			{
				Tuple<bool?, DateTime> pair = NullableCachedChecks[configurationKey];
				// Check it's age - by adding 20 minutes from being obtained or refreshed and if it's older than now remove it
				if (pair.Item2.AddMinutes(20) < DateTime.UtcNow)
					NullableCachedChecks.Remove(configurationKey);
			}
		}
예제 #2
0
        /// <summary>
        /// Checks if the particular bus is required to send the message. Note, this does not imply the public bus is not required as well.
        /// </summary>
        /// <param name="messageType">The <see cref="Type"/> of the message being processed.</param>
        /// <param name="checkPublic">Check for the public or private bus.</param>
        /// <returns>Null for unconfigured, True for a particular bus transmission, false otherwise.</returns>
        protected virtual bool?IsABusRequired(Type messageType, bool checkPublic)
        {
            string configurationKey = string.Format(checkPublic ? "{0}.IsPublicBusRequired" : "{0}.IsPrivateBusRequired", messageType.FullName);
            Tuple <bool?, DateTime> settings;
            bool?isRequired;

            if (!NullableCachedChecks.TryGetValue(configurationKey, out settings))
            {
                bool isRequired1;
                // Check if there is a cached value
                if (ConfigurationManager.TryGetSetting(configurationKey, out isRequired1))
                {
                    isRequired = isRequired1;
                }
                // If not, check the attributes
                else if (checkPublic)
                {
                    var eventAttribute = Attribute.GetCustomAttribute(messageType, typeof(PublicEventAttribute)) as PublicEventAttribute;
                    isRequired = eventAttribute == null ? (bool?)null : true;
                }
                // If not, check the attributes
                else
                {
                    var eventAttribute = Attribute.GetCustomAttribute(messageType, typeof(PrivateEventAttribute)) as PrivateEventAttribute;
                    isRequired = eventAttribute == null ? (bool?)null : true;
                }

                // Now cache the response
                try
                {
                    NullableCachedChecks.Add(configurationKey, new Tuple <bool?, DateTime>(isRequired, DateTime.UtcNow));
                }
                catch (ArgumentException exception)
                {
                    if (exception.Message != "The key already existed in the dictionary.")
                    {
                        throw;
                    }
                    // It's been added since we checked... adding locks is slow, so just move on.
                }
            }
            // Don't refresh the expiry, we'll just update the cache every so often which is faster than constantly changing dictionary values.
            else
            {
                isRequired = settings.Item1;
            }

            // If all the above is still not difinitive, react to the bus the originating message was received on, but we only need to check for private.
            // We do this here so caching is atleast used, but this cannot be cached as that would be wrong
            if (isRequired == null && !checkPublic)
            {
                if (GetWasPrivateBusUsed())
                {
                    return(true);
                }
            }

            return(isRequired);
        }