Repository navigation
Expand file tree
/
Copy pathPriceQueueTriggerFA.cs
More file actions
93 lines (80 loc) · 3.32 KB
/
Copy pathPriceQueueTriggerFA.cs
File metadata and controls
93 lines (80 loc) · 3.32 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus;
using PriceMS.DTO;
using PriceMS.Transformer;
using System.Xml.Linq;
using System.Xml;
using System.Text;
namespace PriceMS.Function;
public class PriceQueueTriggerFA
{
private readonly ILogger<PriceQueueTriggerFA> _logger;
private readonly IPriceTransFormerService _priceTransFormerService;
private readonly ServiceBusReceiver _receiver;
private readonly ServiceBusSender _sender;
public PriceQueueTriggerFA(ILogger<PriceQueueTriggerFA> logger, IPriceTransFormerService priceTransFormerService, ServiceBusClient client)
{
_logger = logger;
_priceTransFormerService = priceTransFormerService;
_receiver = client.CreateReceiver("sbq-pl-a-001-01");
_sender = client.CreateSender("sbq-pl-a-001-02");
}
[Function("PriceQueueTriggerFA")]
public async Task ConsumePriceMessage(
[ServiceBusTrigger("sbq-pl-a-001-01", Connection ="ServiceBusConnectionString")]
ServiceBusReceivedMessage message)
{
_logger.LogInformation("Received Message from Queue: {queueName}", "sbq-pl-a-001-01");
if (message != null)
{
string messagePayload = message.Body.ToString();
_logger.LogInformation("Message ID: {MessageId}", message.MessageId);
_logger.LogInformation("Delivery Count: {DeliveryCount}", message.DeliveryCount);
//_logger.LogInformation("Message: {message}", messagePayload);
}
else
{
_logger.LogError("Message Body is null");
}
List<InputDTO> priceDataList = JsonConvert.DeserializeObject<List<InputDTO>>(message!.Body.ToString()) ?? throw new NullReferenceException("Message Body can't be null.");
//transformation
List<XDocument> transformedPayload = _priceTransFormerService.TransformPricePayload(priceDataList);
//send the transformed msg to service bus
try
{
foreach (XDocument xmlPayload in transformedPayload)
{
//_logger.LogInformation(xmlPayload.ToString());
using var ms = new MemoryStream();
using (var writer = XmlWriter.Create(ms, new XmlWriterSettings
{
Encoding = new UTF8Encoding(false),
Indent = true
}))
{
xmlPayload.Save(writer);
}
ServiceBusMessage serviceBusMessage = new ServiceBusMessage(BinaryData.FromBytes(ms.ToArray()))
{
CorrelationId = Guid.NewGuid().ToString(),
ContentType = "application/xml"
};
serviceBusMessage.ApplicationProperties["recordType"] = "price";
serviceBusMessage.ApplicationProperties["source"] = "PriceQueueTriggerFA";
await _sender.SendMessageAsync(serviceBusMessage);
}
}
catch (Exception exception)
{
_logger.LogError(exception.Message);
}
await Task.CompletedTask;
}
}