diff --git a/Projects/DLL/SystemX.Core.dll b/Projects/DLL/SystemX.Core.dll index 7515f20..d8175ae 100644 Binary files a/Projects/DLL/SystemX.Core.dll and b/Projects/DLL/SystemX.Core.dll differ diff --git a/Projects/HubX/Config/HubX.WebApiConfig.json b/Projects/HubX/Config/HubX.WebApiConfig.json new file mode 100644 index 0000000..8e3987f --- /dev/null +++ b/Projects/HubX/Config/HubX.WebApiConfig.json @@ -0,0 +1,32 @@ +{ + "Server": { + "Address": "https://*", + "Port": 9000, + "IIS": false + }, + "Socket": { + "Address": "*", + "Port": 9010, + "IIS": false + }, + "DataBase": [ + { + "IP": "127.0.0.1", + "Port": 1433, + "DBName": "HubX", + "DBID": 1, + "DBContext": "HubXContext", + "UserID": "alis", + "Password": "Kefico!@34" + }, + { + "IP": "127.0.0.1", + "Port": 1433, + "DBName": "HubX_DEV", + "DBID": 2, + "DBContext": "HubXContext", + "UserID": "alis", + "Password": "Kefico!@34" + } + ] +} \ No newline at end of file diff --git a/Projects/HubX/Config/log4net.config b/Projects/HubX/Config/log4net.config new file mode 100644 index 0000000..0ab5109 --- /dev/null +++ b/Projects/HubX/Config/log4net.config @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac b/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac index 1307ee6..f2df814 100644 Binary files a/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac and b/Projects/HubX/DBPatch/sqlScripts/dacpac/HubX.DB.dacpac differ diff --git a/Projects/HubX/HubX.Library/Config/WebApiConfig.cs b/Projects/HubX/HubX.Library/Config/WebApiConfig.cs new file mode 100644 index 0000000..45e1939 --- /dev/null +++ b/Projects/HubX/HubX.Library/Config/WebApiConfig.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using SystemX.Core.Config; +using SystemX.Core.Config.Model; + +namespace HubX.Library.Config +{ + public class WebApiConfig : WebCommonConfig + { + [JsonPropertyName("DataBase")] + public List? DataBase { get; set; } + + [JsonPropertyName("Socket")] + public Server Socket { get; set; } + } +} diff --git a/Projects/HubX/HubX.Library/DataBase/DBManager.cs b/Projects/HubX/HubX.Library/DataBase/DBManager.cs new file mode 100644 index 0000000..6696e6f --- /dev/null +++ b/Projects/HubX/HubX.Library/DataBase/DBManager.cs @@ -0,0 +1,29 @@ +using Azure.Core; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HubX.Library.DataBase +{ + public class DBManager + { + public DBManager() + { + } + + public async Task Insert(T context) where T : DbContext + { + //using (var transaction = await context.CreateTransactionAsync()) + //{ + // if (context.TCertificates.ToList().Exists(x => x.CCuid == tbscsr.CCuid) == false) + // { + // await context.AddAsync(certificate); + // } + // transactionResult = await context.CloseTransactionAsync(transaction); + //} + } + } +} diff --git a/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs b/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs index 245a75d..2a41316 100644 --- a/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs +++ b/Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs @@ -13,31 +13,37 @@ namespace HubX.Library.Socket.Packet public class PacketHandler { - public static void C2S_INSERT_UniqueKeyHandler(PacketSession session, IMessage packet) - { - C2S_INSERT_UniqueKey movePacket = packet as C2S_INSERT_UniqueKey; - ClientSession clientSession = session as ClientSession; + public static void C2S_INSERT_UniqueKeyHandler(PacketSession session, ArraySegment buffer) + { + var recvData = Encoding.UTF8.GetString(buffer); + + //convert to object + var jsonObject = recvData.ToObject(); + if (jsonObject == null) + { + var recvDataList = recvData.Split(","); + + jsonObject = new C2S_INSERT_UniqueKey + { + Identity = recvDataList[0], + Data1 = recvDataList[1], + Data2 = recvDataList[2], + Data3 = recvDataList[3], + Data4 = recvDataList[4], + Data5 = recvDataList[5], + }; + } + //insert DB + if(jsonObject != null) + { + + } + + ClientSession clientSession = session as ClientSession; Client client = clientSession.Client; if (client == null) return; } - - public static void C_SkillHandler(PacketSession session, IMessage packet) - { - //C_Skill skillPacket = packet as C_Skill; - //ClientSession clientSession = session as ClientSession; - - //Player player = clientSession.MyPlayer; - //if (player == null) - // return; - - //GameRoom room = player.Room; - //if (room == null) - // return; - - //room.Push(room.HandleSkill, player, skillPacket); - } } - } diff --git a/Projects/HubX/HubX.Library/Socket/Packet/Protocol.cs b/Projects/HubX/HubX.Library/Socket/Packet/Protocol.cs index 3909a35..4c5b696 100644 --- a/Projects/HubX/HubX.Library/Socket/Packet/Protocol.cs +++ b/Projects/HubX/HubX.Library/Socket/Packet/Protocol.cs @@ -12,12 +12,12 @@ namespace HubX.Library.Socket.Packet public class C2S : IMessage { - public EnumMessageId MessageId { get; set; } + // public EnumMessageId MessageId { get; set; } } public class S2C : IMessage { - public EnumMessageId MessageId { get; set; } + // public EnumMessageId MessageId { get; set; } public EnumMessageResult Result { get; set; } } diff --git a/Projects/HubX/HubX.Library/Socket/Packet/ServerPacketManager.cs b/Projects/HubX/HubX.Library/Socket/Packet/ServerPacketManager.cs index 62e5426..79b7da6 100644 --- a/Projects/HubX/HubX.Library/Socket/Packet/ServerPacketManager.cs +++ b/Projects/HubX/HubX.Library/Socket/Packet/ServerPacketManager.cs @@ -20,9 +20,9 @@ namespace HubX.Library.Socket.Packet } Dictionary, ushort>> _onRecv = new Dictionary, ushort>>(); - Dictionary> _handler = new Dictionary>(); + Dictionary>> _handler = new Dictionary>>(); - public Action CustomHandler { get; set; } + public Action, ushort> CustomHandler { get; set; } public void Register() { @@ -39,31 +39,34 @@ namespace HubX.Library.Socket.Packet ushort id = BitConverter.ToUInt16(buffer.Array, buffer.Offset + count); count += 2; - Action, ushort> action = null; - if (_onRecv.TryGetValue(id, out action)) - action.Invoke(session, buffer, id); + ushort packetSize = (ushort)(buffer.Count - count); + byte[] packet = new byte[packetSize]; + Array.Copy(buffer.ToArray(), count, packet, 0, packetSize); + + if (_onRecv.TryGetValue(id, out var action)) + action.Invoke(session, packet, id); } - void MakePacket(PacketSession session, ArraySegment buffer, ushort id) where T : IMessage, new() + void MakePacket(PacketSession session, ArraySegment buffer, ushort id) where T : new() { - T pkt = new T(); + // T pkt = new T(); //pkt.MergeFrom(buffer.Array, buffer.Offset + 4, buffer.Count - 4); if (CustomHandler != null) { - CustomHandler.Invoke(session, pkt, id); + CustomHandler.Invoke(session, buffer, id); } else { - Action action = null; + Action> action = null; if (_handler.TryGetValue(id, out action)) - action.Invoke(session, pkt); + action.Invoke(session, buffer); } } - public Action GetPacketHandler(ushort id) + public Action> GetPacketHandler(ushort id) { - Action action = null; + Action> action = null; if (_handler.TryGetValue(id, out action)) return action; return null; diff --git a/Projects/HubX/HubX.Server/Program.cs b/Projects/HubX/HubX.Server/Program.cs index 797a699..0654f85 100644 --- a/Projects/HubX/HubX.Server/Program.cs +++ b/Projects/HubX/HubX.Server/Program.cs @@ -1,7 +1,23 @@ +using HubX.Library.Config; using HubX.Library.Socket.Session; using HubX.Server; +using HubX.Server.TaskManager; using System.Net; +using System.Net.Sockets; using SystemX.Core.Communication; +using SystemX.Core.Services; + +string configDir = @"../Config"; + +//raed log4net config +if (Log4net.IsConfigLoad == true) +{ + Log4net.WriteLine("Log4net Init Success"); +} +else +{ + Log4net.WriteLine("Log4net Init Failed", LogType.Error); +} var builder = WebApplication.CreateBuilder(args); @@ -12,41 +28,61 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +//singleton +builder.Services.AddSingleton>(); + +//config preload +ConfigService preloadConfig = new ConfigService(); +if (preloadConfig.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true) +{ + var config = preloadConfig.GetConfig(); +} +else +{ + Console.WriteLine("Config Preload Load Error."); + return; +} + var app = builder.Build(); +//read api config and set +string serverUrl = string.Empty; +var configService = app.Services.GetService>(); +bool isIIS = false; + +int socketPort = 0; + +if (configService?.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true) +{ + Log4net.WriteLine("WebApi Config Success."); + var apiConfig = ConfigService.Config; + if (apiConfig != null) + { + serverUrl = $"{apiConfig.Server.Address}:{apiConfig.Server.Port}"; + isIIS = apiConfig.Server.IIS; + + //socket + socketPort = apiConfig.Socket.Port; + } +} +else +{ + Log4net.WriteLine("WebApi Config Error."); + return; +} + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { + Log4net.WriteLine($"IsDevelopment:{app.Environment.IsDevelopment()}"); + Log4net.WriteLine($"Swagger Url: {serverUrl}/swagger"); + app.UseSwagger(); app.UseSwaggerUI(); } -Log4net.WriteLine("Run"); -Log4net.WriteLine("Custom LogLevel",LogType.DB); - -WeatherForecast weatherForecast = new WeatherForecast(); -weatherForecast.Summary = "so hot"; -var strJson = weatherForecast.ToJson(); - -var deep = weatherForecast.DeepCopy(); -deep.Summary = "so cool"; - -var rr = strJson.ToObject(); - -Task.Run(async() => -{ - await Task.Delay(2000); - Listener _listener = new Listener(); - - // string host = Dns.GetHostName(); - IPHostEntry ipHost = Dns.GetHostEntry("127.0.0.1"); - IPAddress ipAddr = ipHost.AddressList[0]; - IPEndPoint endPoint = new IPEndPoint(ipAddr, 7777); - - _listener.Init(endPoint, () => { return SessionManager.Instance.Generate(); }); - - Console.WriteLine("Listening..."); -}); +TaskSocket taskSocket = new TaskSocket(); +taskSocket?.Run(socketPort); app.UseHttpsRedirection(); @@ -54,4 +90,12 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); +if (isIIS == true) +{ + app.Run(); +} +else +{ + Log4net.WriteLine($"Operation Url: {serverUrl}"); + app.Run($"{serverUrl}"); +} diff --git a/Projects/HubX/HubX.Server/TaskManager/TaskSocket.cs b/Projects/HubX/HubX.Server/TaskManager/TaskSocket.cs new file mode 100644 index 0000000..bf1b709 --- /dev/null +++ b/Projects/HubX/HubX.Server/TaskManager/TaskSocket.cs @@ -0,0 +1,29 @@ +using HubX.Library.Socket.Session; +using System.Net; +using SystemX.Core.Communication; + +namespace HubX.Server.TaskManager +{ + public class TaskSocket + { + public async Task Run(int socketPort = 7777) + { + try + { + await Task.Delay(1000); + Listener _listener = new Listener(); + IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, socketPort); + + _listener.Init(endPoint, () => { return SessionManager.Instance.Generate(); }); + + Log4net.WriteLine($"Address:{endPoint.Address}, Port:{socketPort}", LogType.SOCKET); + Log4net.WriteLine($"Socket Listening Start", LogType.SOCKET); + } + catch (Exception e) + { + Log4net.WriteLine("Socket Run Failed",LogType.Error); + Log4net.WriteLine(e); + } + } + } +} diff --git a/Projects/HubX/HubX.sln b/Projects/HubX/HubX.sln index 555a145..0401d4e 100644 --- a/Projects/HubX/HubX.sln +++ b/Projects/HubX/HubX.sln @@ -7,9 +7,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HubX.Server", "HubX.Server\ EndProject Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "HubX.DB", "HubX.DB\HubX.DB.sqlproj", "{514DDCCF-6B50-49F8-B212-70498396CF19}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HubX.Library", "HubX.Library\HubX.Library.csproj", "{E6FA1D27-A644-4E50-BF16-DCCA59AA378D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HubX.Library", "HubX.Library\HubX.Library.csproj", "{E6FA1D27-A644-4E50-BF16-DCCA59AA378D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HubX.Library.DB", "HubX.Library.DB\HubX.Library.DB.csproj", "{C43CF1F1-9CB0-44DC-89D7-3514F18EAD46}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HubX.Library.DB", "HubX.Library.DB\HubX.Library.DB.csproj", "{C43CF1F1-9CB0-44DC-89D7-3514F18EAD46}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Config", "Config", "{18B1BDDB-B76F-43A4-BBE8-805CEEF35CE0}" + ProjectSection(SolutionItems) = preProject + Config\HubX.WebApiConfig.json = Config\HubX.WebApiConfig.json + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Projects/SystemX.Core/SystemX.Core/Communication/Socket/Listener.cs b/Projects/SystemX.Core/SystemX.Core/Communication/Socket/Listener.cs index ed7d124..a118396 100644 --- a/Projects/SystemX.Core/SystemX.Core/Communication/Socket/Listener.cs +++ b/Projects/SystemX.Core/SystemX.Core/Communication/Socket/Listener.cs @@ -43,16 +43,23 @@ namespace SystemX.Core.Communication void OnAcceptCompleted(object sender, SocketAsyncEventArgs args) { - if (args.SocketError == SocketError.Success) + try { - Session session = _sessionFactory.Invoke(); - session.Start(args.AcceptSocket); - session.OnConnected(args.AcceptSocket.RemoteEndPoint); + if (args.SocketError == SocketError.Success) + { + Session session = _sessionFactory.Invoke(); + session.Start(args.AcceptSocket); + session.OnConnected(args.AcceptSocket.RemoteEndPoint); + } + else + { + Log4net.WriteLine(args.SocketError.ToString(), LogType.Error); + } } - else + catch(Exception e) { - Log4net.WriteLine(args.SocketError.ToString(), LogType.Error); - } + Log4net.WriteLine(e, LogType.Error); + } RegisterAccept(args); } diff --git a/Projects/SystemX.Core/SystemX.Core/DB/DataBase.cs b/Projects/SystemX.Core/SystemX.Core/Config/Model/DataBase.cs similarity index 95% rename from Projects/SystemX.Core/SystemX.Core/DB/DataBase.cs rename to Projects/SystemX.Core/SystemX.Core/Config/Model/DataBase.cs index 8080bea..9f7706d 100644 --- a/Projects/SystemX.Core/SystemX.Core/DB/DataBase.cs +++ b/Projects/SystemX.Core/SystemX.Core/Config/Model/DataBase.cs @@ -5,7 +5,7 @@ using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; -namespace SystemX.Core.DB +namespace SystemX.Core.Config.Model { public class DataBase { diff --git a/Projects/SystemX.Core/SystemX.Core/Config/Model/Server.cs b/Projects/SystemX.Core/SystemX.Core/Config/Model/Server.cs new file mode 100644 index 0000000..937a168 --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/Config/Model/Server.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace SystemX.Core.Config.Model +{ + public class Server + { + [JsonPropertyName("Address")] + public string? Address { get; set; } + + [JsonPropertyName("Port")] + public int Port { get; set; } + + [JsonPropertyName("IIS")] + public bool IIS { get; set; } + } +} diff --git a/Projects/SystemX.Core/SystemX.Core/Config/WebCommonConfig.cs b/Projects/SystemX.Core/SystemX.Core/Config/WebCommonConfig.cs new file mode 100644 index 0000000..15003a0 --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/Config/WebCommonConfig.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using SystemX.Core.Config.Model; + +namespace SystemX.Core.Config +{ + public class WebCommonConfig + { + [JsonPropertyName("Server")] + public Server Server { get; set; } = new(); + } +} diff --git a/Projects/SystemX.Core/SystemX.Core/DB/DBTransaction.cs b/Projects/SystemX.Core/SystemX.Core/DB/DBTransaction.cs new file mode 100644 index 0000000..90ef9b7 --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/DB/DBTransaction.cs @@ -0,0 +1,107 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SystemX.Core.Config.Model; + +namespace SystemX.Core.DB +{ + public static class DBTransaction + { + /// + /// Get SqlServer Connection String + /// + public static string ConvertToConnectionString(this DataBase dbConfig) + { + return $"server={dbConfig.IP},{dbConfig.Port}; user id={dbConfig.UserID}; password={dbConfig.Password}; database={dbConfig.DBName}; TrustServerCertificate=true;"; + } + + public static IEnumerable? GetEntity(this DbContext dbContext) where TEntity : class + { + IEnumerable? entity = default; + + var type = dbContext.GetType(); + try + { + foreach (var prop in type.GetProperties()) + { + string propertyFullName = $"{prop.PropertyType.FullName}"; + if (propertyFullName.ToLower().Contains(typeof(TEntity).Name.ToLower())) + { + entity = prop.GetValue(dbContext) as IEnumerable; + break; + } + } + } + catch (Exception ex) + { + Log4net.WriteLine(ex); + } + + return entity; + } + + #region Transaction + public static IDbContextTransaction CreateTransaction(this DbContext dbContext) + { + return dbContext.Database.BeginTransaction(); + } + + public static bool CloseTransaction(this DbContext dbContext, IDbContextTransaction transaction) + { + bool result = false; + try + { + dbContext.SaveChanges(); + transaction.Commit(); + result = true; + + Log4net.WriteLine("Transaction Commit", LogType.Debug); + } + catch (Exception ex) + { + transaction.Rollback(); + Log4net.WriteLine("Transaction Rollback", LogType.Error); + Log4net.WriteLine(ex); + } + transaction.Dispose(); + + return result; + } + #endregion + + #region Transaction Async + public static async Task CreateTransactionAsync(this DbContext dbContext) + { + return await dbContext.Database.BeginTransactionAsync(); + } + + public static async Task CloseTransactionAsync(this DbContext dbContext, IDbContextTransaction transaction) + { + bool result = false; + try + { + await dbContext.SaveChangesAsync(); + await transaction.CommitAsync(); + result = true; + + Log4net.WriteLine("Transaction Commit", LogType.Debug); + } + catch (Exception ex) + { + await transaction.RollbackAsync(); + Log4net.WriteLine("Transaction Rollback", LogType.Error); + Log4net.WriteLine(ex); + } + await transaction.DisposeAsync(); + + return result; + } + #endregion + } + + +} diff --git a/Projects/SystemX.Core/SystemX.Core/DB/EFCore.cs b/Projects/SystemX.Core/SystemX.Core/DB/EFCore.cs index bb3a997..5045be6 100644 --- a/Projects/SystemX.Core/SystemX.Core/DB/EFCore.cs +++ b/Projects/SystemX.Core/SystemX.Core/DB/EFCore.cs @@ -1,106 +1,64 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Storage; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SystemX.Core.Config.Model; namespace SystemX.Core.DB { - public static class EFCore + public class EFCore { - /// - /// Get SqlServer Connection String - /// - public static string ConvertToConnectionString(this DataBase dbConfig) - { - return $"server={dbConfig.IP},{dbConfig.Port}; user id={dbConfig.UserID}; password={dbConfig.Password}; database={dbConfig.DBName}; TrustServerCertificate=true;"; + private readonly Dictionary DicDbContext = new Dictionary(); + + public EFCore() + : base() + { } - public static IEnumerable? GetEntity(this DbContext dbContext) where TEntity : class - { - IEnumerable? entity = default; - - var type = dbContext.GetType(); - try - { - foreach (var prop in type.GetProperties()) - { - string propertyFullName = $"{prop.PropertyType.FullName}"; - if (propertyFullName.ToLower().Contains(typeof(TEntity).Name.ToLower())) - { - entity = prop.GetValue(dbContext) as IEnumerable; - break; - } - } - } - catch (Exception ex) - { - Log4net.WriteLine(ex); - } - - return entity; + #region Initialize DBContext + private void InitializeDB() where TDBContext : DbContext, new() + { + //if (dbList is not null) + //{ + // foreach (var db in dbList) + // { + // if (typeof(VpkiAccountDbContext).Name == db.DBContext) + // { + // CreateDBContext(db); + // } + // else if (typeof(VpkiDataDbContext).Name == db.DBContext) + // { + // CreateDBContext(db); + // } + // } + //} } - #region Transaction - public static IDbContextTransaction CreateTransaction(this DbContext dbContext) + private void CreateDBContext(DataBase? dbConfig) where TDBContext : DbContext, new() { - return dbContext.Database.BeginTransaction(); - } + //var connectionString = dbConfig?.ConvertToConnectionString(); - public static bool CloseTransaction(this DbContext dbContext, IDbContextTransaction transaction) - { - bool result = false; - try - { - dbContext.SaveChanges(); - transaction.Commit(); - result = true; + //var dbContext = new TDBContext(); + //dbContext.Database.SetConnectionString($"{connectionString}"); - Log4net.WriteLine("Transaction Commit", LogType.Debug); - } - catch (Exception ex) - { - transaction.Rollback(); - Log4net.WriteLine("Transaction Rollback", LogType.Error); - Log4net.WriteLine(ex); - } - transaction.Dispose(); - - return result; + //if (dbContext is not null) + // DicDbContext.Add($"{dbConfig?.DBContext}_{dbConfig?.DBID}", dbContext); } #endregion - #region Transaction Async - public static async Task CreateTransactionAsync(this DbContext dbContext) + public TDBContext? GetDBContext(int dbID = 1) where TDBContext : DbContext { - return await dbContext.Database.BeginTransactionAsync(); - } + TDBContext? dBContext = default; - public static async Task CloseTransactionAsync(this DbContext dbContext, IDbContextTransaction transaction) - { - bool result = false; - try + var dbContextType = typeof(TDBContext); + if (DicDbContext.TryGetValue($"{dbContextType.Name}_{dbID}", out var context) == true) { - await dbContext.SaveChangesAsync(); - await transaction.CommitAsync(); - result = true; - - Log4net.WriteLine("Transaction Commit", LogType.Debug); + dBContext = context as TDBContext; } - catch (Exception ex) - { - await transaction.RollbackAsync(); - Log4net.WriteLine("Transaction Rollback", LogType.Error); - Log4net.WriteLine(ex); - } - await transaction.DisposeAsync(); - return result; + return dBContext; } - #endregion } - - } diff --git a/Projects/SystemX.Core/SystemX.Core/Log4net/Log4net.cs b/Projects/SystemX.Core/SystemX.Core/Log4net/Log4net.cs index 8d10913..34bb330 100644 --- a/Projects/SystemX.Core/SystemX.Core/Log4net/Log4net.cs +++ b/Projects/SystemX.Core/SystemX.Core/Log4net/Log4net.cs @@ -54,13 +54,15 @@ public static class Log4net static Log4net() { - Console.WriteLine("log4net constructor"); - if (File.Exists("./log4net.config") == false) + string log4netConfigPath = @"../Config/log4net.config"; + + if (File.Exists(log4netConfigPath) == false) { - File.WriteAllText("log4net.config", Config); + Console.WriteLine($"create log4netConfig: {log4netConfigPath}"); + File.WriteAllText(log4netConfigPath, Config); } - IsConfigLoad = OpenConfig(@"./log4net.config"); + IsConfigLoad = OpenConfig(log4netConfigPath); } private static bool OpenConfig(string path) diff --git a/Projects/SystemX.Core/SystemX.Core/Services/ConfigService.cs b/Projects/SystemX.Core/SystemX.Core/Services/ConfigService.cs new file mode 100644 index 0000000..22d45fa --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/Services/ConfigService.cs @@ -0,0 +1,39 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SystemX.Core.Config; + +namespace SystemX.Core.Services +{ + public class ConfigService where T : WebCommonConfig + { + public static T? Config { get; set; } + + public bool OpenConfig(string path) + { + bool result = true; + + try + { + string clientConfigJson = File.ReadAllText(path); + Config = JsonConvert.DeserializeObject(clientConfigJson); + } + catch (Exception e) + { + Console.WriteLine("Config Init Error"); + Console.WriteLine(e.Message); + result = false; + } + + return result; + } + + public T? GetConfig() + { + return Config; + } + } +} diff --git a/Projects/SystemX.Core/SystemX.Core/Utils/JsonUtils.cs b/Projects/SystemX.Core/SystemX.Core/Utils/JsonUtils.cs index 4cda787..16ae4c1 100644 --- a/Projects/SystemX.Core/SystemX.Core/Utils/JsonUtils.cs +++ b/Projects/SystemX.Core/SystemX.Core/Utils/JsonUtils.cs @@ -32,8 +32,7 @@ public static class JsonUtils } catch(Exception e) { - Log4net.WriteLine("JsonUtils.ToObject()", LogType.Error); - Log4net.WriteLine(e); + Log4net.WriteLine("JsonUtils.ToObject()", LogType.Warn); } return result;