« Azure Service Bus » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 46 : Ligne 46 :
{
{
     var client = new ServiceBusClient(ConnectionString);
     var client = new ServiceBusClient(ConnectionString);
     try // use await using in C# 8 / .Net Core
    var processor = client.CreateProcessor(QueueName);
 
     try
     {
     {
         var receiver = client.CreateReceiver(QueueName);
         processor.ProcessMessageAsync += MessageHandler;
        processor.ProcessErrorAsync += ErrorHandler;
 
        await processor.StartProcessingAsync();


        var message = await receiver.ReceiveMessageAsync();
         Console.ReadKey();
         Console.WriteLine($"Received {message.Body}");
     }
     }
     finally
     finally
     {
     {
        await processor.DisposeAsync();
         await client.DisposeAsync();
         await client.DisposeAsync();
     }
     }
}
static async Task MessageHandler(ProcessMessageEventArgs args)
{
    string body = args.Message.Body.ToString();
    Console.WriteLine(body);
    // we can evaluate application logic and use that to determine how to settle the message.
    await args.CompleteMessageAsync(args.Message);
}
static Task ErrorHandler(ProcessErrorEventArgs args)
{
    // the error source tells me at what point in the processing an error occurred
    Console.WriteLine(args.ErrorSource);
    // the fully qualified namespace is available
    Console.WriteLine(args.FullyQualifiedNamespace);
    // as well as the entity path
    Console.WriteLine(args.EntityPath);
    Console.WriteLine(args.Exception.ToString());
    return Task.CompletedTask;
}
}
</filebox>
</filebox>

Version du 15 décembre 2021 à 17:49

Description

It is a message broker with message queues and publish-subscribe topics.

Configure the service bus on Azure

  • Azure portal → Service Bus → Add
  • Create namespace: the namespace will be the url use by the service bus
  • Once deployed, go to the newly create service bus namespace → add queue

Sender

Add Nuget package Azure.Messaging.ServiceBus

Program.cs
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String";
private const string QueueName = "myqueue";
private const string TopicName = "topic1";

static async Task Main(string[] args)
{
    var client = new ServiceBusClient(ConnectionString);
    try  // use await using in C# 8 / .Net Core
    {
        var sender = client.CreateSender(QueueName);
        var sender = client.CreateSender(TopicName);

        var message = new ServiceBusMessage("Hello!");
        await sender.SendMessageAsync(message);
        Console.WriteLine($"Sent {message.Body}");
    }
    finally
    {
        await client.DisposeAsync();
    }
}

Receiver

Add Nuget package Azure.Messaging.ServiceBus

Program.cs
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String";
private const string QueueName = "myqueue";
private const string TopicName = "topic1";

static async Task Main(string[] args)
{
    var client = new ServiceBusClient(ConnectionString);
    var processor = client.CreateProcessor(QueueName);

    try
    {
        processor.ProcessMessageAsync += MessageHandler;
        processor.ProcessErrorAsync += ErrorHandler;

        await processor.StartProcessingAsync();

        Console.ReadKey();
    }
    finally
    {
        await processor.DisposeAsync();
        await client.DisposeAsync();
    }
}

static async Task MessageHandler(ProcessMessageEventArgs args)
{
    string body = args.Message.Body.ToString();
    Console.WriteLine(body);

    // we can evaluate application logic and use that to determine how to settle the message.
    await args.CompleteMessageAsync(args.Message);
}

static Task ErrorHandler(ProcessErrorEventArgs args)
{
    // the error source tells me at what point in the processing an error occurred
    Console.WriteLine(args.ErrorSource);

    // the fully qualified namespace is available
    Console.WriteLine(args.FullyQualifiedNamespace);

    // as well as the entity path
    Console.WriteLine(args.EntityPath);
    Console.WriteLine(args.Exception.ToString());
    return Task.CompletedTask;
}