零度框架升级到 Aspire 13.0 版本

This commit is contained in:
administrator committed 2025-12-16 22:26:14 +08:00
1 parent 41f6c147d5
commit b11ba4f6a5
50 files changed
+336 -446

No files matched your search

@@ -12,12 +12,12 @@ namespace HelloShop.DistributedLock.Dapr
{
expiryInSeconds = expiryInSeconds == default ? 60 : expiryInSeconds;
// The CallerMemberNameAttribute is used to get the name of the calling method.
string? lockOwner = new StackTrace().GetFrame(1)?.GetMethod()?.DeclaringType?.Name;
lockOwner ??= Guid.NewGuid().ToString();
#pragma warning disable CS0618
#pragma warning disable DAPR_DISTRIBUTEDLOCK // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
TryLockResponse response = await daprClient.Lock("lockstore", resourceId, lockOwner, expiryInSeconds, cancellationToken);
#pragma warning restore CS0618
#pragma warning restore DAPR_DISTRIBUTEDLOCK // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
return new DaprDistributedLockResult(response);
}
@@ -5,9 +5,9 @@ using Dapr.Client;
namespace HelloShop.DistributedLock.Dapr
{
#pragma warning disable CS0618
#pragma warning disable DAPR_DISTRIBUTEDLOCK // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
public class DaprDistributedLockResult(TryLockResponse tryLockResponse) : IDistributedLockResult
#pragma warning restore CS0618
#pragma warning restore DAPR_DISTRIBUTEDLOCK // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
{
public async ValueTask DisposeAsync()
{
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -25,26 +25,26 @@ namespace HelloShop.EventBus.RabbitMQ
private readonly EventBusOptions _eventBusOptions = eventBusOptions.Value;
private IConnection? _rabbitMQConnection;
private IModel? _consumerChannel;
private IChannel? _consumerChannel;
public Task StartAsync(CancellationToken cancellationToken)
{
Task.Factory.StartNew(() =>
Task.Factory.StartNew(async () =>
{
try
{
_rabbitMQConnection = serviceProvider.GetRequiredService<IConnection>();
_consumerChannel = _rabbitMQConnection.CreateModel();
_consumerChannel.ExchangeDeclare(exchange: _exchangeName, type: ExchangeType.Direct);
_consumerChannel.QueueDeclare(queue: _queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
_consumerChannel = await _rabbitMQConnection.CreateChannelAsync();
await _consumerChannel.ExchangeDeclareAsync(exchange: _exchangeName, type: ExchangeType.Direct);
await _consumerChannel.QueueDeclareAsync(queue: _queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
var consumer = new AsyncEventingBasicConsumer(_consumerChannel);
consumer.Received += OnMessageReceivedAsync;
_consumerChannel.BasicConsume(queue: _queueName, autoAck: false, consumer: consumer);
consumer.ReceivedAsync += OnMessageReceivedAsync;
await _consumerChannel.BasicConsumeAsync(queue: _queueName, autoAck: false, consumer: consumer);
foreach (var (eventName, _) in _eventBusOptions.EventTypes)
{
_consumerChannel.QueueBind(queue: _queueName, exchange: _exchangeName, routingKey: eventName);
await _consumerChannel.QueueBindAsync(queue: _queueName, exchange: _exchangeName, routingKey: eventName);
}
}
catch (Exception ex)
@@ -58,25 +58,36 @@ namespace HelloShop.EventBus.RabbitMQ
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public void Dispose() => _consumerChannel?.Dispose();
public async void Dispose()
{
if (_consumerChannel is not null)
{
await _consumerChannel.CloseAsync();
await _consumerChannel.DisposeAsync();
}
}
public Task PublishAsync(DistributedEvent @event, CancellationToken cancellationToken = default)
public async Task PublishAsync(DistributedEvent @event, CancellationToken cancellationToken = default)
{
string routingKey = @event.GetType().Name;
using var channel = _rabbitMQConnection?.CreateModel() ?? throw new InvalidOperationException("RabbitMQ connection is not available.");
var channel = _rabbitMQConnection is not null ? await _rabbitMQConnection.CreateChannelAsync(cancellationToken: cancellationToken) : throw new InvalidOperationException("RabbitMQ connection is not available.");
channel.ExchangeDeclare(exchange: _exchangeName, type: ExchangeType.Direct);
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), _eventBusOptions.JsonSerializerOptions);
return _pipeline.Execute(() =>
await using (channel)
{
IBasicProperties properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
channel.BasicPublish(exchange: _exchangeName, routingKey: routingKey, mandatory: true, basicProperties: properties, body: body);
return Task.CompletedTask;
});
await channel.ExchangeDeclareAsync(exchange: _exchangeName, type: ExchangeType.Direct, cancellationToken: cancellationToken);
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), _eventBusOptions.JsonSerializerOptions);
await _pipeline.ExecuteAsync(async (ct) =>
{
var properties = new BasicProperties
{
DeliveryMode = DeliveryModes.Persistent
};
await channel.BasicPublishAsync(exchange: _exchangeName, routingKey: routingKey, mandatory: true, basicProperties: properties, body: body, cancellationToken: ct);
}, cancellationToken);
}
}
private async Task OnMessageReceivedAsync(object sender, BasicDeliverEventArgs eventArgs)
@@ -101,7 +112,10 @@ namespace HelloShop.EventBus.RabbitMQ
}
}
_consumerChannel?.BasicAck(eventArgs.DeliveryTag, multiple: false);
if (_consumerChannel is not null)
{
await _consumerChannel.BasicAckAsync(eventArgs.DeliveryTag, multiple: false);
}
}
private static ResiliencePipeline CreateResiliencePipeline(int retryCount)
@@ -17,7 +17,7 @@ namespace HelloShop.EventBus.RabbitMQ
builder.AddRabbitMQClient(connectionName, configureConnectionFactory: factory =>
{
factory.DispatchConsumersAsync = true;
factory.ConsumerDispatchConcurrency = 5;
});
builder.Services.Configure<EventBusOptions>(builder.Configuration.GetSection(sectionName));