private async Task SendMessageToTopic <T>(T message, AzureMessageContext context)
        {
            try
            {
                var mgmtQueueClient = new ManagementClient(context.ConnectionString);

                var topics = await mgmtQueueClient.GetTopicsAsync();

                if (topics.Count == 0 ||
                    (!topics.Any(x => x.Path == context.QueueOrTopicName.ToLower())))
                {
                    await mgmtQueueClient.CreateTopicAsync(context.QueueOrTopicName);
                }

                topicClient = new TopicClient(context.ConnectionString, context.QueueOrTopicName);

                topicClient.ServiceBusConnection.TransportType = TransportType.AmqpWebSockets;

                var sbMessage = FormatMessage(message);

                topicClient.SendAsync(sbMessage).Wait();
            }
            catch (ServiceBusException sbex)
            {
                throw sbex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static AzureMessageContext GetAzureAssets(string config)
        {
            /*Sample config: "StorageAccount|Queue" */

            AzureMessageContext response = new AzureMessageContext();
            string asstype = string.Empty, queuetype = string.Empty;

            if (!string.IsNullOrWhiteSpace(config))
            {
                string[] configArray = config?.Split('|');
                if (configArray?.Length > 0)
                {
                    asstype = configArray[0];
                }
                if (configArray.Length > 1)
                {
                    queuetype = configArray[1];
                }
            }

            switch (asstype) // determine the type of service to use based on configuratino
            {
            case "ServiceBus":
                response.AssetsType       = AssetsType.ServiceBus;
                response.ConnectionString = Environment.GetEnvironmentVariable(AppSettingsKey.SampleApp_ServiceBus_ConnectionString);
                break;

            case "StorageAccount":
            default:
                response.AssetsType       = AssetsType.StorageAccount;
                response.ConnectionString = Environment.GetEnvironmentVariable(AppSettingsKey.SampleApp_StorageAccount_ConnectionString);
                break;
            }
            switch (queuetype) // // determine the type of queue service to use based on configuration
            {
            case "Topic":
                response.QueueType        = response.AssetsType == AssetsType.ServiceBus ? QueueType.Topic : QueueType.Queue;
                response.QueueOrTopicName = response.AssetsType == AssetsType.ServiceBus ?
                                            Environment.GetEnvironmentVariable(AppSettingsKey.SampleApp_ServiceBus_TopicName)
                        :
                                            Environment.GetEnvironmentVariable(AppSettingsKey.SampleApp_StorageQueueName);
                break;

            case "Queue":
            default:
                response.QueueType        = QueueType.Queue;
                response.QueueOrTopicName = response.AssetsType == AssetsType.ServiceBus ?
                                            Environment.GetEnvironmentVariable(AppSettingsKey.SampleApp_ServiceBus_QueueName)
                        :
                                            Environment.GetEnvironmentVariable(AppSettingsKey.SampleApp_StorageQueueName);
                break;
            }

            return(response);
        }
Exemplo n.º 3
0
 public async Task SendMessage <T>(T message, AzureMessageContext context)
 {
     try
     {
         Initialize(context);
         var hasMessageSent = await AddMessageAsync <T>(context.QueueOrTopicName, message);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public async Task SendMessage <T>(T message, AzureMessageContext context)
 {
     try
     {
         if (context.QueueType == QueueType.Queue)
         {
             await SendMessageToQueue(message, context);
         }
         else
         {
             await SendMessageToTopic(message, context);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 5
0
        public async Task SendMessages <T>(List <T> messages, AzureMessageContext context)
        {
            try
            {
                Initialize(context);
                List <Task <bool> > postMessageTask = new List <Task <bool> >();
                foreach (var message in messages)
                {
                    postMessageTask.Add(AddMessageAsync <T>(context.QueueOrTopicName, message));
                }

                await Task.WhenAll(postMessageTask);

                bool isSuccess = postMessageTask.All(r => r.Result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 6
0
 private void Initialize(AzureMessageContext context)
 {
     _storageAccount   = CloudStorageAccount.Parse(context.ConnectionString);
     _cloudQueueClient = _storageAccount.CreateCloudQueueClient();
 }