First commit
This commit is contained in:
108
Receiver/MessageReceiver.cs
Normal file
108
Receiver/MessageReceiver.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System.Text.Json;
|
||||
using QuestPDF.Fluent;
|
||||
|
||||
namespace Receiver;
|
||||
|
||||
using Azure.Messaging.ServiceBus;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class MessageReceiver
|
||||
{
|
||||
private readonly string _connectionstring =
|
||||
"";
|
||||
|
||||
private readonly string _queue = "test";
|
||||
|
||||
public async Task Receive()
|
||||
{
|
||||
// the client that owns the connection and can be used to create senders and receivers
|
||||
ServiceBusClient client;
|
||||
|
||||
// the processor that reads and processes messages from the queue
|
||||
ServiceBusProcessor processor;
|
||||
|
||||
// The Service Bus client types are safe to cache and use as a singleton for the lifetime
|
||||
// of the application, which is best practice when messages are being published or read
|
||||
// regularly.
|
||||
//
|
||||
// Set the transport type to AmqpWebSockets so that the ServiceBusClient uses port 443.
|
||||
// If you use the default AmqpTcp, make sure that ports 5671 and 5672 are open.
|
||||
|
||||
// TODO: Replace the <NAMESPACE-CONNECTION-STRING> and <QUEUE-NAME> placeholders
|
||||
var clientOptions = new ServiceBusClientOptions()
|
||||
{
|
||||
TransportType = ServiceBusTransportType.AmqpWebSockets
|
||||
};
|
||||
client = new ServiceBusClient(_connectionstring, clientOptions);
|
||||
|
||||
// create a processor that we can use to process the messages
|
||||
// TODO: Replace the <QUEUE-NAME> placeholder
|
||||
processor = client.CreateProcessor(_queue, new ServiceBusProcessorOptions());
|
||||
|
||||
try
|
||||
{
|
||||
// add handler to process messages
|
||||
processor.ProcessMessageAsync += MessageHandler;
|
||||
|
||||
// add handler to process any errors
|
||||
processor.ProcessErrorAsync += ErrorHandler;
|
||||
|
||||
// start processing
|
||||
await processor.StartProcessingAsync();
|
||||
|
||||
Console.WriteLine("Wait for a minute and then press any key to end the processing");
|
||||
Console.ReadKey();
|
||||
|
||||
// stop processing
|
||||
Console.WriteLine("\nStopping the receiver...");
|
||||
await processor.StopProcessingAsync();
|
||||
Console.WriteLine("Stopped receiving messages");
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Calling DisposeAsync on client types is required to ensure that network
|
||||
// resources and other unmanaged objects are properly cleaned up.
|
||||
await processor.DisposeAsync();
|
||||
await client.DisposeAsync();
|
||||
}
|
||||
|
||||
// handle received messages
|
||||
async Task MessageHandler(ProcessMessageEventArgs args)
|
||||
{
|
||||
string body = args.Message.Body.ToString();
|
||||
|
||||
var bodyDeserialized = JsonSerializer.Deserialize<Product>(body);
|
||||
Console.WriteLine($"Received: {bodyDeserialized.Name}");
|
||||
if (bodyDeserialized.Action == "Create")
|
||||
{
|
||||
Document.Create(container =>
|
||||
{
|
||||
container.Page(page =>
|
||||
{
|
||||
page.Header()
|
||||
.Text("Prodotto")
|
||||
.AlignCenter();
|
||||
page.Content()
|
||||
.Text(
|
||||
$"Product: {bodyDeserialized.Name} Code: {bodyDeserialized.Code} Price: {bodyDeserialized.Price}");
|
||||
});
|
||||
}).GeneratePdf($"{bodyDeserialized.Name}{bodyDeserialized.Code}{bodyDeserialized.Price}.pdf");
|
||||
}
|
||||
|
||||
if (bodyDeserialized.Action == "Delete")
|
||||
{
|
||||
System.IO.File.Delete($"{bodyDeserialized.Name}{bodyDeserialized.Code}{bodyDeserialized.Price}.pdf");
|
||||
}
|
||||
// complete the message. message is deleted from the queue.
|
||||
await args.CompleteMessageAsync(args.Message);
|
||||
}
|
||||
|
||||
// handle any errors when receiving messages
|
||||
Task ErrorHandler(ProcessErrorEventArgs args)
|
||||
{
|
||||
Console.WriteLine(args.Exception.ToString());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user