ServiceableBus is a .NET library for handling Azure Service Bus messages with ease. This guide will help you set up and use the ServiceableBus package in your project.
- .NET 9
- Azure Service Bus namespace and connection string
- Add the
ServiceableBuspackage to your project. You can do this via the NuGet Package Manager or by running the following command in the terminal:
dotnet add package ServiceableBus
- Ensure you have the necessary dependencies in your project:
dotnet add package Azure.Messaging.ServiceBus
dotnet add package Microsoft.Extensions.DependencyInjection
- Add the necessary configuration settings to your
appsettings.jsonfile:
{
"ServiceableBus": {
"ConnectionString": "YourAzureServiceBusConnectionString"
}
}
- Create your event class implementing
IServiceableBusEvent:
public class TestEvent : IServiceableBusEvent
{
public const string Topic = "test-topic";
public string Property { get; set; }
}
- Create your event handler class implementing
IServiceableBusEventHandler<T>:
public class TestEventServiceBusHandler : IServiceableBusEventHandler<TestEvent>
{
public Task Handle(TestEvent @event)
{
Console.WriteLine($"Handling event: {@event.Property}");
return Task.CompletedTask;
}
}
- In your
Program.csfile, configure theServiceableBus:
using ServiceableBus.Extensions;
using ServiceableBus.Sample.Api;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
// Adding options is required for the ServiceableBus configuration.
builder.Services.AddOptions();
//Add your Listeners and Handlers here BEFORE adding the ServiceableBus.
builder.AddServiceableBusQueueListener(() =>
new ServiceableQueueListenerOptions<TestEvent>()
{
QueueName = TestEvent.Queue
});
builder.AddServiceableBusTopicListener(() =>
new ServiceableTopicListenerOptions<TestTopicEvent>()
{
SubscriptionName = appTopicSubscriptionName,
TopicName = TestTopicEvent.Topic
});
builder.RegisterServiceableBusHandler<TestEvent, TestEventServiceBusHandler>();
builder.RegisterServiceableBusHandler<TestTopicEvent, TestTopicEventServiceBusHandler>();
builder.AddServiceableBus();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.Run();
- Run your application:
dotnet run
- Adding a Sender for TestEvent and sending a message:
using ServiceableBus.Extensions;
using ServiceableBus.Sample.Api;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
// Adding options is required for the ServiceableBus configuration.
builder.Services.AddOptions();
// Add your Listeners, Handlers and Senders here BEFORE adding the ServiceableBus.
builder.AddServiceableBusQueueListener(() =>
new ServiceableQueueListenerOptions<TestEvent>()
{
QueueName = TestEvent.Queue
});
builder.RegisterServiceableBusHandler<TestEvent, TestEventServiceBusHandler>();
builder.AddServiceableBusEventSender<TestEvent>(TestEvent.Topic);
builder.AddServiceableBus();
var app = builder.Build();
//Test fire the sender by getting the IServiceableBusPublisher and calling PublishAsync.
var sender = app.Services.GetService<IServiceableBusPublisher>();
await sender.PublishAsync(new TestEvent
{
MessageTypeName = "TestEvent",
Payload = new TestEvent.TestEventPayload("Test", 1, 4)
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.Run();
The ServiceablePropertyBag class is used to store and manage a collection of key-value pairs. It provides a convenient way to pass additional properties along with your events.
- Define a
ServiceablePropertyBagwith properties:
var propertyBag = new ServiceablePropertyBag { Properties =[("Property1", "Value1"), ("Property2", 123), ("Property3", true)]};
- Use the
ServiceablePropertyBagwhen publishing an event:
await sender.PublishAsync(new TestEvent
{
MessageTypeName = "TestEvent",
Payload = new TestEvent.TestEventPayload("Test", 1, 4)
},
() => propertyBag);
- Ensure that your Azure Service Bus connection string and other settings are correctly configured in the
appsettings.jsonfile. - You can add multiple listeners and handlers as needed by following the pattern shown above.
This project is licensed under the MIT License.
Contributions are welcome! Please open an issue or submit a pull request.
For any questions or issues, please open an issue on the GitHub repository.