/
SpecialMapper.cs
59 lines (50 loc) · 1.71 KB
/
SpecialMapper.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
using JasperFx.Core;
using RabbitMQ.Client;
using Wolverine.RabbitMQ.Internal;
namespace Wolverine.RabbitMQ.Tests;
#region sample_rabbit_special_mapper
public class SpecialMapper : IRabbitMqEnvelopeMapper
{
public void MapEnvelopeToOutgoing(Envelope envelope, IBasicProperties outgoing)
{
// All of this is default behavior, but this sample does show
// what's possible here
outgoing.CorrelationId = envelope.CorrelationId;
outgoing.MessageId = envelope.Id.ToString();
outgoing.ContentType = "application/json";
if (envelope.DeliverBy.HasValue)
{
var ttl = Convert.ToInt32(envelope.DeliverBy.Value.Subtract(DateTimeOffset.Now).TotalMilliseconds);
outgoing.Expiration = ttl.ToString();
}
if (envelope.TenantId.IsNotEmpty())
{
outgoing.Headers ??= new Dictionary<string, object>();
outgoing.Headers["tenant-id"] = envelope.TenantId;
}
}
public void MapIncomingToEnvelope(Envelope envelope, IBasicProperties incoming)
{
envelope.CorrelationId = incoming.CorrelationId;
envelope.ContentType = "application/json";
if (Guid.TryParse(incoming.MessageId, out var id))
{
envelope.Id = id;
}
else
{
envelope.Id = Guid.NewGuid();
}
if (incoming.Headers != null && incoming.Headers.TryGetValue("tenant-id", out var tenantId))
{
// Watch this in real life, some systems will send header values as
// byte arrays
envelope.TenantId = (string)tenantId;
}
}
public IEnumerable<string> AllHeaders()
{
yield break;
}
}
#endregion