/
ConsumerEndpointBuilderConsumeBinaryFilesExtensions.cs
60 lines (56 loc) · 2.48 KB
/
ConsumerEndpointBuilderConsumeBinaryFilesExtensions.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
57
58
59
60
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System;
using Silverback.Messaging.BinaryFiles;
using Silverback.Messaging.Messages;
using Silverback.Messaging.Serialization;
using Silverback.Util;
namespace Silverback.Messaging.Configuration
{
/// <summary>
/// Adds the <c>ConsumeBinaryFiles</c> method to the
/// <see cref="ConsumerEndpointBuilder{TEndpoint,TBuilder}" />.
/// </summary>
public static class ConsumerEndpointBuilderConsumeBinaryFilesExtensions
{
/// <summary>
/// <para>
/// Sets the serializer to an instance of <see cref="BinaryFileMessageSerializer" /> (or
/// <see cref="BinaryFileMessageSerializer{TModel}" />) to wrap the consumed binary files into a
/// <see cref="BinaryFileMessage" />.
/// </para>
/// <para>
/// This settings will force the <see cref="BinaryFileMessageSerializer" /> to be used regardless of
/// the message type header.
/// </para>
/// </summary>
/// <remarks>
/// This replaces the <see cref="IMessageSerializer" /> and the endpoint will only be able to deal with
/// binary files.
/// </remarks>
/// <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="IBinaryFileMessageSerializerBuilder" />
/// and configures it.
/// </param>
/// <returns>
/// The endpoint builder so that additional calls can be chained.
/// </returns>
public static TBuilder ConsumeBinaryFiles<TBuilder>(
this IConsumerEndpointBuilder<TBuilder> endpointBuilder,
Action<IBinaryFileMessageSerializerBuilder>? serializerBuilderAction = null)
where TBuilder : IConsumerEndpointBuilder<TBuilder>
{
Check.NotNull(endpointBuilder, nameof(endpointBuilder));
var serializerBuilder = new BinaryFileMessageSerializerBuilder();
serializerBuilderAction?.Invoke(serializerBuilder);
endpointBuilder.DeserializeUsing(serializerBuilder.Build());
return (TBuilder)endpointBuilder;
}
}
}