Skip to content

Commit

Permalink
updated docs to include json serialization options (#10)
Browse files Browse the repository at this point in the history
* updated docs to include json serialization options

* updated docs and fixed unusable get message count method
  • Loading branch information
jasonshave authored Jun 19, 2022
1 parent ebaa6f5 commit 8b27fba
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ You will need to create an Azure Storage account in the Azure portal using a uni
1. [Create a storage account](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal) in your Azure portal.
2. [Obtain your connection string](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-keys-manage?tabs=azure-portal) from the Azure portal.

## Message handling behavior

- Multiple messages are pulled when `ReceiveMessagesAsync<T>` is called.
- If your handler does not throw, messages are automatically removed from the queue otherwise the message is returned to the queue for delivery again.
- Deserialization uses the `System.Text.Json` deserialization behavior. This can be overridden by specifying your own `JsonSerializerOptions` as seen below.
- You can 'peek' messages using `PeekMessages<T>` which returns a collection but doesn't remove them from the queue.

## Usage

1. Add the Nuget package `JasonShave.AzureStorage.QueueService` to your .NET project
Expand All @@ -32,16 +39,13 @@ You will need to create an Azure Storage account in the Azure portal using a uni
3. Configure the library from your `Startup.cs` or `Program.cs` file as follows:

```csharp
// get QueueClientSettings using this method
services.AddAzureStorageQueueServices(options =>
{
options.ConnectionString = configuration["QueueClientSettings:ConnectionString"];
options.QueueName = configuration["QueueClientSettings:QueueName"];
options.CreateIfNotExists = true;
});

// or use the IConfiguration binder
// get configuration from IConfiguration binder
services.AddAzureStorageQueueServices(options => configuration.Bind(nameof(QueueClientSettings), options));

// optionally customize JsonSerializerOptions
services.AddAzureStorageQueueServices(
options => hostContext.Configuration.Bind(nameof(QueueClientSettings), options),
serializationOptions => serializationOptions.AllowTrailingCommas = true);
```

4. Inject the `IQueueService` interface and use as follows:
Expand All @@ -50,7 +54,7 @@ You will need to create an Azure Storage account in the Azure portal using a uni
public class Worker : IHostedService
{
private readonly IQueueService _queueService;
private readonly IMyMessageHandler _myMessageHandler;
private readonly IMyMessageHandler _myMessageHandler; // see optional handler below

public Worker(IQueueService queueService, IMyMessageHandler myMessageHandler)
{
Expand All @@ -63,14 +67,32 @@ You will need to create an Azure Storage account in the Azure portal using a uni
while (!cancellationToken.IsCancellationRequested)
{
await _queueService.ReceiveMessagesAsync<MyMessage>(
message => _eventGridMessageHandler.HandleAsync(message),
exception => _eventGridMessageHandler.HandleAsync(exception),
message => _myMessageHandler.HandleAsync(message),
exception => _myMessageHandler.HandleExceptionAsync(exception),
cancellationToken);
}
}
}
```

5. Create your own message handler (optional)

The `ReceiveMessagesAsync<T>` method has two `HandleAsync()` methods. The first one handles the `<T>` message type you specify, and the second handles an `Exception` type. These can be implemented as follows:

```csharp
public interface IMyMessageHandler
{
Task HandleAsync(MyMessage message);
Task HandleExceptionAsync(Exception exception);
}

public class MyMessageHandler : IMyMessageHandler
{
public async Task HandleAsync(MyMessage message) => // do work
public async Task HandleExceptionAsync(Exception exception) => // handle exception
}
```

## License

This project is licensed under the MIT License - see the [LICENSE.md](license.md) file for details.
2 changes: 0 additions & 2 deletions src/AzureStorage.QueueService/Interfaces/IQueueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ public interface IQueueService
{
Task<IEnumerable<TMessage>> PeekMessages<TMessage>(int numMessages, CancellationToken cancellationToken = default);

Task<int> GetMessageCount(int numMessages, CancellationToken cancellationToken = default);

Task ReceiveMessagesAsync<TMessage>(Func<TMessage?, Task> handleMessage, Func<Exception, Task> handleException, CancellationToken cancellationToken = default)
where TMessage : class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ public async Task<IEnumerable<TMessage>> PeekMessages<TMessage>(int numMessages,
return results;
}

public async Task<int> GetMessageCount(int numMessages, CancellationToken cancellationToken = default)
{
PeekedMessage[] result = await _queueClient.PeekMessagesAsync(numMessages, cancellationToken);
return result.Count();
}

public async Task ReceiveMessagesAsync<TMessage>(Func<TMessage?, Task> handleMessage, Func<Exception, Task> handleException, CancellationToken cancellationToken = default)
where TMessage : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ public async Task Peek_Messages_Returns_Collection()

// act
var messages = await subject.PeekMessages<TestObject>(It.IsAny<int>());
var numMessages = await subject.GetMessageCount(It.IsAny<int>());


// assert
messages.Should().NotBeEmpty();
messages.Count().Should().Be(1);
numMessages.Should().Be(1);
messages.Count().Should().Be(1);
}

[Fact(DisplayName = "Peek messages returns message collection")]
Expand Down

0 comments on commit 8b27fba

Please sign in to comment.