예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CloudEvent"/> class and sets its data,
        /// attributes, and headers according to the payload and headers of the <paramref name=
        /// "receiverMessage"/>.
        /// </summary>
        /// <param name="receiverMessage">
        /// The <see cref="IReceiverMessage"/> with headers that map to cloud event attributes.
        /// </param>
        /// <param name="protocolBinding">
        /// The <see cref="IProtocolBinding"/> used to map <see cref="IReceiverMessage"/> headers to
        /// CloudEvent attributes. If <see langword="null"/>, then <see cref="DefaultProtocolBinding"/>
        /// is used instead.
        /// </param>
        public CloudEvent(IReceiverMessage receiverMessage, IProtocolBinding protocolBinding = null)
        {
            if (receiverMessage is null)
            {
                throw new ArgumentNullException(nameof(receiverMessage));
            }

            FromReceiverMessage(receiverMessage, protocolBinding);
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CorrelatedEvent"/> class and sets its
 /// data, attributes, and headers according to the payload and headers of the <paramref
 /// name="receiverMessage"/>.
 /// </summary>
 /// <param name="receiverMessage">
 /// The <see cref="IReceiverMessage"/> with headers that map to cloud event attributes.
 /// </param>
 /// <param name="protocolBinding">
 /// The <see cref="IProtocolBinding"/> used to map <see cref="IReceiverMessage"/> headers to
 /// CloudEvent attributes. If <see langword="null"/>, then <see cref="CloudEvent.DefaultProtocolBinding"/>
 /// is used instead.
 /// </param>
 public CorrelatedEvent(IReceiverMessage receiverMessage, IProtocolBinding protocolBinding = null)
     : base(receiverMessage, protocolBinding)
 {
 }
 public void Invoke(SenderMessage senderMessage, IProtocolBinding protocolBinding) =>
 _invokeValidateMethod(senderMessage, protocolBinding);
예제 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SequentialEvent"/> class and sets its
 /// data, attributes, and headers according to the payload and headers of the <paramref
 /// name="receiverMessage"/>.
 /// </summary>
 /// <param name="receiverMessage">
 /// The <see cref="IReceiverMessage"/> with headers that map to cloud event attributes.
 /// </param>
 /// <param name="protocolBinding">
 /// The <see cref="IProtocolBinding"/> used to map <see cref="IReceiverMessage"/> headers to
 /// CloudEvent attributes. If <see langword="null"/>, then <see cref="CloudEvent.DefaultProtocolBinding"/>
 /// is used instead.
 /// </param>
 public SequentialEvent(IReceiverMessage receiverMessage, IProtocolBinding protocolBinding = null)
     : base(receiverMessage, protocolBinding)
 {
 }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PartitionedEvent"/> class and sets its
 /// data, attributes, and headers according to the payload and headers of the <paramref
 /// name="receiverMessage"/>.
 /// </summary>
 /// <param name="receiverMessage">
 /// The <see cref="IReceiverMessage"/> with headers that map to cloud event attributes.
 /// </param>
 /// <param name="protocolBinding">
 /// The <see cref="IProtocolBinding"/> used to map <see cref="IReceiverMessage"/> headers
 /// to CloudEvent attributes. If <see langword="null"/>, then <see cref=
 /// "CloudEvent.DefaultProtocolBinding"/> is used instead.
 /// </param>
 public PartitionedEvent(IReceiverMessage receiverMessage, IProtocolBinding protocolBinding = null)
     : base(receiverMessage, protocolBinding)
 {
 }
예제 #6
0
 public static new void Validate(SenderMessage senderMessage, IProtocolBinding protocolBinding = null) =>
 CloudEvent.Validate(senderMessage, protocolBinding);
예제 #7
0
        /// <summary>
        /// Adds a <see cref="ValidatingSender"/> decorator that ensures messages are valid
        /// CloudEvents.
        /// <para>
        /// The <typeparamref name="TCloudEvent"/> type <em>must</em> define a public static method
        /// named "Validate" with the exact parameters <c>(<see cref="SenderMessage"/>, <see cref=
        /// "IProtocolBinding"/>)</c>. A <see cref="MissingMemberException"/> is immediately thrown if
        /// the class does not define such a method.
        ///  </para>
        /// </summary>
        /// <typeparam name="TCloudEvent">The type of CloudEvent used to apply validation.</typeparam>
        /// <param name="builder">The <see cref="ISenderBuilder"/>.</param>
        /// <param name="protocolBinding">
        /// The <see cref="IProtocolBinding"/> used to map CloudEvent attributes to <see cref="SenderMessage"/>
        /// headers.
        /// </param>
        /// <returns>The same <see cref="ISenderBuilder"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="builder"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="MissingMemberException">
        /// If the <typeparamref name="TCloudEvent"/> class does not define a public static method
        /// named "Validate" with the exact parameters <c>(<see cref="SenderMessage"/>, <see cref=
        /// "IProtocolBinding"/>)</c>.
        /// </exception>
        public static ISenderBuilder AddValidation <TCloudEvent>(this ISenderBuilder builder, IProtocolBinding protocolBinding = null)
            where TCloudEvent : CloudEvent
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var validateMethod = _validateMethods.GetOrAdd(typeof(TCloudEvent), ValidateMethod.Create)
                                 ?? throw MissingValidateMethod(typeof(TCloudEvent));

            return(builder.AddValidation(message => validateMethod.Invoke(message, protocolBinding)));
        }
예제 #8
0
        /// <summary>
        /// Start listening for CloudEvents and handle them using the specified callback function.
        /// <para>
        /// The <typeparamref name="TCloudEvent"/> type <em>must</em> define a constructor with the
        /// exact parameters <c>(<see cref="IReceiverMessage"/>, <see cref="IProtocolBinding"/>)
        /// </c>. A <see cref="MissingMemberException"/> is immediately thrown if the class does
        /// not define such a constructor.
        /// </para>
        /// </summary>
        /// <param name="receiver">The receiver to start.</param>
        /// <param name="onEventReceivedAsync">
        /// A function that is invoked when a CloudEvent is received.
        /// </param>
        /// <param name="protocolBinding">
        /// The <see cref="IProtocolBinding"/> used to map <see cref="IReceiverMessage"/> headers to
        /// CloudEvent attributes.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="receiver"/> or <paramref name="onEventReceivedAsync"/> is <see
        /// langword="null"/>.
        /// </exception>
        /// <exception cref="MissingMemberException">
        /// If the <typeparamref name="TCloudEvent"/> class does not define a public constructor
        /// with the exact parameters <c>(<see cref="IReceiverMessage"/>, <see cref=
        /// "IProtocolBinding"/>)</c>.
        /// </exception>
        public static void Start <TCloudEvent>(this IReceiver receiver,
                                               Func <TCloudEvent, IReceiverMessage, Task> onEventReceivedAsync, IProtocolBinding protocolBinding = null)
            where TCloudEvent : CloudEvent
        {
            if (receiver is null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }
            if (onEventReceivedAsync is null)
            {
                throw new ArgumentNullException(nameof(onEventReceivedAsync));
            }

            if (!MessageConstructor.Exists(typeof(TCloudEvent)))
            {
                throw MissingReceiverConstructor(typeof(TCloudEvent));
            }

            receiver.Start(message => onEventReceivedAsync(message.To <TCloudEvent>(protocolBinding), message));
        }
예제 #9
0
        /// <summary>
        /// Creates an instance of <typeparamref name="TCloudEvent"/> with properties mapped from
        /// the headers of <paramref name="receiverMessage"/>.
        /// <para>
        /// The <typeparamref name="TCloudEvent"/> type <em>must</em> define a public constructor
        /// with the exact parameters <c>(<see cref="IReceiverMessage"/>, <see cref=
        /// "IProtocolBinding"/>)</c>. A <see cref="MissingMemberException"/> is immediately
        /// thrown if the class does not define such a constructor.
        /// </para>
        /// </summary>
        /// <typeparam name="TCloudEvent">The type of <see cref="CloudEvent"/> to create.</typeparam>
        /// <param name="receiverMessage">
        /// The <see cref="IReceiverMessage"/> to be mapped to the new <typeparamref name="TCloudEvent"/>.
        /// </param>
        /// <param name="protocolBinding">
        /// The <see cref="IProtocolBinding"/> used to map <see cref="IReceiverMessage"/> headers to
        /// CloudEvent attributes.
        /// </param>
        /// <returns>
        /// A new <typeparamref name="TCloudEvent"/> with properties mapped from the headers of the <see cref="IReceiverMessage"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="receiverMessage"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="MissingMemberException">
        /// If the <typeparamref name="TCloudEvent"/> class does not define a public constructor
        /// with the exact parameters <c>(<see cref="IReceiverMessage"/>, <see cref=
        /// "IProtocolBinding"/>)</c>.
        /// </exception>
        public static TCloudEvent To <TCloudEvent>(this IReceiverMessage receiverMessage, IProtocolBinding protocolBinding = null)
            where TCloudEvent : CloudEvent
        {
            if (receiverMessage is null)
            {
                throw new ArgumentNullException(nameof(receiverMessage));
            }

            var messageConstructor = _messageConstructors.GetOrAdd(typeof(TCloudEvent), MessageConstructor.Create)
                                     ?? throw MissingReceiverConstructor(typeof(TCloudEvent));

            return((TCloudEvent)messageConstructor.Invoke(receiverMessage, protocolBinding));
        }
 public object Invoke(IReceiverMessage receiverMessage, IProtocolBinding protocolBinding) =>
 _invokeConstructor(receiverMessage, protocolBinding);