コード例 #1
0
        /// <summary>
        ///     <para>
        ///         Sets the serializer to an instance of <see cref="NewtonsoftJsonMessageSerializer" /> (or
        ///         <see cref="NewtonsoftJsonMessageSerializer{TMessage}" />) to serialize the produced messages as
        ///         JSON.
        ///     </para>
        ///     <para>
        ///         By default this serializer forwards the message type in an header to let the consumer know which
        ///         type has to be deserialized. This approach allows to mix messages of different types in the same
        ///         endpoint and it's ideal when both the producer and the consumer are using Silverback but might not
        ///         be optimal for interoperability. This behavior can be changed using the builder action and
        ///         specifying a fixed message type.
        ///     </para>
        /// </summary>
        /// <typeparam name="TBuilder">
        ///     The actual builder type.
        /// </typeparam>
        /// <param name="endpointBuilder">
        ///     The endpoint builder.
        /// </param>
        /// <param name="serializerBuilderAction">
        ///     An optional <see cref="Action{T}" /> that takes the
        ///     <see cref="INewtonsoftJsonMessageSerializerBuilder" /> and configures it.
        /// </param>
        /// <returns>
        ///     The endpoint builder so that additional calls can be chained.
        /// </returns>
        public static TBuilder SerializeAsJsonUsingNewtonsoft <TBuilder>(
            this IProducerEndpointBuilder <TBuilder> endpointBuilder,
            Action <INewtonsoftJsonMessageSerializerBuilder>?serializerBuilderAction = null)
            where TBuilder : IProducerEndpointBuilder <TBuilder>
        {
            Check.NotNull(endpointBuilder, nameof(endpointBuilder));

            var serializerBuilder = new NewtonsoftJsonMessageSerializerBuilder();

            serializerBuilderAction?.Invoke(serializerBuilder);
            endpointBuilder.SerializeUsing(serializerBuilder.Build());

            return((TBuilder)endpointBuilder);
        }
        /// <summary>
        ///     <para>
        ///         Sets the serializer to an instance of <see cref="NewtonsoftJsonMessageSerializer" /> (or
        ///         <see cref="NewtonsoftJsonMessageSerializer{TMessage}" />) to deserialize the consumed JSON.
        ///     </para>
        ///     <para>
        ///         By default this serializer relies on the message type header to determine the type of the message
        ///         to be deserialized. This behavior can be changed using the builder action and specifying a fixed
        ///         message type.
        ///     </para>
        /// </summary>
        /// <typeparam name="TBuilder">
        ///     The actual builder type.
        /// </typeparam>
        /// <param name="endpointBuilder">
        ///     The endpoint builder.
        /// </param>
        /// <param name="serializerBuilderAction">
        ///     An optional <see cref="Action{T}" /> that takes the
        ///     <see cref="INewtonsoftJsonMessageSerializerBuilder" /> and configures it.
        /// </param>
        /// <returns>
        ///     The endpoint builder so that additional calls can be chained.
        /// </returns>
        public static TBuilder DeserializeJsonUsingNewtonsoft <TBuilder>(
            this IConsumerEndpointBuilder <TBuilder> endpointBuilder,
            Action <INewtonsoftJsonMessageSerializerBuilder>?serializerBuilderAction = null)
            where TBuilder : IConsumerEndpointBuilder <TBuilder>
        {
            Check.NotNull(endpointBuilder, nameof(endpointBuilder));

            var serializerBuilder = new NewtonsoftJsonMessageSerializerBuilder();

            if (endpointBuilder.MessageType != null)
            {
                serializerBuilder.UseFixedType(endpointBuilder.MessageType);
            }

            serializerBuilderAction?.Invoke(serializerBuilder);
            endpointBuilder.DeserializeUsing(serializerBuilder.Build());

            return((TBuilder)endpointBuilder);
        }