/
ProducerEndpointBuilderSerializeAsJsonUsingNewtonsoftExtensions.cs
56 lines (52 loc) · 2.55 KB
/
ProducerEndpointBuilderSerializeAsJsonUsingNewtonsoftExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System;
using Silverback.Messaging.Serialization;
using Silverback.Util;
namespace Silverback.Messaging.Configuration
{
/// <summary>
/// Adds the <c>SerializeAsJsonUsingNewtonsoft</c> method to the <see cref="ProducerEndpoint" />.
/// </summary>
public static class ProducerEndpointBuilderSerializeAsJsonUsingNewtonsoftExtensions
{
/// <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;
}
}
}