ServiceableBus.Contracts 1.3.40

dotnet add package ServiceableBus.Contracts --version 1.3.40
                    
NuGet\Install-Package ServiceableBus.Contracts -Version 1.3.40
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ServiceableBus.Contracts" Version="1.3.40" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ServiceableBus.Contracts" Version="1.3.40" />
                    
Directory.Packages.props
<PackageReference Include="ServiceableBus.Contracts" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ServiceableBus.Contracts --version 1.3.40
                    
#r "nuget: ServiceableBus.Contracts, 1.3.40"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ServiceableBus.Contracts@1.3.40
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ServiceableBus.Contracts&version=1.3.40
                    
Install as a Cake Addin
#tool nuget:?package=ServiceableBus.Contracts&version=1.3.40
                    
Install as a Cake Tool

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

  1. Add the ServiceableBus package 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
  1. Ensure you have the necessary dependencies in your project:
dotnet add package Azure.Messaging.ServiceBus
dotnet add package Microsoft.Extensions.DependencyInjection

Configuration

  1. Add the necessary configuration settings to your appsettings.json file:
{
  "ServiceableBus": {
    "ConnectionString": "YourAzureServiceBusConnectionString"
  }
}
  1. Create your event class implementing IServiceableBusEvent:
public class TestEvent : IServiceableBusEvent
{
    public const string Topic = "test-topic";
    public string Property { get; set; }
}
  1. 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

  1. In your Program.cs file, configure the ServiceableBus:
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();
  1. Run your application:
dotnet run
  1. 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

  1. Define a ServiceablePropertyBag with properties:
var propertyBag = new ServiceablePropertyBag { Properties =[("Property1", "Value1"), ("Property2", 123), ("Property3", true)]};
  1. Use the ServiceablePropertyBag when 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.json file.
  • 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.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ServiceableBus.Contracts:

Package Downloads
ServiceableBus.Azure

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.40 177 2/14/2025
1.2.38 155 2/14/2025
1.1.32 171 2/13/2025
1.0.23 168 2/13/2025
1.0.17 162 2/13/2025
1.0.12 178 2/13/2025
1.0.11 169 2/13/2025
1.0.10 167 2/13/2025
1.0.9 177 2/13/2025
1.0.8 165 2/13/2025
1.0.7 163 2/13/2025
1.0.6 167 2/13/2025
1.0.5 159 2/13/2025
1.0.4 166 2/13/2025
1.0.3 165 2/13/2025
1.0.2 167 2/13/2025
1.0.1 165 2/13/2025