Azure

Azure Service Bus With Instance

Azure Service Bus

Azure Service Bus is a totally managed messaging service from Microsoft that permits functions to ship messages to one another and obtain messages reliably, with out having to fret concerning the underlying infrastructure. This service is usually used for eventualities the place decoupled communication between functions is required, similar to microservices structure, or for enabling communication between cloud and on-premises methods.

On this article, we’ll discover Azure Service Bus, what it does, and the way it may be utilized in a C# utility.

What’s Azure Service Bus?

Azure Service Bus is a messaging service that allows functions to ship and obtain messages in a dependable, safe, and scalable method. This service is constructed on prime of Azure’s Service Material and gives a messaging infrastructure that may deal with a big quantity of messages and help a number of messaging patterns.

One of many key options of Azure Service Bus is its potential to help a number of messaging patterns, similar to point-to-point messaging, publish/subscribe messaging, and request/reply messaging. This makes it a super resolution for a lot of use instances, similar to enabling communication between microservices, sending and receiving messages between cloud and on-premises methods, and dealing with background jobs or workflows.

Getting Began with Azure Service Bus in C#

To start out utilizing Azure Service Bus in a C# utility, you will have to create a Service Bus namespace and arrange a queue or a subject/subscription. A namespace is a container that holds all of the messaging entities, similar to queues and matters, for a given resolution.

After getting created a namespace, you should use Microsoft.Azure.ServiceBus NuGet package deal to work together with the Service Bus out of your C# utility. This package deal gives a easy and handy API for sending and receiving messages, in addition to managing messaging entities.

Sending a Message to a Queue

Right here is an easy instance of how one can ship a message to a Service Bus queue in C#,

utilizing Microsoft.Azure.ServiceBus;
namespace ServiceBusExample {
    class Program {
        static void Important(string[] args) {
            const string serviceBusConnectionString = "Endpoint=sb://your-namespace.servicebus.home windows.web/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=your-key";
            const string queueName = "your-queue-name";
            var queueClient = new QueueClient(serviceBusConnectionString, queueName);
            var message = new Message(Encoding.UTF8.GetBytes("Hiya, World!"));
            queueClient.SendAsync(message).GetAwaiter().GetResult();
            Console.WriteLine("Message despatched to the queue efficiently.");
            queueClient.Shut();
        }
    }
}

Receiving a Message

utilizing Microsoft.Azure.ServiceBus;
namespace ServiceBusExample {
    class Program {
        static void Important(string[] args) {
            const string serviceBusConnectionString = "Endpoint=sb://your-namespace.servicebus.home windows.web/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=your-key";
            const string queueName = "your-queue-name";
            var queueClient = new QueueClient(serviceBusConnectionString, queueName);
            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) {
                MaxConcurrentCalls = 1,
                    AutoComplete = false
            };
            queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
            Console.WriteLine("Listening for messages...");
            Console.ReadLine();
            queueClient.Shut();
        }
        static Activity ProcessMessagesAsync(Message message, CancellationToken cancellationToken) {
            Console.WriteLine($ "Obtained message: SequenceNumber:{message.SystemProperties.SequenceNumber} Physique:{Encoding.UTF8.GetString(message.Physique)}");
            return Activity.CompletedTask;
        }
        static Activity ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs) {
            Console.WriteLine($ "Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
            return Activity.CompletedTask;
        }
    }
}

On this instance, we first outline the connection string and the title of the queue we need to ship and obtain messages from. To obtain messages, we create an occasion of the QueueClient class and use its RegisterMessageHandler technique to register a message handler delegate that may course of the obtained messages. The ProcessMessagesAsync technique is known as for every message obtained and prints the message’s sequence quantity and physique. The ExceptionReceivedHandler technique is known as if any exceptions are encountered throughout message processing.

Lastly, we name queueClient.Shut to shut the connection to the Service Bus after we’re executed.

Thanks for studying my article.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button