Hi @winanjaya,
It seems the _connection.ExecuteScalar<long>
is your customize extension method. Any way, for how to dependency injection in the static class, you could follow like below:
Program.cs
builder.Services.AddSingleton<IDbConnection>((sp) => new NpgsqlConnection(Models.AppSettings.PG_SQL.Connection_String));
var app = builder.Build();
var connection = app.Services.GetRequiredService<IDbConnection>();
Test.Configure(connection);
Test.cs
public class Test
{
protected static IDbConnection _connection;
public static void Configure(IDbConnection connection)
{
_connection = connection;
}
public static async Task MyTest()
{
//...
}
}
Call the static class in the controller action
public class HomeController : Controller
{
public async Task Index()
{
await Test.MyTest();
}
}
If there has any problem, please let me know freely.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Rena