|
@@ -1,4 +1,5 @@
|
|
|
using EVCB_OCPP.Domain;
|
|
|
+using EVCB_OCPP.Domain.Extensions;
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
@@ -19,57 +20,6 @@ namespace EVCB_OCPP.WSServer.Helper;
|
|
|
public static class AddPortalDbContext
|
|
|
{
|
|
|
public const string CommandTimeoutKey = "CommandTimeout";
|
|
|
- public static IServiceCollection AddMainDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
- {
|
|
|
- const string DbUserIdKey = "MainDbUserIdKey";
|
|
|
- const string DbPassKey = "MainDbPass";
|
|
|
- const string DbConnectionStringKey = "MainDBContext";
|
|
|
-
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
-
|
|
|
- services.AddSingleton(
|
|
|
- (serviceProvider) =>
|
|
|
- new SqlConnectionFactory<MainDBContext>(serviceProvider.GetRequiredService<ILogger<SqlConnectionFactory>>())
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
- AddPortalDbContextInternal<MainDBContext>(services, configuration, conneciotnString, logToConsole: false);
|
|
|
- return services;
|
|
|
- }
|
|
|
-
|
|
|
- public static IServiceCollection AddMeterValueDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
- {
|
|
|
- const string DbUserIdKey = "MeterValueDbUserId";
|
|
|
- const string DbPassKey = "MeterValueDbPass";
|
|
|
- const string DbConnectionStringKey = "MeterValueDBContext";
|
|
|
-
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- (serviceProvider) =>
|
|
|
- new SqlConnectionFactory<MeterValueDBContext>(serviceProvider.GetRequiredService<ILogger<SqlConnectionFactory>>())
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
- AddPortalDbContextInternal<MeterValueDBContext>(services, configuration, conneciotnString, logToConsole: false);
|
|
|
- return services;
|
|
|
- }
|
|
|
-
|
|
|
- public static IServiceCollection AddConnectionLogDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
- {
|
|
|
- const string DbUserIdKey = "ConnectionLogDbUserId";
|
|
|
- const string DbPassKey = "ConnectionLogDbPass";
|
|
|
- const string DbConnectionStringKey = "ConnectionLogDBContext";
|
|
|
-
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- (serviceProvider) =>
|
|
|
- new SqlConnectionFactory<ConnectionLogDBContext>(serviceProvider.GetRequiredService<ILogger<SqlConnectionFactory>>())
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
- AddPortalDbContextInternal<ConnectionLogDBContext>(services, configuration, conneciotnString);
|
|
|
- return services;
|
|
|
- }
|
|
|
|
|
|
public static IServiceCollection AddWebDBConetext(this IServiceCollection services, IConfiguration configuration)
|
|
|
{
|
|
@@ -77,13 +27,8 @@ public static class AddPortalDbContext
|
|
|
const string DbPassKey = "WebDbPass";
|
|
|
const string DbConnectionStringKey = "WebDBContext";
|
|
|
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- (serviceProvider) =>
|
|
|
- new SqlConnectionFactory<WebDBConetext>(serviceProvider.GetRequiredService<ILogger<SqlConnectionFactory>>())
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
+ var conneciotnString = configuration.GetConnectionString(DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
+ services.AddSqlConnectionFactory<WebDBConetext>(conneciotnString);
|
|
|
return services;
|
|
|
}
|
|
|
|
|
@@ -93,13 +38,8 @@ public static class AddPortalDbContext
|
|
|
const string DbPassKey = "OnlineLogDbPass";
|
|
|
const string DbConnectionStringKey = "OnlineLogDBContext";
|
|
|
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- (serviceProvider) =>
|
|
|
- new SqlConnectionFactory<OnlineLogDBContext>(serviceProvider.GetRequiredService<ILogger<SqlConnectionFactory>>())
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
+ var conneciotnString = configuration.GetConnectionString(DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
+ services.AddSqlConnectionFactory<OnlineLogDBContext>(conneciotnString);
|
|
|
return services;
|
|
|
}
|
|
|
|
|
@@ -118,51 +58,6 @@ public static class AddPortalDbContext
|
|
|
options.UseLoggerFactory(serviceProvider.GetRequiredService<ILoggerFactory>());
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- private static string GetConnectionString(IConfiguration configuration, string UserIdKey, string DbPassKey, string ConnectionStringKey )
|
|
|
- {
|
|
|
- string mainDbUserId = string.IsNullOrEmpty(configuration[UserIdKey]) ? string.Empty : $"user id={configuration[UserIdKey]};";
|
|
|
- string mainDbUserPass = string.IsNullOrEmpty(configuration[DbPassKey]) ? string.Empty : $"password={configuration[DbPassKey]};";
|
|
|
- return $"{configuration.GetConnectionString(ConnectionStringKey)}{mainDbUserId}{mainDbUserPass}";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-public class SqlConnectionFactory<T> where T: DbContext
|
|
|
-{
|
|
|
- private readonly ILogger<SqlConnectionFactory> logger;
|
|
|
-
|
|
|
- public string ConnectionString { get; init; }
|
|
|
- public SqlConnectionFactory(ILogger<SqlConnectionFactory> logger)
|
|
|
- {
|
|
|
- this.logger = logger;
|
|
|
- }
|
|
|
-
|
|
|
- public SqlConnection Create()
|
|
|
- {
|
|
|
- var sqlConnection = new SqlConnection(ConnectionString);
|
|
|
- sqlConnection.Open();
|
|
|
- return sqlConnection;
|
|
|
- }
|
|
|
-
|
|
|
- public async Task<SqlConnection> CreateAsync()
|
|
|
- {
|
|
|
- var timer = Stopwatch.StartNew();
|
|
|
- long t0, t1;
|
|
|
-
|
|
|
- var sqlConnection = new SqlConnection(ConnectionString);
|
|
|
- t0 = timer.ElapsedMilliseconds;
|
|
|
-
|
|
|
- await sqlConnection.OpenAsync();
|
|
|
- t1 = timer.ElapsedMilliseconds;
|
|
|
- timer.Stop();
|
|
|
-
|
|
|
- if (t1 > 500)
|
|
|
- {
|
|
|
- logger.LogWarning($"{typeof(T)} SqlConnection Open slow {t0}/{t1}");
|
|
|
- }
|
|
|
-
|
|
|
- return sqlConnection;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|