应用程序启动时的ASP.NET Core启动侦听器

时间:2020-02-17 12:23:54

标签: c# azure asp.net-core

我正在使用Azure服务总线队列,并且想在我的应用程序中创建一个侦听器,以侦听添加到队列中的新项目,然后将其存储在本地/发送通知。

我需要侦听器从应用程序开始并以应用程序生命周期运行。

按照Microsoft的教程,我创建了两个工作正常的控制台应用程序。然后,我将接收器调整为一个接口,在构造函数中启动侦听器。

以下内容如何最好地链接到应用程序启动,以便它永久侦听队列中的新消息。

我的界面如下:

namespace PH.Services.AzureServiceBusQueue
{
    public interface IAzureServiceBusQueueClient
    {
    }

    public class AzureServiceBusQueueClient : IAzureServiceBusQueueClient
    {
        // Connection String for the namespace can be obtained from the Azure portal under the 
        // 'Shared Access policies' section.
        const string ServiceBusConnectionString = "connection string";
        const string QueueName = "QueueName";
        static IQueueClient queueClient;
        private readonly CloudStorageSettings _settings;

        public AzureServiceBusQueueClient()
        {
            //_settings = settings;
        ConfigureClient();
    }

    private void ConfigureClient()
    {
        queueClient = new QueueClient(ServiceBusConnectionString, QueueName);

        // Register QueueClient's MessageHandler and receive messages in a loop
        RegisterOnMessageHandlerAndReceiveMessages();

        queueClient.CloseAsync();
    }

    static void RegisterOnMessageHandlerAndReceiveMessages()
    {
        // Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
        var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
        {
            // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
            // Set it according to how many messages the application wants to process in parallel.
            MaxConcurrentCalls = 1,

            // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
            // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
            AutoComplete = false
        };

        // Register the function that will process messages
        queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
    }

    static async Task ProcessMessagesAsync(Message message, CancellationToken token)
    {
        // Process the message
        Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");

        // Complete the message so that it is not received again.
        // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
        await queueClient.CompleteAsync(message.SystemProperties.LockToken);

        // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
        // If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls 
        // to avoid unnecessary exceptions.
    }

    static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
    {
        Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
        var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
        Console.WriteLine("Exception context for troubleshooting:");
        Console.WriteLine($"- Endpoint: {context.Endpoint}");
        Console.WriteLine($"- Entity Path: {context.EntityPath}");
        Console.WriteLine($"- Executing Action: {context.Action}");
        return Task.CompletedTask;
    }
}

0 个答案:

没有答案