Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet6 got rid of Main method - how can I change the following in Dotnet5? [duplicate]

I have a connection string setup I used in Dotnet5 and prior versions of Dotnet Core and it's worked great, but upgrading to Dotnet6 and I have no idea how to get the following to work (basically a connection string i use called DBConn is the part that needs to change - reads the connection string from appsettings.json as typical):

In the Program.cs file:

public class Program
    {
        public static string DBConn { get; set; } 
        public static void Main(string[] args)
        {
            IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json", true, true).Build();
            DBConn = config.GetConnectionString("DBConn");

            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

In Dotnet6, there is no Program class or Main method (or somehow it's incorporated), so I can't use the database connection code as written. While the docs claim that the Program.cs file IS the Main method - I don't see how this even works nor how to do what's worked for years to work here. I presume there is a better way to get connection string from the appsettings.json file, but I have no idea how to do it (the docs are limited to Dotnet5)

Here's what the program.cs looks like in DotNet6:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
like image 583
MC9000 Avatar asked Sep 14 '25 21:09

MC9000


2 Answers

Because I don't have weeks of time to read/study up on new Microsoft programming paradigms and theoretical best practices get this working, I came up with a kludge that works. No DI, No convoluted options nor "design patterns from Hell that will change in 6 months", No special classes, No Entity Framework, just works. The program.cs as it looks now:

var builder = WebApplication.CreateBuilder(args);
    
    //simple kludge part 1
    string dBconn = builder.Configuration.GetConnectionString("DBConn");
    DBConn = dBconn;
    
    // Add services to the container.
    builder.Services.AddControllers();
    // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    
    app.UseHttpsRedirection();
    
    app.UseAuthorization();
    
    app.MapControllers();
    
    app.Run();
    
    //simple kludge part 2
    partial class Program
    {
        public static string DBConn { get; private set; } = "";
    }

Usage (anywhere)

Program.DBConn;

Is this a good practice? Who knows? But sometimes we aren't given the time to learn 350 pages of stuff in 1 day that will be "obsoleted" a year from now, and need to get shit done. - If MS really wants to help, make the #1 most common function (getting common config settings) built into the framework.

like image 58
MC9000 Avatar answered Sep 17 '25 09:09

MC9000


var builder = WebApplication.CreateBuilder(args);

builder.Configuration
    .AddJsonFile("appsettings.json", false, true)
    .AddJsonFile("strings.json", false, true)
    .AddEnvironmentVariables();

//To get a section from json files :
var serviceOptionsSection = builder.Configuration.GetSection("ServiceOptions");

// To setup custom configs :
builder.Configuration.Setup(o =>
{
    o.ServiceId = _serviceOptions.ServiceId;
    ...
});

// Add services :
builder.Services.AddScoped<IDefaultUnitOfWork, DefaultUnitOfWork>();

// app cases :
var app = builder.Build();
...
app.UseRouting();
...

// Last line :
await app.RunAsync();
like image 41
Ali Khancherli Avatar answered Sep 17 '25 11:09

Ali Khancherli