Skip to content

Commit 3670b71

Browse files
committed
Refactor and migrate to RabbitMQ with MassTransit
Modernized codebase by adopting C# 9.0 features, including primary constructors for `DemoCommand` and `Product` classes. Transitioned from Azure Service Bus to RabbitMQ with MassTransit, removing Azure Service Bus dependencies and related configurations. Updated `Program.cs` to streamline service registration, added `Serilog` initialization, and reorganized middleware. Enhanced `appsettings.json` and `launchSettings.json` with updated logging, Swagger metadata, and environment profiles. Refactored `TokenProvider`, `MongoDictionarySerializer`, and `ExternalServiceClient` for improved readability and null safety. Removed unused controllers and Azure Service Bus extensions. Updated project files to conditionally include references based on build configuration. These changes improve maintainability, align with best practices, and prepare the codebase for future scalability.
1 parent cee7765 commit 3670b71

File tree

19 files changed

+129
-287
lines changed

19 files changed

+129
-287
lines changed

src/demo/Genocs.Core.Demo.Contracts/DemoCommand.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Genocs.Core.Demo.Contracts;
44

5-
public class DemoCommand : ICommand
5+
public class DemoCommand(string payload) : ICommand
66
{
7-
public string Payload { get; }
8-
9-
public DemoCommand(string payload) => Payload = payload;
7+
public string Payload { get; } = payload;
108
}

src/demo/Genocs.Core.Demo.Contracts/OrderRequestEvent.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@ public class OrderRequest : IEvent
1313
public List<Product> Basket { get; set; } = default!;
1414
}
1515

16-
public class Product
16+
public class Product(string sKU, int count, decimal price)
1717
{
18-
public string SKU { get; private set; }
19-
public int Count { get; private set; }
20-
public decimal Price { get; private set; }
21-
22-
public Product(string sKU, int count, decimal price)
23-
{
24-
SKU = sKU;
25-
Count = count;
26-
Price = price;
27-
}
18+
public string SKU { get; private set; } = sKU;
19+
public int Count { get; private set; } = count;
20+
public decimal Price { get; private set; } = price;
2821
}

src/demo/Genocs.Core.Demo.HelloWorld/Genocs.Core.Demo.HelloWorld.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<UserSecretsId>genocs</UserSecretsId>
8+
<IsPublishable>false</IsPublishable>
9+
<IsPackable>false</IsPackable>
810
</PropertyGroup>
911

1012
<ItemGroup>
1113
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
1214
</ItemGroup>
1315

14-
<ItemGroup>
16+
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
1517
<ProjectReference Include="..\..\Genocs.Auth\Genocs.Auth.csproj" />
1618
<ProjectReference Include="..\..\Genocs.Logging\Genocs.Logging.csproj" />
1719
<ProjectReference Include="..\..\Genocs.OpenTelemetry\Genocs.OpenTelemetry.csproj" />
1820
<ProjectReference Include="..\..\Genocs.WebApi.Swagger\Genocs.WebApi.Swagger.csproj" />
1921
</ItemGroup>
2022

23+
<ItemGroup Condition="'$(Configuration)' != 'Debug'">
24+
<PackageReference Include="Genocs.Auth" Version="7.3.*" />
25+
<PackageReference Include="Genocs.Logging" Version="7.3.*" />
26+
<PackageReference Include="Genocs.OpenTelemetry" Version="7.3.*" />
27+
<PackageReference Include="Genocs.WebApi.Swagger" Version="7.3.*" />
28+
</ItemGroup>
29+
2130
<ItemGroup>
2231
<ProjectReference Include="..\Genocs.Core.Demo.Infrastructure\Genocs.Core.Demo.Infrastructure.csproj" />
2332
</ItemGroup>

src/demo/Genocs.Core.Demo.HelloWorld/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Genocs.WebApi.Swagger;
88
using Genocs.WebApi.Swagger.Docs;
99
using Microsoft.AspNetCore.Authorization.Infrastructure;
10+
using Serilog;
1011

1112
StaticLogger.EnsureInitialized();
1213

@@ -15,9 +16,10 @@
1516
builder.Host
1617
.UseLogging();
1718

18-
builder.AddGenocs()
19-
.AddOpenTelemetry()
19+
builder
20+
.AddGenocs()
2021
.AddJwt("simmetric_jwt")
22+
.AddOpenTelemetry()
2123
.AddWebApi()
2224
.AddSwaggerDocs()
2325
.AddWebApiSwaggerDocs()
@@ -92,3 +94,5 @@
9294
app.MapGet("/onlyreader2", () => "ok").RequireAuthorization("Reader2");
9395

9496
app.Run();
97+
98+
Log.CloseAndFlush();

src/demo/Genocs.Core.Demo.HelloWorld/appsettings.Development.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/demo/Genocs.Core.Demo.HelloWorld/appsettings.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"app": {
3-
"name": "Demo HelloWorld",
4-
"service": "api-helloworld",
5-
"instance": "01",
3+
"name": "Hello World WebApi",
4+
"service": "helloworld_webapi",
5+
"instance": "1",
66
"version": "v1.0",
77
"displayBanner": true,
88
"displayVersion": true
@@ -65,17 +65,11 @@
6565
"connectionString": "InstrumentationKey=1496274b-bda7-4ac6-88ab-9f73b4d3c7b8;IngestionEndpoint=https://italynorth-0.in.applicationinsights.azure.com/;LiveEndpoint=https://italynorth.livediagnostics.monitor.azure.com/;ApplicationId=c417f66d-3611-48a2-80fe-5a6d302bed4f"
6666
}
6767
},
68-
"Logging": {
69-
"LogLevel": {
70-
"Default": "Debug",
71-
"Microsoft.AspNetCore": "Debug"
72-
}
73-
},
7468
"swagger": {
7569
"enabled": true,
7670
"reDocEnabled": true,
77-
"name": "Fiscanner",
78-
"title": "Fiscanner API",
71+
"name": "HelloWorldWebApi",
72+
"title": "Hello World WebAPI",
7973
"version": "v2",
8074
"description": "The Fiscanner API provides comprehensive financial scanning and document processing capabilities. This API documentation is fully compliant with OpenAPI 3.0 standards and can be seamlessly integrated with LLM orchestrators such as LangChain, Semantic Kernel, and AutoGen. The documentation is also optimized for use with Microsoft Power Automate and Azure Logic Apps custom connectors, enabling easy workflow automation and integration scenarios.",
8175
"routePrefix": "swagger",

src/demo/Genocs.Core.Demo.Infrastructure/SecurityAuthentication/TokenProvider.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace Genocs.Core.Demo.Infrastructure.SecurityAuthentication;
1414
/// </summary>
1515
public sealed class TokenProvider(IConfiguration configuration)
1616
{
17-
public string Issuer => configuration["JwtSettings:Issuer"];
18-
public string Audience => configuration["JwtSettings:Audience"];
19-
public string Key => configuration["JwtSettings:Key"];
20-
public int Expiration => int.Parse(configuration["JwtSettings:Expiration"]);
17+
public string? Issuer => configuration["JwtSettings:Issuer"];
18+
public string? Audience => configuration["JwtSettings:Audience"];
19+
public string? Key => configuration["JwtSettings:Key"];
20+
public int Expiration => int.Parse(configuration["JwtSettings:Expiration"] ?? "0");
2121

2222
/// <summary>
2323
/// This method creates a token using the JwtSecurityTokenHandler class.
@@ -32,8 +32,8 @@ public string CreateToken()
3232
{
3333
Subject = new ClaimsIdentity(new Claim[]
3434
{
35-
new Claim(ClaimTypes.Name, "username"),
36-
new Claim(ClaimTypes.Role, "role")
35+
new(ClaimTypes.Name, "username"),
36+
new(ClaimTypes.Role, "role")
3737
}),
3838
Expires = DateTime.UtcNow.AddMinutes(Expiration),
3939
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
@@ -55,8 +55,8 @@ public string CreateTokenWithJsonWebTokenHandler()
5555
{
5656
Subject = new ClaimsIdentity(new Claim[]
5757
{
58-
new Claim(ClaimTypes.Name, "username"),
59-
new Claim(ClaimTypes.Role, "role")
58+
new(ClaimTypes.Name, "username"),
59+
new(ClaimTypes.Role, "role")
6060
}),
6161
Expires = DateTime.UtcNow.AddMinutes(Expiration),
6262
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)

src/demo/Genocs.Core.Demo.WebApi/Controllers/FileUploadController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public class FileUploadController : ControllerBase
99

1010
/// <summary>
1111
/// This is an example of file Upload using API
12-
/// Use it as a reference
12+
/// Use it as a reference.
1313
/// </summary>
1414
/// <param name="files"></param>
1515
/// <param name="fileTag"></param>
1616
/// <returns></returns>
17-
[Route(""), HttpPost]
17+
[Route("")]
18+
[HttpPost]
1819
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
1920
[ProducesResponseType(StatusCodes.Status400BadRequest)]
2021
public async Task<IActionResult> PostUploadAndEvaluate([FromForm(Name = "docs")] List<IFormFile> files, [FromQuery] string fileTag)

src/demo/Genocs.Core.Demo.WebApi/Controllers/ServiceBusController.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/demo/Genocs.Core.Demo.WebApi/Controllers/ServiceBusMassTransitController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ await _publishEndpoint.Publish<OrderSubmitted>(new
5656
return Ok("Sent");
5757
}
5858

59-
6059
//[HttpPost("SubmitOrder")]
6160
//public async Task<IActionResult> PostSubmitOrder()
6261
//{

0 commit comments

Comments
 (0)