Skip to content

Commit f58038c

Browse files
committed
Refactor to minimal hosting model
Migrated from the traditional `Startup.cs` and `Program.cs` structure to the .NET 6+ minimal hosting model: - Consolidated service registration and middleware configuration into `Program.cs`. - Removed `Startup.cs` files across all integrations (Amazon SQS, Kafka, Pulsar, RabbitMQ with MongoDB, RabbitMQ with SQL Server). - Added `WebApplication` and `WebApplicationBuilder` for streamlined configuration. - Updated namespaces and constants for better alignment with the new structure. - Included Docker setup commands as comments for dependencies like Kafka, PostgreSQL, and SQL Server. - Simplified project structure, improving readability and reducing boilerplate code.
1 parent 3e0dee4 commit f58038c

File tree

12 files changed

+184
-322
lines changed

12 files changed

+184
-322
lines changed
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
using Microsoft.AspNetCore.Hosting;
2-
using Microsoft.Extensions.Hosting;
1+
using Amazon;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.DependencyInjection;
34

4-
namespace Sample.AmazonSQS.InMemory
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Configure services
8+
builder.Services.AddCap(x =>
59
{
6-
public class Program
7-
{
8-
public static void Main(string[] args)
9-
{
10-
CreateHostBuilder(args).Build().Run();
11-
}
10+
x.UseInMemoryStorage();
11+
x.UseAmazonSQS(RegionEndpoint.CNNorthWest1);
12+
x.UseDashboard();
13+
});
14+
15+
builder.Services.AddControllers();
16+
17+
var app = builder.Build();
18+
19+
// Configure middleware pipeline
20+
app.UseRouting();
21+
app.MapControllers();
1222

13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
14-
Host.CreateDefaultBuilder(args)
15-
.ConfigureWebHostDefaults(webBuilder =>
16-
{
17-
webBuilder.UseStartup<Startup>();
18-
});
19-
}
20-
}
23+
app.Run();

samples/Sample.AmazonSQS.InMemory/Startup.cs

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

samples/Sample.Kafka.PostgreSql/AppDbContext.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using System.Threading.Tasks;
1+
using System.Data.Common;
22
using System.Threading;
3+
using System.Threading.Tasks;
4+
using DotNetCore.CAP;
35
using Microsoft.EntityFrameworkCore;
46
using Microsoft.EntityFrameworkCore.Infrastructure;
57
using Microsoft.EntityFrameworkCore.Storage;
6-
using DotNetCore.CAP;
78
using Microsoft.Extensions.DependencyInjection;
89
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
9-
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
10-
using System.Data.Common;
1110

1211
namespace Sample.Kafka.PostgreSql
1312
{

samples/Sample.Kafka.PostgreSql/Controllers/ValuesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task<IActionResult> WithoutTransaction()
4747
[Route("~/adonet/transaction")]
4848
public IActionResult AdonetWithTransaction()
4949
{
50-
using (var connection = new NpgsqlConnection(Startup.DbConnectionString))
50+
using (var connection = new NpgsqlConnection(AppConstants.DbConnectionString))
5151
{
5252
using (var transaction = connection.BeginTransaction(producer, autoCommit: false))
5353
{
Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
1-
using Microsoft.AspNetCore.Hosting;
2-
using Microsoft.Extensions.Hosting;
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Storage;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Sample.Kafka.PostgreSql;
36

4-
namespace Sample.Kafka.PostgreSql
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Configure services
10+
builder.Services.AddDbContext<AppDbContext>((sp, opt) =>
11+
{
12+
opt.UseNpgsql(AppConstants.DbConnectionString)
13+
.ReplaceService<IRelationalConnection, CapNpgsqlRelationalConnection>();
14+
});
15+
16+
builder.Services.AddCap(x =>
17+
{
18+
//x.UseEntityFramework<AppDbContext>();
19+
//docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres
20+
x.UsePostgreSql(AppConstants.DbConnectionString);
21+
22+
/* //Run Kafka Docker Container (Powershell)
23+
docker run -d `
24+
--name kafka `
25+
-p 9092:9092 `
26+
-e KAFKA_NODE_ID=1 `
27+
-e KAFKA_PROCESS_ROLES=broker,controller `
28+
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092,CONTROLLER://:9093 `
29+
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092 `
30+
-e KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER `
31+
-e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT `
32+
-e KAFKA_CONTROLLER_QUORUM_VOTERS=1@localhost:9093 `
33+
-e KAFKA_LOG_DIRS=/var/lib/kafka/data `
34+
-e KAFKA_AUTO_CREATE_TOPICS_ENABLE=true `
35+
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 `
36+
-e KAFKA_OFFSETS_TOPIC_MIN_ISR=1 `
37+
-e KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1 `
38+
-e KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=1 `
39+
apache/kafka:3.7.0
40+
*/
41+
x.UseKafka("127.0.0.1:9092");
42+
x.UseDashboard();
43+
});
44+
45+
builder.Services.AddControllers();
46+
47+
var app = builder.Build();
48+
49+
// Configure middleware pipeline
50+
app.UseRouting();
51+
app.MapControllers();
52+
53+
app.Run();
54+
55+
public static class AppConstants
556
{
6-
public class Program
7-
{
8-
public static void Main(string[] args)
9-
{
10-
CreateHostBuilder(args).Build().Run();
11-
}
12-
13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
14-
Host.CreateDefaultBuilder(args)
15-
.ConfigureWebHostDefaults(webBuilder =>
16-
{
17-
webBuilder.UseStartup<Startup>();
18-
});
19-
}
57+
public const string DbConnectionString = "User ID=postgres;Password=mysecretpassword;Host=127.0.0.1;Port=5432;Database=postgres;";
2058
}

samples/Sample.Kafka.PostgreSql/Startup.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
using Microsoft.AspNetCore.Hosting;
2-
using Microsoft.Extensions.Hosting;
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
34

4-
namespace Sample.Pulsar.InMemory
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Configure services
8+
string pulsarUri = builder.Configuration.GetValue("AppSettings:PulsarUri", "pulsar://localhost:6650");
9+
10+
builder.Services.AddCap(x =>
511
{
6-
public class Program
7-
{
8-
public static void Main(string[] args)
9-
{
10-
CreateHostBuilder(args).Build().Run();
11-
}
12+
x.UseInMemoryStorage();
13+
x.UsePulsar(pulsarUri);
14+
x.UseDashboard();
15+
});
16+
17+
builder.Services.AddControllers();
18+
19+
var app = builder.Build();
20+
21+
// Configure middleware pipeline
22+
app.UseRouting();
23+
app.MapControllers();
1224

13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
14-
Host.CreateDefaultBuilder(args)
15-
.ConfigureWebHostDefaults(webBuilder =>
16-
{
17-
webBuilder.UseStartup<Startup>();
18-
});
19-
}
20-
}
25+
app.Run();

samples/Sample.Pulsar.InMemory/Startup.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
using Microsoft.AspNetCore;
2-
using Microsoft.AspNetCore.Hosting;
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using MongoDB.Driver;
35

4-
namespace Sample.RabbitMQ.MongoDB
6+
var builder = WebApplication.CreateBuilder(args);
7+
8+
// Configure services
9+
builder.Services.AddSingleton<IMongoClient>(new MongoClient(builder.Configuration.GetConnectionString("MongoDB")));
10+
11+
builder.Services.AddCap(x =>
512
{
6-
public class Program
7-
{
8-
public static void Main(string[] args)
9-
{
10-
CreateWebHostBuilder(args).Build().Run();
11-
}
13+
x.UseMongoDB(builder.Configuration.GetConnectionString("MongoDB"));
14+
x.UseRabbitMQ("");
15+
x.UseDashboard();
16+
});
17+
18+
builder.Services.AddControllers();
19+
20+
var app = builder.Build();
21+
22+
// Configure middleware pipeline
23+
app.UseRouting();
24+
app.MapControllers();
1225

13-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14-
WebHost.CreateDefaultBuilder(args)
15-
.UseStartup<Startup>();
16-
}
17-
}
26+
app.Run();

0 commit comments

Comments
 (0)