ServiceableBus
Simple library to create Senders and Listeners to AzureServiceBus Queues and Topics. More advanced features to follow
Install / Use
/learn @gthompson90/ServiceableBusREADME
ServiceableBus
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.
Prerequisites
- .NET 9
- Azure Service Bus namespace and connection string
Installation
- 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
Configuration
- 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;
}
}
Usage
- 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();
ServiceablePropertyBag
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.
Example Usage
- 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);
Additional Information
- 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.
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Contact
For any questions or issues, please open an issue on the GitHub repository.
Related Skills
node-connect
348.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
