Dawning.Messaging 1.0.1

dotnet add package Dawning.Messaging --version 1.0.1
                    
NuGet\Install-Package Dawning.Messaging -Version 1.0.1
                    
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="Dawning.Messaging" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dawning.Messaging" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Dawning.Messaging" />
                    
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 Dawning.Messaging --version 1.0.1
                    
#r "nuget: Dawning.Messaging, 1.0.1"
                    
#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 Dawning.Messaging@1.0.1
                    
#: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=Dawning.Messaging&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Dawning.Messaging&version=1.0.1
                    
Install as a Cake Tool

Dawning.Messaging

消息服务库,支持 RabbitMQ 和 Azure Service Bus。

安装

dotnet add package Dawning.Messaging

功能特性

  • ✅ 统一的消息发布/订阅接口
  • ✅ RabbitMQ 支持
  • ✅ Azure Service Bus 支持
  • ✅ 批量消息发送
  • ✅ 自动重连
  • ✅ JSON 序列化

快速开始

RabbitMQ

builder.Services.AddDawningRabbitMQ(options =>
{
    options.HostName = "localhost";
    options.Port = 5672;
    options.UserName = "guest";
    options.Password = "guest";
    options.PrefetchCount = 10;
});

Azure Service Bus

builder.Services.AddDawningServiceBus(
    "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=xxx;SharedAccessKey=xxx",
    options =>
    {
        options.TopicName = "my-topic";
        options.SubscriptionName = "my-subscription";
        options.MaxConcurrentCalls = 10;
    });

发布消息

public class OrderService
{
    private readonly IMessagePublisher _publisher;

    public OrderService(IMessagePublisher publisher)
    {
        _publisher = publisher;
    }

    public async Task CreateOrderAsync(Order order)
    {
        // 保存订单...
        
        // 发布事件
        await _publisher.PublishAsync(new OrderCreatedEvent
        {
            OrderId = order.Id,
            CustomerId = order.CustomerId,
            TotalAmount = order.TotalAmount,
            CreatedAt = DateTime.UtcNow
        });
    }
}

订阅消息

public class OrderEventHandler : BackgroundService
{
    private readonly IMessageSubscriber _subscriber;
    private readonly ILogger<OrderEventHandler> _logger;

    public OrderEventHandler(IMessageSubscriber subscriber, ILogger<OrderEventHandler> logger)
    {
        _subscriber = subscriber;
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await _subscriber.SubscribeAsync<OrderCreatedEvent>(
            async (message, ct) =>
            {
                _logger.LogInformation("Order {OrderId} created for customer {CustomerId}", 
                    message.OrderId, message.CustomerId);
                
                // 处理订单创建事件...
            },
            subscriptionName: "order-processor",
            cancellationToken: stoppingToken);
    }
}

配置选项

appsettings.json (RabbitMQ)

{
  "Messaging": {
    "Provider": "RabbitMQ",
    "DefaultExchange": "my-app.events",
    "RabbitMQ": {
      "HostName": "localhost",
      "Port": 5672,
      "UserName": "guest",
      "Password": "guest",
      "VirtualHost": "/",
      "ExchangeType": "topic",
      "Durable": true,
      "PrefetchCount": 10
    }
  }
}

appsettings.json (Azure Service Bus)

{
  "Messaging": {
    "Provider": "AzureServiceBus",
    "ServiceBus": {
      "ConnectionString": "Endpoint=sb://xxx.servicebus.windows.net/;...",
      "TopicName": "my-topic",
      "SubscriptionName": "my-subscription",
      "MaxConcurrentCalls": 10,
      "AutoCompleteMessages": true
    }
  }
}

API 参考

IMessagePublisher

方法 描述
PublishAsync<T>(message, routingKey?) 发布单条消息
PublishBatchAsync<T>(messages, routingKey?) 批量发布消息

IMessageSubscriber

方法 描述
SubscribeAsync<T>(handler, subscriptionName?) 订阅消息
UnsubscribeAsync(subscriptionName) 取消订阅
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 128 1/2/2026