// Do NOT wait for the messages to be dispatched (fire and forget)
        public static void Dispatch(this IMessageDispatcher messageDispatcher, DispatchDataDictionary dispatchData, bool publish, bool retryOnFailure = true, ILogger logger = null)
        {
            if (messageDispatcher == null)
            {
                throw new ArgumentNullException(nameof(messageDispatcher));
            }

            if (dispatchData == null)
            {
                throw new ArgumentNullException(nameof(dispatchData));
            }

            DispatchInternal(messageDispatcher, dispatchData, publish, retryOnFailure, logger);
        }
Exemplo n.º 2
0
        public virtual ValueTask <IDispatchResult> ProcessAsync <TMessage>(DispatchDataDictionary <TMessage> dispatchData,
                                                                           Func <DispatchDataDictionary <TMessage>, ValueTask <IDispatchResult> > next,
                                                                           CancellationToken cancellation)
            where TMessage : class
        {
            if (dispatchData == null)
            {
                throw new ArgumentNullException(nameof(dispatchData));
            }

            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            return(next(dispatchData));
        }
        private static async void DispatchInternal(
            IMessageDispatcher messageDispatcher,
            DispatchDataDictionary dispatchData,
            bool publish,
            bool retryOnFailure,
            ILogger logger)
        {
            // Assuming the argument are already checked.

            try
            {
                IDispatchResult dispatchResult;

                do
                {
                    dispatchResult = await messageDispatcher.DispatchAsync(dispatchData, publish, cancellation : default);
                }while (!dispatchResult.IsSuccess && retryOnFailure);
            }
            catch (Exception exc)
            {
                ExceptionHelper.LogException(exc, logger);
            }
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (!CanConvert(objectType))
            {
                throw new InvalidOperationException();
            }

            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new InvalidOperationException();
            }

            var    messageType = objectType.IsGenericTypeDefinition ? objectType.GetGenericArguments().First() : null;
            object message     = null;

            ImmutableDictionary <string, object> .Builder data = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                else if (reader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)reader.Value == "message-type")
                    {
                        reader.Read();
                        var deserializedMessageType = TypeLoadHelper.LoadTypeFromUnqualifiedName(reader.Value as string);

                        if (messageType != null && messageType != deserializedMessageType)
                        {
                            throw new InvalidOperationException();
                        }

                        messageType = deserializedMessageType;
                    }
                    else if ((string)reader.Value == "message")
                    {
                        reader.Read();
                        message = serializer.Deserialize(reader, typeof(object));
                    }
                    else if ((string)reader.Value == "data")
                    {
                        data = ReadData(reader, serializer);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            if (messageType == null || message == null)
            {
                throw new InvalidOperationException();
            }

            return(DispatchDataDictionary.Create(messageType, message, data?.ToImmutable() ?? ImmutableDictionary <string, object> .Empty));
        }