|
@@ -0,0 +1,74 @@
|
|
|
+using Microsoft.Azure.ServiceBus;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Text;
|
|
|
+
|
|
|
+namespace TEAMModelOS.SDK.Module.AzureServiceBus
|
|
|
+{
|
|
|
+ public class AzureServiceBusClientSingleton
|
|
|
+ {
|
|
|
+
|
|
|
+ private ServiceBusModel ServiceBusModel;
|
|
|
+ public static AzureServiceBusOptions azureServiceBusOptions;
|
|
|
+ private AzureServiceBusClientSingleton() { }
|
|
|
+ public ServiceBusModel GetServiceBusClient()
|
|
|
+ {
|
|
|
+ if (ServiceBusModel != null)
|
|
|
+ {
|
|
|
+ return ServiceBusModel;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ getInstance(azureServiceBusOptions);
|
|
|
+ return ServiceBusModel;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static AzureServiceBusClientSingleton getInstance(AzureServiceBusOptions _azureServiceBusOptions)
|
|
|
+ {
|
|
|
+ azureServiceBusOptions= _azureServiceBusOptions;
|
|
|
+
|
|
|
+
|
|
|
+ return SingletonInstance.instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class SingletonInstance
|
|
|
+ {
|
|
|
+ public static AzureServiceBusClientSingleton instance = new AzureServiceBusClientSingleton()
|
|
|
+ {
|
|
|
+ ServiceBusModel = new ServiceBusModel { topClients = GetTopClients(),subClients=GetSubClients() }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Dictionary<string, TopClient> GetTopClients() {
|
|
|
+ Dictionary<string, TopClient> topClients = new Dictionary<string, TopClient>();
|
|
|
+ string ConnectionString = azureServiceBusOptions.ConnectionString;
|
|
|
+ azureServiceBusOptions.Topics.ForEach(x=> { topClients.TryAdd(x.Name, new TopClient { topicClient = new TopicClient(ConnectionString, x.Name) }); });
|
|
|
+ return topClients;
|
|
|
+ }
|
|
|
+ private static Dictionary<string, SubClient> GetSubClients()
|
|
|
+ {
|
|
|
+ Dictionary<string, SubClient> subClients = new Dictionary<string, SubClient>();
|
|
|
+ string ConnectionString = azureServiceBusOptions.ConnectionString;
|
|
|
+ azureServiceBusOptions.Topics.ForEach(x => { x.Subscribe.ForEach(y => { subClients.TryAdd(y, new SubClient { topName = x.Name, subscriptionClient = new SubscriptionClient(ConnectionString, x.Name, y) }); }); });
|
|
|
+ return subClients;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public class ServiceBusModel {
|
|
|
+ public Dictionary<string, TopClient> topClients { get; set; } = new Dictionary<string, TopClient>();
|
|
|
+ public Dictionary<string, SubClient> subClients { get; set; } = new Dictionary<string, SubClient>();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public class TopClient {
|
|
|
+
|
|
|
+ public ITopicClient topicClient { get; set; }
|
|
|
+ }
|
|
|
+ public class SubClient
|
|
|
+ {
|
|
|
+ public string topName { get; set; }
|
|
|
+
|
|
|
+ public ISubscriptionClient subscriptionClient { get; set; }
|
|
|
+ }
|
|
|
+}
|