예제 #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  message     = req.Query["message"];
            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            message = message ?? data?.message;

            var storageConnection = Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process);
            var queueClient       = new QueueClient(storageConnection, "myqueue-items");

            if (!await queueClient.ExistsAsync()) //check if exists explicitly to avoid 409 trace information in the logs.
            {
                await queueClient.CreateIfNotExistsAsync();
            }

            var base64String = GetUTF8String(message);

            var receipt = await queueClient.SendMessageAsync(base64String);

            return(new OkObjectResult($"Added a new message with id {receipt.Value.MessageId}"));
        }
예제 #2
0
        static async Task Main(string[] args)
        {
            QueueClient client = new QueueClient("UseDevelopmentStorage=true", "vsqueue");

            if (await client.ExistsAsync())
            {
                await client.SendMessageAsync("vstext");

                while (true)
                {
                    QueueMessage[] messages = client.ReceiveMessages();
                    if (messages.Count() > 0)
                    {
                        foreach (var message in messages)
                        {
                            var result = await ProcessMessage(message);

                            if (result)
                            {
                                await client.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                            }
                            else
                            {
                                await client.SendMessageAsync(message.MessageText);
                            }
                        }
                    }
                }
            }
        }
        public async Task <ValueModel <Message> > Enqueue <T>(T contract) where T : class
        {
            contract.ThrowIfNull(nameof(contract));

            var data = _serializer.SerializeToJson(contract);

            var message = new Message
            {
                Type = contract.GetType().Name,
                Data = data
            };

            var messageJson = _serializer.SerializeToJson(message);

            await _queueClient.CreateIfNotExistsAsync();

            if (await _queueClient.ExistsAsync())
            {
                await _queueClient.SendMessageAsync(messageJson);

                return(new ValueModel <Message>(message));
            }

            return(new ValueModel <Message>());
        }
        public async Task RecieveMessagesAsync()
        {
            _logger.LogInformation("Starting receiver");

            while (!_stopping)
            {
                if (await _queueClient.ExistsAsync())
                {
                    // Get the next message
                    QueueMessage[] retrievedMessage = await _queueClient.ReceiveMessagesAsync();

                    foreach (var msg in retrievedMessage)
                    {
                        await HandleMessageAsync(msg);
                    }

                    continue;
                }

                //Wait for 1 second before polling
                await Task.Delay(1000);
            }

            _logger.LogInformation("Stopping Reciever.");
        }
        public async Task <bool> Create(string queueName)
        {
            try
            {
                var queueClient = new QueueClient(Options.ConnectionString, queueName);
                await queueClient.CreateIfNotExistsAsync();

                if (await queueClient.ExistsAsync())
                {
                    WriteLine($"Queue created: '{queueClient.Name}'");
                    return(true);
                }
                else
                {
                    WriteLine($"Make sure the Azurite storage emulator running and try again.");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                WriteLine($"Exception: {ex.Message}\n\n");
                WriteLine($"Make sure the Azurite storage emulator running and try again.");
                return(false);
            }
        }
예제 #6
0
        public async Task DataCleanup_DeleteQueues_ShouldWork()
        {
            // Setup
            QueueClient queueClient = await SetupTestQueue();

            var queueHelper = new QueueHelper(StorageConnectionString);

            (await queueHelper.GetQueues()).Should().HaveCountGreaterOrEqualTo(1);

            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = StorageConnectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // Act
            HttpResponseMessage response = await _clientFixture.GetClient().PostAsync(_UrlDeleteQueues, content);

            // Verify
            response.StatusCode.Should().Be(HttpStatusCode.OK);
            bool queueExists = await queueClient.ExistsAsync();

            queueExists.Should().BeFalse();
            (await queueHelper.GetQueues()).Should().HaveCount(0);
        }
예제 #7
0
        // Helper method for creating queueClient
        async Task <QueueClient> CreateQueueClient()
        {
            if (String.IsNullOrWhiteSpace(_connectionString) || String.IsNullOrWhiteSpace(_queueName))
            {
                throw new NullReferenceException("ConnectionString or Queue name is null. Remember to call Init() method, providing a connection string and queue name");
            }

            var queueClientOptions = new QueueClientOptions()
            {
                MessageEncoding = QueueMessageEncoding.Base64
            };

            // Instantiate a QueueClient which will be used to create and manipulate the queue
            QueueClient queueClient = new QueueClient(_connectionString, _queueName, queueClientOptions);

            var logMessagePrefix = $"Ensuring queue '{queueClient.Name}' exists. ";

            if (await queueClient.ExistsAsync())
            {
                _logger.LogTrace(logMessagePrefix + "Allready exists");
            }
            else
            {
                _logger.LogTrace(logMessagePrefix + "Did not exsist. Will create it");
            }

            // Create the queue if it doesn't already exist
            await queueClient.CreateIfNotExistsAsync();

            return(queueClient);
        }
예제 #8
0
        public async Task AddMessageToBlobQueue(int fileAddedCount, int fileTypeId, string blobName)
        {
            try
            {
                if ((await _queueClient.ExistsAsync()) && fileAddedCount == 1)
                {
                    var message = new DTO.Response.Queue.Message
                    {
                        FileTypeId = fileTypeId,
                        BlobName   = blobName
                    };

                    var messageText = JsonConvert.SerializeObject(message);
                    _logger.LogInformation($"Adding message to blob queue", message);
                    await _queueClient.SendMessageAsync(messageText.StringToBase64());
                }
            }
            catch (RequestFailedException requestFailedException)
            {
                _logger.LogError(requestFailedException, "The queue does not exist");
                throw;
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "An error occurred while trying to add a message to the queue");
                throw;
            }
        }
예제 #9
0
 async Task<bool> ExistsAsync(QueueClient sendQueue, CancellationToken cancellationToken)
 {
     var key = sendQueue.Uri.ToString();
     return await rememberExistence.GetOrAdd(key, async keyNotFound =>
      {
          var exists = await sendQueue.ExistsAsync(cancellationToken).ConfigureAwait(false);
          return exists.Value;
      }).ConfigureAwait(false);
 }
예제 #10
0
        public async Task Queue_IfBoundToTypeAndQueueIsMissing_DoesNotCreate(string methodName)
        {
            // Act
            await CallAsync(typeof(MissingQueueProgram), methodName);

            // Assert
            QueueClient queue = queueServiceClient.GetQueueClient(OutputQueueName);

            Assert.False(await queue.ExistsAsync());
        }
        public async Task Delete(string queueName)
        {
            var queueClient = new QueueClient(Options.ConnectionString, queueName);

            if (await queueClient.ExistsAsync())
            {
                await queueClient.DeleteAsync();
            }

            WriteLine($"Queue deleted: '{queueClient.Name}'");
        }
예제 #12
0
        public async Task Queue_IfBoundToTypeAndQueueIsMissing_CreatesAndSends(string methodName, string expectedMessage)
        {
            // Act
            await CallAsync(typeof(MissingQueueProgram), methodName);

            // Assert
            QueueClient queue = queueServiceClient.GetQueueClient(OutputQueueName);

            Assert.True(await queue.ExistsAsync());
            AssertMessageSent(expectedMessage, queue);
        }
예제 #13
0
        public async Task Queue_IfBoundToCloudQueueAndQueueIsMissing_Creates()
        {
            // Act
            QueueClient result = await CallAsync <QueueClient>(typeof(BindToCloudQueueProgram), "BindToCloudQueue",
                                                               (s) => BindToCloudQueueProgram.TaskSource = s);

            // Assert
            Assert.NotNull(result);
            QueueClient queue = queueServiceClient.GetQueueClient(QueueName);

            Assert.True(await queue.ExistsAsync());
        }
예제 #14
0
        public async Task Queue_IfBoundToOutStructAndQueueIsMissing_CreatesAndSends()
        {
            // Act
            await CallAsync(typeof(MissingQueueProgram), "FuncWithOutT");

            // Assert
            QueueClient queue = queueServiceClient.GetQueueClient(OutputQueueName);

            Assert.True(await queue.ExistsAsync());
            AssertMessageSent(new StructMessage {
                Value = TestQueueMessage
            }, queue);
        }
        public async Task Publish(string queueName, T message)
        {
            var queueClient = new QueueClient(Options.ConnectionString, queueName);

            await queueClient.CreateIfNotExistsAsync();

            if (await queueClient.ExistsAsync())
            {
                queueClient.SendMessage(JsonSerializer.Serialize(message, typeof(T)));
            }

            WriteLine($"Inserted: {message}");
        }
        public static async Task PeekMessage()
        {
            QueueClient queueClient = new QueueClient(ConnectionString, "storagequeue");

            //check if queue exists
            if (await queueClient.ExistsAsync())
            {
                var peekedMessages = await queueClient.PeekMessagesAsync(Constant.Max);

                foreach (var mess in peekedMessages.Value)
                {
                    Console.WriteLine(string.Format("Messages Id: {0} peeked from Queue", mess.MessageId));
                }
            }
        }
        public static async Task GetMessages()
        {
            QueueClient queueClient = new QueueClient(ConnectionString, "storagequeue");

            //check if queue exists
            if (await queueClient.ExistsAsync())
            {
                var queueMessages = await queueClient.ReceiveMessagesAsync(Constant.Max);

                foreach (var mess in queueMessages.Value)
                {
                    Console.WriteLine(string.Format("Messages Id:{0} read from Queue", mess.MessageId));
                    await queueClient.DeleteMessageAsync(mess.MessageId, mess.PopReceipt);
                }
            }
        }
        public async Task RetrieveMessageAsync(string connectionString, string queueName)
        {
            QueueClient queueClient = new QueueClient(connectionString, queueName);
            await queueClient.CreateIfNotExistsAsync();

            if (await queueClient.ExistsAsync())
            {
                logger.LogInformation($"Queue '{queueClient.Name}' created");
            }
            else
            {
                logger.LogInformation($"Queue '{queueClient.Name}' exists");
            }
            QueueMessage[] retrievedMessage = await queueClient.ReceiveMessagesAsync();

            logger.LogInformation($"Retrieved message with id '{retrievedMessage[0].MessageId}'");
        }
예제 #19
0
        private async Task SendToQueue(AddSpeaker command, CancellationToken cancellationToken)
        {
            // Get the connection string from app settings
            string connectionString = this.configuration["Speakers:StorageAccount"];
            string queueName        = this.configuration["Speakers:CommandQueueName"];

            var serializedCommand = JsonSerializer.Serialize(command);

            var queueClient = new QueueClient(connectionString, queueName);

            if (await queueClient.ExistsAsync(cancellationToken))
            {
                await queueClient.SendMessageAsync(
                    Base64Encode(serializedCommand),
                    cancellationToken);
            }
        }
        public async Task InsertMessageAsync(string connectionString, string queueName, string message)
        {
            QueueClient queueClient = new QueueClient(connectionString, queueName);
            await queueClient.CreateIfNotExistsAsync();

            if (await queueClient.ExistsAsync())
            {
                logger.LogInformation($"Queue '{queueClient.Name}' created");
            }
            else
            {
                logger.LogInformation($"Queue '{queueClient.Name}' exists");
            }
            await queueClient.SendMessageAsync(message);

            logger.LogInformation($"Inserted: {message}");
        }
예제 #21
0
        public static async Task SendMessageToQueue()
        {
            QueueClient queue = new QueueClient(ConnectionString, "storagequeue");
            //create if Queue doesnot exists
            await queue.CreateIfNotExistsAsync();

            if (await queue.ExistsAsync())
            {
                //send message to Queue
                await queue.SendMessageAsync(string.Format("This is test message to queue at {0}", DateTime.UtcNow));

                Console.WriteLine("Message added to queue");
            }
            else
            {
                Console.WriteLine("Unable to write on queue");
            }
        }
예제 #22
0
        static async Task Main(string[] args)
        {
            QueueClient queue = new QueueClient(CONNECTION_STRING, "nowezamowienia");

            if (await queue.ExistsAsync())
            {
                QueueProperties properties = await queue.GetPropertiesAsync();

                if (properties.ApproximateMessagesCount > 0)
                {
                    QueueMessage[] messages = await queue.ReceiveMessagesAsync(1);

                    Console.WriteLine(messages[0].MessageText);

                    queue.DeleteMessage(messages[0].MessageId, messages[0].PopReceipt);
                }
            }
        }
예제 #23
0
        public async Task <bool> QueueAsync()
        {
            // <snippet_AsyncQueue>
            // Get the connection string from app settings
            string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

            // Instantiate a QueueClient which will be used to create and manipulate the queue
            QueueClient queueClient = new QueueClient(connectionString, "myqueue");

            // Create the queue if it doesn't already exist
            await queueClient.CreateIfNotExistsAsync();

            if (await queueClient.ExistsAsync())
            {
                Console.WriteLine($"Queue '{queueClient.Name}' created");
            }
            else
            {
                Console.WriteLine($"Queue '{queueClient.Name}' exists");
            }

            // Async enqueue the message
            await queueClient.SendMessageAsync("Hello, World");

            Console.WriteLine($"Message added");

            // Async receive the message
            QueueMessage[] retrievedMessage = await queueClient.ReceiveMessagesAsync();

            Console.WriteLine($"Retrieved message with content '{retrievedMessage[0].MessageText}'");

            // Async delete the message
            await queueClient.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);

            Console.WriteLine($"Deleted message: '{retrievedMessage[0].MessageText}'");

            // Async delete the queue
            await queueClient.DeleteAsync();

            Console.WriteLine($"Deleted queue: '{queueClient.Name}'");
            // </snippet_AsyncQueue>

            return(true);
        }
예제 #24
0
        private static async Task <string> RetrieveNextMessageAsync(QueueClient theQueue)
        {
            string theMessage = string.Empty;

            if (await theQueue.ExistsAsync())
            {
                QueueProperties properties = await theQueue.GetPropertiesAsync();

                if (properties.ApproximateMessagesCount > 0)
                {
                    QueueMessage[] retrievedMessage = await theQueue.ReceiveMessagesAsync(1);

                    theMessage = retrievedMessage[0].MessageText;

                    await theQueue.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
                }
            }

            return(theMessage);
        }
예제 #25
0
        public async Task DataCleanup_DeleteQueues_BadStorageConnectionString_ReturnsBadRequest(string connectionString)
        {
            // Setup
            QueueClient queueClient = await SetupTestQueue();

            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = connectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // Act
            HttpResponseMessage response = await _clientFixture.GetClient().PostAsync(_UrlDeleteQueues, content);

            // Verify
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
            bool queueExists = await queueClient.ExistsAsync();

            queueExists.Should().BeTrue();
        }
예제 #26
0
        public async Task <UserOutQueueItem> Dequeue()
        {
            if (!await _outQueueClient.ExistsAsync())
            {
                throw new InvalidOperationException("User outqueue client does not exist.");
            }

            var result = await _outQueueClient.ReceiveMessagesAsync();

            var message = result.Value.FirstOrDefault();

            if (message == null)
            {
                throw new NullReferenceException(nameof(message));
            }

            _outQueueClient.DeleteMessage(message.MessageId, message.PopReceipt);

            return(new UserOutQueueItem(true, message.MessageText));
        }
예제 #27
0
        static async Task Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .AddYamlFile("appsettings.yml")
                         .Build();

            var queueClient = new QueueClient(config.GetConnectionString("MainStorage"), config["QueName"]);

            Console.WriteLine(await queueClient.ExistsAsync());

            var responses = new List <Azure.Response <QueueMessage[]> >();

            for (var i = 0; i < 22; i++)
            {
                responses.Add(await queueClient.ReceiveMessagesAsync(32));
            }

            var messages = responses.SelectMany(r => r.Value);
            var tweets   = messages
                           .Select(m => ReadQueueMessage(m.Body))
                           .ToArray();

            var serialized = JsonSerializer.Serialize(tweets, new JsonSerializerOptions {
                WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });
            await File.WriteAllTextAsync("result.json", serialized);


            // var peekedMassges = await queueClient.PeekMessagesAsync(10);
            // Console.WriteLine(peekedMassges.Value.Length);


            // var message = ReadQueueMessage(peekedMassges.Value[0].Body);

            // var tweets = peekedMassges.Value
            //     .Select(m => ReadQueueMessage(m.Body))
            //     .ToArray();

            // var serialized = JsonSerializer.Serialize(tweets, new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
            // await File.WriteAllTextAsync("result.json", serialized);
        }
예제 #28
0
        public async Task <MessageModel> Receive()
        {
            QueueClient queue = await queueRetriever.GetQueue();

            bool exists = await queue.ExistsAsync();

            if (exists)
            {
                var message = await queue.ReceiveMessageAsync();

                if (message.Value != null)
                {
                    var result = message.Value.Body.ToObjectFromJson <MessageModel>();
                    await queue.DeleteMessageAsync(message.Value.MessageId, message.Value.PopReceipt);

                    return(result);
                }
            }

            return(null);
        }
        public async Task Subscribe(string queueName)
        {
            var queueClient = new QueueClient(Options.ConnectionString, queueName);

            if (await queueClient.ExistsAsync())
            {
                var retrievedMessage = await queueClient.ReceiveMessageAsync();

                var message = JsonSerializer.Deserialize <T>(retrievedMessage.Value.Body);

                if (message != null)
                {
                    await Handler.Invoke(message, new CancellationToken());
                }

                WriteLine($"Dequeued message: '{message.GetType()}'");

                // Delete the message
                queueClient.DeleteMessage(retrievedMessage.Value.MessageId, retrievedMessage.Value.PopReceipt);
            }
        }
예제 #30
0
파일: Program.cs 프로젝트: bibincs/AZ-204
        static async Task <string> RetrieveNextMessageAsync(QueueClient theQueue)
        {
            if (await theQueue.ExistsAsync())
            {
                QueueProperties properties = await theQueue.GetPropertiesAsync();

                if (properties.ApproximateMessagesCount > 0)
                {
                    QueueMessage[] retrievedMessage = await theQueue.ReceiveMessagesAsync(1);

                    string theMessage = retrievedMessage[0].MessageText;
                    await theQueue.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);

                    return(theMessage);
                }
                else
                {
                    Console.Write("The queue is empty. Attempt to delete it? (Y/N) ");
                    string response = Console.ReadLine();

                    if (response.ToUpper() == "Y")
                    {
                        await theQueue.DeleteIfExistsAsync();

                        return("The queue was deleted.");
                    }
                    else
                    {
                        return("The queue was not deleted.");
                    }
                }
            }
            else
            {
                return("The queue does not exist. Add a message to the command line to create the queue and store the message.");
            }
        }