diff --git a/SCHALE.Common/Database/ServicesExtesions.cs b/SCHALE.Common/Database/ServicesExtesions.cs deleted file mode 100644 index bfe04f3..0000000 --- a/SCHALE.Common/Database/ServicesExtesions.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace SCHALE.Common.Database -{ - public static class ServicesExtesions - { - public static void AddSQLServerProvider(this IServiceCollection services, string connectionString) - { - services.AddDbContext(o => - { - o.UseSqlServer(connectionString, b => - { - b.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null); - }) - .UseLazyLoadingProxies(); - }, ServiceLifetime.Singleton, ServiceLifetime.Singleton); - } - } -} diff --git a/SCHALE.GameServer/GameServer.cs b/SCHALE.GameServer/GameServer.cs index 8ba8cfb..99a54c4 100644 --- a/SCHALE.GameServer/GameServer.cs +++ b/SCHALE.GameServer/GameServer.cs @@ -80,19 +80,8 @@ namespace SCHALE.GameServer ); builder.Host.UseSerilog(); - // Add services to the container. - builder.Services.AddSingleton(s => - { - var sqlProvider = config.GetValue("SQL Provider"); - var noConnectionStringException = new ArgumentNullException($"ConnectionString for {sqlProvider} in appsettings is missing"); - return sqlProvider switch - { - "SQLite3" => SCHALESqliteContext.Create(config.GetConnectionString("SQLite3") ?? throw noConnectionStringException), - "SQLServer" => SCHALEContext.Create(config.GetConnectionString("SQLServer") ?? throw noConnectionStringException), - _ => throw new ArgumentException($"SQL Provider '{sqlProvider}' is not valid"), - }; - }); - + // Add services to the container. + builder.Services.AddDbProvider(config); builder.Services.AddControllers(); builder.Services.AddProtocolHandlerFactory(); builder.Services.AddMemorySessionKeyService(); diff --git a/SCHALE.GameServer/Utils/ServiceExtensions.cs b/SCHALE.GameServer/Utils/ServiceExtensions.cs new file mode 100644 index 0000000..bb071f0 --- /dev/null +++ b/SCHALE.GameServer/Utils/ServiceExtensions.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore; +using SCHALE.Common.Database; + +namespace SCHALE.GameServer.Utils +{ + public static class ServicesExtesions + { + public static Exception NoConnectionStringException = new ArgumentNullException($"ConnectionString in appsettings is missing"); + + public static void AddDbProvider(this IServiceCollection services, IConfiguration conf) + { + var sqlProvider = conf.GetValue("SQL Provider"); + switch (sqlProvider) + { + case "SQLite3": + services.AddDbContext(opt => + opt + .UseSqlite(conf.GetConnectionString("SQLite3") ?? throw NoConnectionStringException) + .UseLazyLoadingProxies() + , ServiceLifetime.Singleton, ServiceLifetime.Singleton); + break; + case "SQLServer": + services.AddDbContext(opt => + opt + .UseSqlServer(conf.GetConnectionString("SQLServer") ?? throw NoConnectionStringException, + actions => + { + actions.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null); + }) + .UseLazyLoadingProxies() + , ServiceLifetime.Singleton, ServiceLifetime.Singleton); + break; + default: throw new ArgumentException($"SQL Provider '{sqlProvider}' is not valid"); + } + } + } +} \ No newline at end of file