Azure

Street To AZ-204 – Growing Message-Based mostly Resolution

Intro

 

This text’s intention is to elucidate the principle expertise measured on this sub-topic of the AZ-204 Certification. Azure Service Bus and Azure Queue Storage are the principle elements that can have their fundamentals defined right here alongside a sensible instance.

 

This certification may be very in depth and this text approaches solely the principle subjects, ensure you know these elements deep down earlier than taking the examination. One other nice tip is doing examination simulators earlier than the official examination in an effort to validate your data.

 

What’s the Certification AZ-204 – Growing Options for Microsoft Azure?

 

The AZ-204 – Growing Options for Microsoft Azure certification measures designing, constructing, testing, and sustaining expertise of an utility and/or service within the Microsoft Azure Cloud atmosphere. It approaches, amongst others, these elements,

  • Azure Digital Machines;
  • Docker;
  • Azure Containers;
  • Service Internet App;
  • Azure Features;
  • Cosmos DB;
  • Azure Storage;
  • Azure AD;
  • Azure Key Vault;
  • Azure Managed Identities;
  • Azure Redis Cache;
  • Azure Logic App;
  • Azure Occasion Grid;
  • Azure Occasion Hub;
  • Azure Notification Hub;
  • Azure Service Bus;
  • Azure Queue Storage.

Goal Viewers

 

Any IT skilled prepared to enhance his data in Microsoft Azure is inspired to take this certification, it’s an effective way to measure your expertise inside trending applied sciences. However, some group of execs is keener to take most benefit of it,

  • Azure Builders, with not less than 1 yr of expertise with Microsoft Azure;
  • Skilled Software program Builders, on the lookout for an Architect place in a hybrid atmosphere;
  • Software program Builders, working to maneuver functions to the cloud atmosphere.

Expertise Measured

 

In accordance with in the present day’s date, the talents which are measured within the examination are break up as follows,

  • Develop Azure compute options (25-30%)
    • Implement Azure features

  • Develop for Azure storage (10-15%)
  • Implement Azure safety (15-20%)
  • Monitor, troubleshoot, and optimize Azure options (10-15%)
  • Hook up with and eat Azure providers and third-party providers (25- 30%)

Advantages of Getting Licensed

 

The principle profit right here is having a worldwide acknowledged certification that proves that you’ve got data of this subject. Amongst intrinsic and extrinsic advantages, we have now,

  • Larger development potential, as certifications are a giant plus;
  • Reductions and offers in Microsoft merchandise and companions, like PluralSight and UpWork;
  • MCP Newsletters, with trending applied sciences;
  • Larger publicity on LinkedIn, as recruiters often seek for particular certifications;
  • Larger wage, you may be extra worthwhile to your organization;
  • Distinctive happiness when getting the end result and also you have been permitted, figuring out that every one your efforts have been price it;

Fundamental Expertise Measured by This Matter

 

What’s Azure Service Bus?

 

Service Bus is a totally managed enterprise message dealer that helps message queuing and publish/subscribe to subjects. Use Azure Service Bus to scale your functions with the ability of asynchronous messages and built-in integration with Azure Providers.

 

With Service Bus you may deal with single or batch messaging, load stability messages consumption, subjects subscription, message periods, and transactions dealing with with a assure that it’s in compliance with commonplace and protocols like Superior Message Queuing Protocol – AMQP 1.0, Java Message Service – JMS 2.Zero for Premium SKU, and JMS 1.1 for Standart SKU. 

 

Service Bus important terminologies are those as follows,

  • The namespace works like a server and comprises N queues and subjects;
  • Queue, comprises the messages;
  • Sender, who sends the message;
  • Receiver, who receives the message;
  • Matter, works as a queue however with a number of receivers;
  • Subscription, a receiver in a subject;
  • Batch, group of messages;
  • Protected-batch, validates if every message might be included within the batch;
  • Session, permits FIFO and group your messages within the queue;
  • Peek, returns a message with out eradicating it from the queue;
  • Lifeless-letter queue, queue for messages that would not be delivered by its regular queue;
  • Peek & Lock, retrieves a message from the queue with out eradicating it, and locks the message from being obtained by others receivers;
  • Obtain & Delete, retrieves and delete a message from the queue;
  • Auto delete on idle, units a time span to delete the queue if it isn’t used;
  • Duplicate detection historical past, earlier than sending a message it checks if the message was not despatched earlier than;

What are Azure Queue Storage queues?

 

Azure Queue Storage queues is an easy message dealer, it gives cloud message trade between functions and providers with messages as much as 64kb in measurement in authenticated HTPP/HTPPS protocols. They’re inside an Azure Storage Account and also you entry it with the identical entry keys and connection strings that you simply use your different sources contained in the Storage Account.

 

Azure Queue Storage gives three methods of dealing with messages on the queue. The primary solution to deal with a message on the queue is to peek a message whenever you retrieve a message from the queue with out eradicating or locking it, the second approach is to delete a message after this message is processed and the third approach is to obtain a message within the entrance of the queue however locking it quickly.

 

Service Bus vs Azure Queue Storage Queues

 

Superficially evaluating Service Bus with an Azure Queue Storage queues lets say that Service Bus is a really highly effective and full message-broker that’s really helpful for medium/excessive complexity whereas Azure Queue Storage queues is a fundamental message-broker that might be used for low complexity. Under we are able to see a comparability desk with the principle message-broker functionalities and the way Service Bus and Azure Queues Storage queues match with these functionalities aspect by aspect.

 

 

Sensible Examples

 

Azure Service Bus with .Web Core

 

Pre-Requisites

  • Nuget Package deal Azure.Messaging.ServiceBus; 

 

Creating Service Bus with Azure CLI

 

Setting Variables

  1. $resourceGroup = “serviceBus-RG”  
  2. $location =“westeurope”  
  3. $serviceBusNamespace=“sampleservbusnamespace”  

Creating the Service Bus Namespace

  1. az servicebus namespace create -g $resourceGroup -n $serviceBusNamespace -l $location –sku Fundamental  

End result

 

 

 

Creating Service Bus Objects with .Web Core

 

Strategies to get, create, replace and delete a queue, a subject, and a subscription

  1. class ServiceBusObjects  
  2.    {  
  3.        public static async Activity<QueueProperties> CreateQueueAsync(string connectionString, string queueName, bool requiresSession = false)  
  4.        {  
  5.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  6.            var choices = new CreateQueueOptions(queueName)  
  7.            {  
  8.                DefaultMessageTimeToLive = TimeSpan.FromDays(2),  
  9.                DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1),  
  10.                EnableBatchedOperations = true,  
  11.                DeadLetteringOnMessageExpiration = true,  
  12.                EnablePartitioning = false,  
  13.                ForwardDeadLetteredMessagesTo = null,  
  14.                ForwardTo = null,  
  15.                LockDuration = TimeSpan.FromSeconds(45),  
  16.                MaxDeliveryCount = 8,  
  17.                MaxSizeInMegabytes = 2048,  
  18.                UserMetadata = “some metadata”  
  19.            };  
  20.            choices.RequiresSession = requiresSession;  
  21.   
  22.            choices.AuthorizationRules.Add(new SharedAccessAuthorizationRule(  
  23.                “allClaims”,  
  24.                new[] { AccessRights.Handle, AccessRights.Ship, AccessRights.Hear }));  
  25.   
  26.            return await consumer.CreateQueueAsync(choices);  
  27.        }  
  28.   
  29.        public static async Activity<QueueProperties> GetQueueAsync(string connectionString, string queueName)  
  30.        {  
  31.            attempt  
  32.            {  
  33.                var consumer = new ServiceBusAdministrationClient(connectionString);  
  34.                return await consumer.GetQueueAsync(queueName);  
  35.            }  
  36.            catch (Exception ex)  
  37.            {  
  38.                return null;  
  39.            }  
  40.        }  
  41.        public static async Activity<QueueProperties> UpdateQueueAsync(string connectionString, QueueProperties queue)  
  42.        {  
  43.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  44.            queue.UserMetadata = “different metadata”;  
  45.            return await consumer.UpdateQueueAsync(queue);  
  46.        }  
  47.        public static async Activity<Azure.Response> DeleteQueueAsync(string connectionString, string queueName)  
  48.        {  
  49.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  50.            return await consumer.DeleteQueueAsync(queueName);  
  51.        }  
  52.        public static async Activity<TopicProperties> CreateTopicAsync(string topicName, string connectionString)  
  53.        {  
  54.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  55.            var topicOptions = new CreateTopicOptions(topicName)  
  56.            {  
  57.                DefaultMessageTimeToLive = TimeSpan.FromDays(2),  
  58.                DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1),  
  59.                EnableBatchedOperations = true,  
  60.                EnablePartitioning = false,  
  61.                MaxSizeInMegabytes = 2048,  
  62.                UserMetadata = “some metadata”  
  63.            };  
  64.   
  65.            topicOptions.AuthorizationRules.Add(new SharedAccessAuthorizationRule(  
  66.                “allClaims”,  
  67.                new[] { AccessRights.Handle, AccessRights.Ship, AccessRights.Hear }));  
  68.   
  69.            return await consumer.CreateTopicAsync(topicOptions);  
  70.        }  
  71.   
  72.        public static async Activity<TopicProperties> GetTopicAsync(string topicName, string connectionString)  
  73.        {  
  74.            attempt  
  75.            {  
  76.                var consumer = new ServiceBusAdministrationClient(connectionString);  
  77.                return await consumer.GetTopicAsync(topicName);  
  78.            }  
  79.            catch (Exception ex)  
  80.            {  
  81.                return null;  
  82.            }            
  83.        }  
  84.   
  85.        public static async Activity<TopicProperties> UpdateTopicAsync(TopicProperties subject, string connectionString)  
  86.        {  
  87.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  88.            subject.UserMetadata = “different metadata”;  
  89.            return await consumer.UpdateTopicAsync(subject);  
  90.        }  
  91.        public static async Activity<Azure.Response> DeleteTopicAsync(string topicName, string connectionString)  
  92.        {  
  93.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  94.            return await consumer.DeleteTopicAsync(topicName);  
  95.        }  
  96.   
  97.        public static async Activity<SubscriptionProperties> CreateSubscriptionAsync(string topicName, string connectionString, string subscriptionName)  
  98.        {  
  99.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  100.   
  101.            var subscriptionOptions = new CreateSubscriptionOptions(topicName, subscriptionName)  
  102.            {  
  103.                DefaultMessageTimeToLive = TimeSpan.FromDays(2),  
  104.                EnableBatchedOperations = true,  
  105.                UserMetadata = “some metadata”  
  106.            };  
  107.            return await consumer.CreateSubscriptionAsync(subscriptionOptions);  
  108.        }  
  109.   
  110.        public static async Activity<SubscriptionProperties> GetSubscriptionAsync(string topicName, string connectionString, string subscriptionName)  
  111.        {  
  112.            attempt  
  113.            {  
  114.                var consumer = new ServiceBusAdministrationClient(connectionString);  
  115.                return await consumer.GetSubscriptionAsync(topicName, subscriptionName);  
  116.            }  
  117.            catch (Exception ex)  
  118.            {  
  119.                return null;  
  120.            }  
  121.        }  
  122.        public static async Activity<SubscriptionProperties> UpdateSubscriptionAsync(string connectionString, SubscriptionProperties subscription)  
  123.        {  
  124.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  125.            subscription.UserMetadata = “different metadata”;  
  126.            return await consumer.UpdateSubscriptionAsync(subscription);  
  127.        }  
  128.        public static async Activity<Azure.Response> DeleteSubscriptionAsync(string topicName, string connectionString, string subscriptionName)  
  129.        {  
  130.            var consumer = new ServiceBusAdministrationClient(connectionString);  
  131.            return await consumer.DeleteSubscriptionAsync(topicName, subscriptionName);  
  132.        }  
  133.   
  134.    }  

Creating the Objects

  1.    non-public static readonly string ConnectionString = “Endpoint=sb://sampleservbusnamespace.servicebus.home windows.internet/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=+SXXUY4kweUSphE1l78avtHWrp9NyDGKPiePOi/KWfw=”;  
  2.         non-public static readonly string QueueName = “sampleQueue”;  
  3.         non-public static readonly string SessionQueueName = “sampleSessionQueue”;  
  4.         non-public static readonly string TopicName = “sampleTopic”;  
  5.         non-public static readonly string SubscriptionName = “sampleSubscription”;  
  6.         static async Activity Fundamental(string[] args)  
  7.         {  
  8.             Console.WriteLine(“Howdy World!”);  
  9.  
  10.             #area Creating Objects  
  11.             QueueProperties queue = await ServiceBusObjects.GetQueueAsync(ConnectionString, QueueName);  
  12.             if (queue == null)  
  13.                 queue = await ServiceBusObjects.CreateQueueAsync(ConnectionString, QueueName);  
  14.   
  15.             QueueProperties sessionQueue = await ServiceBusObjects.GetQueueAsync(ConnectionString, SessionQueueName);  
  16.             if (sessionQueue == null)  
  17.                 sessionQueue = await ServiceBusObjects.CreateQueueAsync(ConnectionString, SessionQueueName, true);  
  18.   
  19.             TopicProperties subject = await ServiceBusObjects.GetTopicAsync(TopicName, ConnectionString);  
  20.             if (subject == null)  
  21.                 subject = await ServiceBusObjects.CreateTopicAsync(TopicName, ConnectionString);  
  22.   
  23.             SubscriptionProperties subscription = await ServiceBusObjects.GetSubscriptionAsync(TopicName, ConnectionString, SubscriptionName);  
  24.             if (subscription == null)  
  25.                 subscription = await ServiceBusObjects.CreateSubscriptionAsync(TopicName, ConnectionString, SubscriptionName);  
  26.  
  27.             #endregion  
  28. }   

End result 

 

 

 

 

Consuming and Publishing to Service Bus with .Web Core

 

Creating the Technique to publish and eat to queues 

  1. public class SendAndReceiveMessage  
  2.     {  
  3.         public static async Activity SendMessageAsync(string connectionString, string queueName, string messageText)  
  4.         {  
  5.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  6.   
  7.               
  8.             ServiceBusSender sender = consumer.CreateSender(queueName);  
  9.   
  10.               
  11.             ServiceBusMessage message = new ServiceBusMessage(messageText);  
  12.   
  13.               
  14.             await sender.SendMessageAsync(message);  
  15.         }  
  16.         public static async Activity SendSessionMessageAsync(string connectionString, string queueName, string messageText, string sessionId)  
  17.         {  
  18.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  19.   
  20.               
  21.             ServiceBusSender sender = consumer.CreateSender(queueName);  
  22.   
  23.               
  24.             ServiceBusMessage message = new ServiceBusMessage(messageText)  
  25.             {  
  26.                 SessionId = sessionId  
  27.             };  
  28.   
  29.               
  30.             await sender.SendMessageAsync(message);  
  31.         }  
  32.         public static async Activity<lengthy> SendScheduledMessageAsync(string connectionString, string queueName, string messageText, DateTimeOffset timeOffset)  
  33.         {  
  34.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  35.   
  36.               
  37.             ServiceBusSender sender = consumer.CreateSender(queueName);  
  38.   
  39.               
  40.             ServiceBusMessage message = new ServiceBusMessage(messageText);  
  41.   
  42.               
  43.             return await sender.ScheduleMessageAsync(message, timeOffset);  
  44.         }  
  45.   
  46.   
  47.         public static async Activity CancelScheduledMessageAsync(string connectionString, string queueName, lengthy messageSequenceNumber)  
  48.         {  
  49.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  50.   
  51.               
  52.             ServiceBusSender sender = consumer.CreateSender(queueName);  
  53.   
  54.             await sender.CancelScheduledMessageAsync(messageSequenceNumber);  
  55.   
  56.         }  
  57.         public static async Activity<lengthy> DeferMessageAsync(string connectionString, string queueName)  
  58.         {  
  59.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  60.   
  61.               
  62.             ServiceBusReceiver receiver = consumer.CreateReceiver(queueName);  
  63.   
  64.             ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();  
  65.   
  66.               
  67.               
  68.             await receiver.DeferMessageAsync(receivedMessage);  
  69.   
  70.             return receivedMessage.SequenceNumber;  
  71.   
  72.         }  
  73.   
  74.         public static async Activity<ServiceBusReceivedMessage> GetDeferredMessageAsync(string connectionString, string queueName, lengthy messageSequenceNumber)  
  75.         {  
  76.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  77.   
  78.               
  79.             ServiceBusReceiver receiver = consumer.CreateReceiver(queueName);  
  80.   
  81.               
  82.               
  83.             return await receiver.ReceiveDeferredMessageAsync(messageSequenceNumber);  
  84.         }  
  85.   
  86.         public static async Activity DeadLetterMessageAsync(string connectionString, string queueName)  
  87.         {  
  88.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  89.   
  90.               
  91.             ServiceBusReceiver receiver = consumer.CreateReceiver(queueName);  
  92.   
  93.             ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();  
  94.   
  95.               
  96.             await receiver.DeadLetterMessageAsync(receivedMessage);  
  97.         }  
  98.         public static async Activity<ServiceBusReceivedMessage> GetDeadLetterMessageAsync(string connectionString, string queueName)  
  99.         {  
  100.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  101.   
  102.               
  103.             ServiceBusReceiver receiver = consumer.CreateReceiver(queueName, new ServiceBusReceiverOptions  
  104.             {  
  105.                 SubQueue = SubQueue.DeadLetter  
  106.             });  
  107.   
  108.               
  109.             return await receiver.ReceiveMessageAsync();  
  110.         }  
  111.   
  112.         public static async Activity<ServiceBusReceivedMessage> GetMessageAsync(string connectionString, string queueName)  
  113.         {  
  114.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  115.   
  116.               
  117.             ServiceBusReceiver receiver = consumer.CreateReceiver(queueName);  
  118.   
  119.               
  120.             return await receiver.ReceiveMessageAsync();  
  121.         }  
  122.         public static async Activity<ServiceBusReceivedMessage> GetMessageFromSessionAsync(string connectionString, string queueName, string sessionId)  
  123.         {  
  124.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  125.   
  126.             ServiceBusSessionReceiver receiver = null;  
  127.             if (string.IsNullOrEmpty(sessionId))  
  128.                 receiver = await consumer.AcceptNextSessionAsync(queueName);  
  129.             else  
  130.                 receiver = await consumer.AcceptSessionAsync(queueName, sessionId);  
  131.   
  132.               
  133.             return await receiver.ReceiveMessageAsync();  
  134.         }  
  135.         public static async Activity CompleteOrAbandonMessageAsync(string connectionString, string queueName, bool abandon)  
  136.         {  
  137.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  138.   
  139.               
  140.             ServiceBusReceiver receiver = consumer.CreateReceiver(queueName);  
  141.   
  142.               
  143.             ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();  
  144.   
  145.             Console.WriteLine($“Message obtained {receivedMessage.Physique} to be deserted {abandon}”);  
  146.   
  147.             if (!abandon)   
  148.                 await receiver.CompleteMessageAsync(receivedMessage);  
  149.             else   
  150.                 await receiver.AbandonMessageAsync(receivedMessage);  
  151.         }  
  152.   
  153.   
  154.         public static async Activity SendMessageBatchAsync(string connectionString, string queueName, Listing<string> messageTexts)  
  155.         {  
  156.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  157.   
  158.               
  159.             ServiceBusSender sender = consumer.CreateSender(queueName);  
  160.   
  161.               
  162.             IList<ServiceBusMessage> messages = new Listing<ServiceBusMessage>();  
  163.   
  164.             messageTexts.ForEach(msg => messages.Add(new ServiceBusMessage(msg)));  
  165.   
  166.               
  167.             await sender.SendMessagesAsync(messages);  
  168.         }  
  169.         public static async Activity SendMessageSafeBatchAsync(string connectionString, string queueName, Listing<string> messageTexts)  
  170.         {  
  171.             await utilizing var consumer = new ServiceBusClient(connectionString);  
  172.   
  173.               
  174.             ServiceBusSender sender = consumer.CreateSender(queueName);  
  175.   
  176.               
  177.             Queue<ServiceBusMessage> messages = new Queue<ServiceBusMessage>();  
  178.   
  179.             messageTexts.ForEach(msg => messages.Enqueue(new ServiceBusMessage(msg)));  
  180.   
  181.             whereas (messages.Depend > 0)  
  182.             {  
  183.                   
  184.                 utilizing ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();  
  185.   
  186.                   
  187.                 if (messageBatch.TryAddMessage(messages.Peek()))  
  188.                 {  
  189.                       
  190.                     messages.Dequeue();  
  191.                 }  
  192.                 else  
  193.                 {  
  194.                       
  195.                     throw new Exception($“Message {messageTexts.Depend – messages.Depend} is too giant and can not be despatched.”);  
  196.                 }  
  197.   
  198.                   
  199.                 whereas (messages.Depend > 0 && messageBatch.TryAddMessage(messages.Peek()))  
  200.                 {  
  201.                       
  202.                     messages.Dequeue();  
  203.                 }  
  204.   
  205.                   
  206.                 await sender.SendMessagesAsync(messageBatch);  
  207.   
  208.             }  
  209.         }  
  210.     }  

Publishing and Consuming

  1. non-public static readonly string ConnectionString = “Endpoint=sb://sampleservbusnamespace.servicebus.home windows.internet/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=+SXXUY4kweUSphE1l78avtHWrp9NyDGKPiePOi/KWfw=”;  
  2. non-public static readonly string QueueName = “sampleQueue”;  
  3. non-public static readonly string SessionQueueName = “sampleSessionQueue”;  
  4. non-public static readonly string TopicName = “sampleTopic”;  
  5. non-public static readonly string SubscriptionName = “sampleSubscription”;  
  6. static async Activity Fundamental(string[] args)  
  7. {  
  8.     Console.WriteLine(“Howdy World!”);  
  9.     #area Sending and Receiving Messages  
  10.     int depend = 0;  
  11.   
  12.       
  13.     await SendAndReceiveMessage.SendMessageAsync(ConnectionString, QueueName, $“Message {depend++}”);  
  14.     await SendAndReceiveMessage.SendMessageBatchAsync(ConnectionString, QueueName, new System.Collections.Generic.Listing<string> { $“Message {depend++}”, $“Message {depend++}”, $“Message {depend++}”, $“Message {depend++}” });  
  15.     await SendAndReceiveMessage.SendMessageSafeBatchAsync(ConnectionString, QueueName, new System.Collections.Generic.Listing<string> { $“Message {depend++}”, $“Message {depend++}”, $“Message {depend++}”, $“Message {depend++}” });  
  16.     lengthy firstScheduledMessageNumber = await SendAndReceiveMessage.SendScheduledMessageAsync(ConnectionString, QueueName, $“Message {depend++}”new DateTimeOffset(DateTime.Now.AddMinutes(10)));  
  17.     lengthy secondScheduledMessageNumber = await SendAndReceiveMessage.SendScheduledMessageAsync(ConnectionString, QueueName, $“Message {depend++}”new DateTimeOffset(DateTime.Now.AddMinutes(10)));  
  18.     await SendAndReceiveMessage.CancelScheduledMessageAsync(ConnectionString, QueueName, firstScheduledMessageNumber);  
  19.     lengthy deferredMessageNumber = await SendAndReceiveMessage.DeferMessageAsync(ConnectionString, QueueName);  
  20.     Console.WriteLine((await SendAndReceiveMessage.GetDeferredMessageAsync(ConnectionString, QueueName, deferredMessageNumber)).Physique);  
  21.     await SendAndReceiveMessage.DeadLetterMessageAsync(ConnectionString, QueueName);  
  22.     Console.WriteLine((await SendAndReceiveMessage.GetDeadLetterMessageAsync(ConnectionString, QueueName)).Physique);  
  23.     Console.WriteLine((await SendAndReceiveMessage.GetMessageAsync(ConnectionString, QueueName)).Physique);  
  24.     await SendAndReceiveMessage.CompleteOrAbandonMessageAsync(ConnectionString, QueueName, false);  
  25.     await SendAndReceiveMessage.CompleteOrAbandonMessageAsync(ConnectionString, QueueName, true);  
  26.     #endregion  
  27.  
  28.     #area Sending and Receiving Session Messages  
  29.     await SendAndReceiveMessage.SendSessionMessageAsync(ConnectionString, SessionQueueName, $“Message {depend++}”“session id 1”);  
  30.     Console.WriteLine((await SendAndReceiveMessage.GetMessageFromSessionAsync(ConnectionString, SessionQueueName, “session id 1”)).Physique);  
  31.     #endregion  

Outcomes

 

 

 

Utilizing Service Bus Processor with .Web Core 

 

Message Processor Technique

  1. class Processor  
  2.    {  
  3.        public static async Activity RunProcessor(string connectionString, string queueName, TimeSpan timeSpan)  
  4.        {  
  5.              
  6.            await utilizing var consumer = new ServiceBusClient(connectionString);  
  7.   
  8.              
  9.            var choices = new ServiceBusProcessorOptions  
  10.            {  
  11.                  
  12.                  
  13.                AutoCompleteMessages = false,  
  14.   
  15.                  
  16.                MaxConcurrentCalls = 2  
  17.            };  
  18.   
  19.              
  20.            ServiceBusProcessor processor = consumer.CreateProcessor(queueName, choices);  
  21.   
  22.            processor.ProcessMessageAsync += MessageHandler;  
  23.            processor.ProcessErrorAsync += ErrorHandler;  
  24.   
  25.            await processor.StartProcessingAsync();  
  26.   
  27.              
  28.              
  29.            DateTime endProcessing = DateTime.Now.Add(timeSpan);  
  30.            whereas (DateTime.Now < endProcessing)  
  31.               await  Activity.Delay(100);  
  32.   
  33.              
  34.            await processor.StopProcessingAsync();  
  35.        }  
  36.   
  37.        non-public static async Activity MessageHandler(ProcessMessageEventArgs args)  
  38.        {  
  39.            string physique = args.Message.Physique.ToString();  
  40.            Console.WriteLine($“Message obtained from processor: {physique}”);  
  41.   
  42.              
  43.            await args.CompleteMessageAsync(args.Message);  
  44.        }  
  45.   
  46.        non-public static Activity ErrorHandler(ProcessErrorEventArgs args)  
  47.        {  
  48.              
  49.            Console.WriteLine(args.ErrorSource);  
  50.              
  51.            Console.WriteLine(args.FullyQualifiedNamespace);  
  52.              
  53.            Console.WriteLine(args.EntityPath);  
  54.            Console.WriteLine(args.Exception.ToString());  
  55.            return Activity.CompletedTask;  
  56.        }  
  57.    }  

Utilizing Service Bus Session Processor with .Web Core 

 

Session Processor Technique

  1. class SessionProcessor  
  2.    {  
  3.        public static async Activity RunSessionProcessor(string connectionString, string queueName, TimeSpan timeSpan)  
  4.        {  
  5.              
  6.            await utilizing var consumer = new ServiceBusClient(connectionString);  
  7.   
  8.              
  9.            var choices = new ServiceBusSessionProcessorOptions  
  10.            {  
  11.                  
  12.                  
  13.                AutoCompleteMessages = false,  
  14.   
  15.                  
  16.                MaxConcurrentSessions = 5,  
  17.   
  18.                  
  19.                  
  20.                MaxConcurrentCallsPerSession = 2  
  21.            };  
  22.   
  23.              
  24.            ServiceBusSessionProcessor processor = consumer.CreateSessionProcessor(queueName, choices);  
  25.   
  26.            processor.ProcessMessageAsync += MessageHandler;  
  27.            processor.ProcessErrorAsync += ErrorHandler;  
  28.   
  29.            await processor.StartProcessingAsync();  
  30.   
  31.              
  32.              
  33.            DateTime endProcessing = DateTime.Now.Add(timeSpan);  
  34.            whereas (DateTime.Now < endProcessing)  
  35.                await Activity.Delay(100);  
  36.   
  37.              
  38.            await processor.StopProcessingAsync();  
  39.        }  
  40.   
  41.        non-public static async Activity MessageHandler(ProcessSessionMessageEventArgs args)  
  42.        {  
  43.            string physique = args.Message.Physique.ToString();  
  44.            Console.WriteLine($“Message obtained from session processor: {physique}”);  
  45.   
  46.              
  47.            await args.CompleteMessageAsync(args.Message);  
  48.   
  49.              
  50.              
  51.            await args.SetSessionStateAsync(new BinaryData(“some state”));  
  52.        }  
  53.   
  54.        non-public static Activity ErrorHandler(ProcessErrorEventArgs args)  
  55.        {  
  56.              
  57.            Console.WriteLine(args.ErrorSource);  
  58.              
  59.            Console.WriteLine(args.FullyQualifiedNamespace);  
  60.              
  61.            Console.WriteLine(args.EntityPath);  
  62.            Console.WriteLine(args.Exception.ToString());  
  63.            return Activity.CompletedTask;  
  64.        }  
  65.    }  

Azure Queue Storage queues with .Web Core

 

Conditions

  • Nuget Package deal Azure.Storage.Queues;
  • Azure Storage Account beforehand created. Right here named as “storageaccountnormal”; 

 

Creating Queue Storage objects with .Web Core

 

Strategies

  1. public class StorageQueueObjects  
  2.    {  
  3.          
  4.          
  5.          
  6.        public async static Activity<QueueClient> CreateQueue(string queueName, string connectionString)  
  7.        {  
  8.              
  9.            QueueClient queueClient = new QueueClient(connectionString, queueName);  
  10.   
  11.              
  12.            await queueClient.CreateIfNotExistsAsync();  
  13.   
  14.            return queueClient;  
  15.        }  
  16.   
  17.          
  18.          
  19.          
  20.        public async static Activity<Azure.Response<bool>> DeleteQueue(string queueName, string connectionString)  
  21.        {  
  22.              
  23.            QueueClient queueClient = new QueueClient(connectionString, queueName);  
  24.   
  25.              
  26.            return await queueClient.DeleteIfExistsAsync();  
  27.        }  
  28.    }   

Creating the Objects

  1.  non-public readonly static string ConnectionString = “DefaultEndpointsProtocol=https;AccountName=storageaccountnormal;AccountKey=ypYvvkSb93K4ca3YYNvh92+oyOZZLmn6eJh0TBHc5h54DtFd22sOLGtgjeu2/30AyF948HaT125nbEj4HxCvWQ==;EndpointSuffix=core.home windows.internet”;  
  2.         non-public readonly static string QueueName = “samplestoragequeue”;  
  3.   
  4.         static async Activity Fundamental(string[] args)  
  5.         {  
  6.             Console.WriteLine(“Howdy World!”);  
  7.   
  8.             QueueClient queue = await StorageQueueObjects.CreateQueue(QueueName, ConnectionString);  
  9. }  

End result

 

 

Receiving and Publishing to Azure Queue Storage with .Web Core 

 

Strategies

  1. public class SendAndReceiveMessage  
  2.     {  
  3.           
  4.           
  5.           
  6.         public static async Activity<Azure.Response<SendReceipt>> InsertMessage(QueueClient queueClient, string message)  
  7.         {  
  8.             return await queueClient.SendMessageAsync(message);  
  9.         }  
  10.           
  11.           
  12.           
  13.         public static async Activity<Azure.Response<PeekedMessage[]>> PeekMessage(QueueClient queueClient)  
  14.         {  
  15.             return await queueClient.PeekMessagesAsync();  
  16.         }  
  17.   
  18.           
  19.           
  20.           
  21.         public static async Activity<QueueMessage[]> DequeueMessage(QueueClient queueClient)  
  22.         {  
  23.               
  24.             QueueMessage[] retrievedMessage = await queueClient.ReceiveMessagesAsync();  
  25.   
  26.   
  27.               
  28.             await queueClient.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);  
  29.   
  30.             return retrievedMessage;  
  31.         }  
  32.   
  33.   
  34.           
  35.           
  36.           
  37.         public static async Activity<QueueMessage[]> DequeueMessages(QueueClient queueClient)  
  38.         {  
  39.               
  40.             QueueMessage[] receivedMessages = await queueClient.ReceiveMessagesAsync(20, TimeSpan.FromMinutes(5));  
  41.   
  42.             foreach (QueueMessage message in receivedMessages)  
  43.             {  
  44.                   
  45.                 await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);  
  46.             }  
  47.   
  48.             return receivedMessages;  
  49.         }  
  50.     }  

Receiving and Publishing Messages

  1. non-public readonly static string ConnectionString = “DefaultEndpointsProtocol=https;AccountName=storageaccountnormal;AccountKey=ypYvvkSb93K4ca3YYNvh92+oyOZZLmn6eJh0TBHc5h54DtFd22sOLGtgjeu2/30AyF948HaT125nbEj4HxCvWQ==;EndpointSuffix=core.home windows.internet”;  
  2.       non-public readonly static string QueueName = “samplestoragequeue”;  
  3.   
  4.       static async Activity Fundamental(string[] args)  
  5.       {  
  6.           Console.WriteLine(“Howdy World!”);  
  7.   
  8.           QueueClient queue = await StorageQueueObjects.CreateQueue(QueueName, ConnectionString);  
  9.   
  10.             
  11.           for (int i = 0; i < 50; i++)  
  12.           {  
  13.               await SendAndReceiveMessage.InsertMessage(queue, “Message Quantity “ + i);  
  14.           }  
  15.   
  16.           PeekedMessage[] peekedMessages = await SendAndReceiveMessage.PeekMessage(queue);  
  17.           peekedMessages.ToList().ForEach(x => Console.WriteLine(“Message peeked: “ + x.Physique));  
  18.   
  19.           QueueMessage[] message = await SendAndReceiveMessage.DequeueMessage(queue);  
  20.           Console.WriteLine(“Single Message dequeued: “ + message[0].Physique);  
  21.   
  22.           QueueMessage[] messages = await SendAndReceiveMessage.DequeueMessages(queue);  
  23.           messages.ToList().ForEach(x => Console.WriteLine(“Message dequeued: “ + x.Physique));  
  24.   
  25.       }   

End result 

 

 

 

Exterior References

Show More

Related Articles

Leave a Reply

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

Back to top button