/
HeadersWriterProducerBehavior.cs
31 lines (26 loc) · 1.14 KB
/
HeadersWriterProducerBehavior.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
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System.Threading.Tasks;
using Silverback.Messaging.Broker.Behaviors;
using Silverback.Messaging.Messages;
using Silverback.Util;
namespace Silverback.Messaging.Headers
{
/// <summary>
/// Maps the properties decorated with the <see cref="HeaderAttribute" /> to the message headers.
/// </summary>
public class HeadersWriterProducerBehavior : IProducerBehavior
{
/// <inheritdoc cref="ISorted.SortIndex" />
public int SortIndex => BrokerBehaviorsSortIndexes.Producer.HeadersWriter;
/// <inheritdoc cref="IProducerBehavior.HandleAsync" />
public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next)
{
Check.NotNull(context, nameof(context));
Check.NotNull(next, nameof(next));
HeaderAttributeHelper.GetHeaders(context.Envelope.Message)
.ForEach(header => context.Envelope.Headers.AddOrReplace(header.Name, header.Value));
await next(context).ConfigureAwait(false);
}
}
}