123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using Microsoft.Azure.Cosmos.Table;
- using Microsoft.Azure.Cosmos.Table.Queryable;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Linq;
- using Azure.Messaging.ServiceBus;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- namespace TEAMModelOS.SDK.DI
- {
- public static class AzureServiceBusExtensions
- {
- public static Dictionary<string, ServiceBusClient> topClients { get; set; } = new Dictionary<string, ServiceBusClient>();
- /// <summary>
- /// 發送信息至對列或主題
- /// </summary>
- /// <param name="pathName">QueueName or TopicName</param>
- /// <param name="message">訊息</param>
- /// <returns></returns>
- public static async Task SendMessageAsync(this ServiceBusClient client, string pathName, string message)
- {
- try
- {
- ServiceBusSender sender = client.CreateSender(pathName);
- await sender.SendMessageAsync(new ServiceBusMessage(Encoding.UTF8.GetBytes(message)));
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// 批量發送訊息至對列或主題,如果批量失敗返回False
- /// </summary>
- /// <param name="pathName">QueueName or TopicName</param>
- /// <param name="messages">批量訊息</param>
- /// <returns></returns>
- public static async Task<bool> SendBatchMessageAsync(this ServiceBusClient client, string pathName, IList<string> messages)
- {
- try
- {
- ServiceBusSender sender = client.CreateSender(pathName);
- ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();
- foreach (var msg in messages)
- {
- if (!messageBatch.TryAddMessage(new ServiceBusMessage(Encoding.UTF8.GetBytes(msg)))) return false;
- }
- await sender.SendMessagesAsync(messageBatch);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 發送信息至對列或主題(指定時間排程)
- /// </summary>
- /// <param name="pathName">QueueName or TopicName</param>
- /// <param name="message">訊息</param>
- /// <returns>排程訊息的序列號。</returns>
- public static async Task<long> SendScheduleMessageAsync(this ServiceBusClient client, string pathName, string message, DateTimeOffset scheduleTime)
- {
- try
- {
- ServiceBusSender sender = client.CreateSender(pathName);
- return await sender.ScheduleMessageAsync(new ServiceBusMessage(Encoding.UTF8.GetBytes(message)), scheduleTime);
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// 批量發送訊息至對列或主題(指定時間排程),如果批量失敗返回False
- /// </summary>
- /// <param name="pathName">QueueName or TopicName</param>
- /// <param name="messages">批量訊息</param>
- /// <returns>排程訊息的序列號</returns>
- public static async Task<long[]> SendScheduleMessagesAsync(this ServiceBusClient client, string pathName, IList<string> messages, DateTimeOffset scheduleTime)
- {
- try
- {
- ServiceBusSender sender = client.CreateSender(pathName);
- List<ServiceBusMessage> msgs = new List<ServiceBusMessage>() { };
- foreach (var msg in messages)
- {
- msgs.Add(new ServiceBusMessage(Encoding.UTF8.GetBytes(msg)));
- }
- return await sender.ScheduleMessagesAsync(msgs, scheduleTime);
- }
- catch
- {
- return null;
- }
- }
- }
- }
|