Skip to content

Commit

Permalink
feat: dotnet 9 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 authored Nov 13, 2024
2 parents 18f4583 + 804679d commit 1e6c51f
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Install .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x.x'
dotnet-version: '9.x.x'

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependency-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.x.x
9.x.x
- name: Restore dependencies
run: dotnet restore
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.x.x
9.x.x
- name: Restore dependencies
run: dotnet restore
Expand Down
18 changes: 9 additions & 9 deletions CleanArchitecture.Api/CleanArchitecture.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<UserSecretsId>64377c40-44d6-4989-9662-5d778f8b3b92</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
Expand All @@ -14,17 +14,17 @@
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.66.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.9.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MockQueryable.NSubstitute" Version="7.0.3" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand All @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down
8 changes: 4 additions & 4 deletions CleanArchitecture.Domain/CleanArchitecture.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.2" />
<PackageReference Include="RabbitMQ.Client" Version="7.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static IRuleBuilderOptions<T, string> StringMustBeBase64<T>(this IRuleBui
private static bool IsBase64String(string base64)
{
base64 = base64.Trim();
return base64.Length % 4 == 0 && Base64Regex().IsMatch(base64);
return base64.Length % 4 == 0 && new Regex("^[a-zA-Z0-9\\+/]*={0,3}$").IsMatch(base64);
}

public static IRuleBuilder<T, string> Password<T>(
Expand All @@ -32,7 +32,4 @@ public static IRuleBuilder<T, string> Password<T>(
.Matches("[^a-zA-Z0-9]").WithErrorCode(DomainErrorCodes.User.SpecialCharPassword);
return options;
}

[GeneratedRegex("^[a-zA-Z0-9\\+/]*={0,3}$", RegexOptions.None)]
private static partial Regex Base64Regex();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace CleanArchitecture.Domain.Rabbitmq.Actions;
Expand All @@ -15,8 +16,8 @@ public BindQueueToExchange(string queueName, string exchangeName, string routing
_queueName = queueName;
}

public void Perform(IModel channel)
public async Task Perform(IChannel channel)
{
channel.QueueBind(_queueName, _exchangeName, _routingKey);
await channel.QueueBindAsync(_queueName, _exchangeName, _routingKey);
}
}
5 changes: 3 additions & 2 deletions CleanArchitecture.Domain/Rabbitmq/Actions/CreateExchange.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace CleanArchitecture.Domain.Rabbitmq.Actions;
Expand All @@ -13,8 +14,8 @@ public CreateExchange(string name, string type)
_type = type;
}

public void Perform(IModel channel)
public async Task Perform(IChannel channel)
{
channel.ExchangeDeclare(_name, _type);
await channel.ExchangeDeclareAsync(_name, _type);
}
}
5 changes: 3 additions & 2 deletions CleanArchitecture.Domain/Rabbitmq/Actions/CreateQueue.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace CleanArchitecture.Domain.Rabbitmq.Actions;
Expand All @@ -11,9 +12,9 @@ public CreateQueue(string queueName)
QueueName = queueName;
}

public void Perform(IModel channel)
public async Task Perform(IChannel channel)
{
channel.QueueDeclare(
await channel.QueueDeclareAsync(
QueueName,
false,
false,
Expand Down
3 changes: 2 additions & 1 deletion CleanArchitecture.Domain/Rabbitmq/Actions/IRabbitMqAction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace CleanArchitecture.Domain.Rabbitmq.Actions;

public interface IRabbitMqAction
{
void Perform(IModel channel);
Task Perform(IChannel channel);
}
9 changes: 5 additions & 4 deletions CleanArchitecture.Domain/Rabbitmq/Actions/RegisterConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace CleanArchitecture.Domain.Rabbitmq.Actions;

public sealed class RegisterConsumer : IRabbitMqAction
{
private readonly Action<string, string, string, ConsumeEventHandler> _addConsumer;
private readonly Func<string, string, string, ConsumeEventHandler, Task> _addConsumer;
private readonly ConsumeEventHandler _consumer;
private readonly string _exchange;
private readonly string _queue;
Expand All @@ -16,7 +17,7 @@ public RegisterConsumer(
string queue,
string routingKey,
ConsumeEventHandler consumer,
Action<string, string, string, ConsumeEventHandler> addConsumer)
Func<string, string, string, ConsumeEventHandler, Task> addConsumer)
{
_exchange = exchange;
_queue = queue;
Expand All @@ -25,8 +26,8 @@ public RegisterConsumer(
_addConsumer = addConsumer;
}

public void Perform(IModel channel)
public async Task Perform(IChannel channel)
{
_addConsumer(_exchange, _queue, _routingKey, _consumer);
await _addConsumer(_exchange, _queue, _routingKey, _consumer);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace CleanArchitecture.Domain.Rabbitmq.Actions;
Expand All @@ -11,8 +12,8 @@ public SendAcknowledgement(ulong deliveryTag)
DeliveryTag = deliveryTag;
}

public void Perform(IModel channel)
public async Task Perform(IChannel channel)
{
channel.BasicAck(DeliveryTag, false);
await channel.BasicAckAsync(DeliveryTag, false);
}
}
6 changes: 3 additions & 3 deletions CleanArchitecture.Domain/Rabbitmq/Actions/SendMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RabbitMQ.Client;

Expand All @@ -23,16 +24,15 @@ public SendMessage(string routingKey, string exchange, object message)
_message = message;
}

public void Perform(IModel channel)
public async Task Perform(IChannel channel)
{
var json = JsonConvert.SerializeObject(_message, s_serializerSettings);

var content = Encoding.UTF8.GetBytes(json);

channel.BasicPublish(
await channel.BasicPublishAsync(
_exchange,
_routingKey,
null,
content);
}
}
43 changes: 26 additions & 17 deletions CleanArchitecture.Domain/Rabbitmq/RabbitMqHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CleanArchitecture.Domain.Rabbitmq;

public sealed class RabbitMqHandler : BackgroundService
{
private readonly IModel? _channel;
private IChannel? _channel;
private readonly RabbitMqConfiguration _configuration;

private readonly ConcurrentDictionary<string, List<ConsumeEventHandler>> _consumers = new();
Expand All @@ -28,27 +28,30 @@ public RabbitMqHandler(
{
_configuration = configuration;
_logger = logger;
}

if (!configuration.Enabled)
public override async Task StartAsync(CancellationToken cancellationToken)
{
if (!_configuration.Enabled)
{
logger.LogInformation("RabbitMQ is disabled. Connection will not be established");
_logger.LogInformation("RabbitMQ is disabled. Connection will not be established");
return;
}

var factory = new ConnectionFactory
{
AutomaticRecoveryEnabled = true,
HostName = configuration.Host,
Port = configuration.Port,
UserName = configuration.Username,
Password = configuration.Password,
DispatchConsumersAsync = true
HostName = _configuration.Host,
Port = _configuration.Port,
UserName = _configuration.Username,
Password = _configuration.Password,
};

var connection = factory.CreateConnection();
_channel = connection.CreateModel();
var connection = await factory.CreateConnectionAsync(cancellationToken);
_channel = await connection.CreateChannelAsync(null, cancellationToken);
}


public void InitializeExchange(string exchangeName, string type = ExchangeType.Fanout)
{
if (!_configuration.Enabled)
Expand Down Expand Up @@ -126,19 +129,25 @@ public void AddExchangeConsumer(string exchange, string queue, ConsumeEventHandl
AddExchangeConsumer(exchange, string.Empty, queue, consumer);
}

private void AddEventConsumer(string exchange, string queueName, string routingKey, ConsumeEventHandler consumer)
private async Task AddEventConsumer(string exchange, string queueName, string routingKey, ConsumeEventHandler consumer)
{
if (!_configuration.Enabled)
{
_logger.LogInformation("RabbitMQ is disabled. Event consumer will not be added.");
return;
}

var key = $"{exchange}-{routingKey}";

if (!_consumers.TryGetValue(key, out var consumers))
{
consumers = new List<ConsumeEventHandler>();
_consumers.TryAdd(key, consumers);

var eventHandler = new AsyncEventingBasicConsumer(_channel);
eventHandler.Received += CallEventConsumersAsync;
var eventHandler = new AsyncEventingBasicConsumer(_channel!);
eventHandler.ReceivedAsync += CallEventConsumersAsync;

_channel!.BasicConsume(queueName, false, eventHandler);
await _channel!.BasicConsumeAsync(queueName, false, eventHandler);
}

consumers.Add(consumer);
Expand Down Expand Up @@ -202,19 +211,19 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

while (true)
{
HandleEnqueuedActions();
await HandleEnqueuedActions();

await Task.Delay(1000, stoppingToken);
}
}

private void HandleEnqueuedActions()
private async Task HandleEnqueuedActions()
{
while (_pendingActions.TryDequeue(out var action))
{
try
{
action.Perform(_channel!);
await action.Perform(_channel!);
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 1e6c51f

Please sign in to comment.