[성현모] Config 기능 추가, Socket Recv Json, string 기능 분리
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
@ -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
|
||||
{
|
||||
21
Projects/SystemX.Core/SystemX.Core/Config/Model/Server.cs
Normal file
21
Projects/SystemX.Core/SystemX.Core/Config/Model/Server.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
||||
16
Projects/SystemX.Core/SystemX.Core/Config/WebCommonConfig.cs
Normal file
16
Projects/SystemX.Core/SystemX.Core/Config/WebCommonConfig.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
107
Projects/SystemX.Core/SystemX.Core/DB/DBTransaction.cs
Normal file
107
Projects/SystemX.Core/SystemX.Core/DB/DBTransaction.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Get SqlServer Connection String
|
||||
/// </summary>
|
||||
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<TEntity>? GetEntity<TEntity>(this DbContext dbContext) where TEntity : class
|
||||
{
|
||||
IEnumerable<TEntity>? 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<TEntity>;
|
||||
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<IDbContextTransaction> CreateTransactionAsync(this DbContext dbContext)
|
||||
{
|
||||
return await dbContext.Database.BeginTransactionAsync();
|
||||
}
|
||||
|
||||
public static async Task<bool> 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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Get SqlServer Connection String
|
||||
/// </summary>
|
||||
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<string, DbContext> DicDbContext = new Dictionary<string, DbContext>();
|
||||
|
||||
public EFCore()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
public static IEnumerable<TEntity>? GetEntity<TEntity>(this DbContext dbContext) where TEntity : class
|
||||
{
|
||||
IEnumerable<TEntity>? 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<TEntity>;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log4net.WriteLine(ex);
|
||||
}
|
||||
|
||||
return entity;
|
||||
#region Initialize DBContext
|
||||
private void InitializeDB<TDBContext>() where TDBContext : DbContext, new()
|
||||
{
|
||||
//if (dbList is not null)
|
||||
//{
|
||||
// foreach (var db in dbList)
|
||||
// {
|
||||
// if (typeof(VpkiAccountDbContext).Name == db.DBContext)
|
||||
// {
|
||||
// CreateDBContext<TDBContext>(db);
|
||||
// }
|
||||
// else if (typeof(VpkiDataDbContext).Name == db.DBContext)
|
||||
// {
|
||||
// CreateDBContext<VpkiDataDbContext>(db);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
#region Transaction
|
||||
public static IDbContextTransaction CreateTransaction(this DbContext dbContext)
|
||||
private void CreateDBContext<TDBContext>(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<IDbContextTransaction> CreateTransactionAsync(this DbContext dbContext)
|
||||
public TDBContext? GetDBContext<TDBContext>(int dbID = 1) where TDBContext : DbContext
|
||||
{
|
||||
return await dbContext.Database.BeginTransactionAsync();
|
||||
}
|
||||
TDBContext? dBContext = default;
|
||||
|
||||
public static async Task<bool> 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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
39
Projects/SystemX.Core/SystemX.Core/Services/ConfigService.cs
Normal file
39
Projects/SystemX.Core/SystemX.Core/Services/ConfigService.cs
Normal file
@ -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<T> 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<T>(clientConfigJson);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Config Init Error");
|
||||
Console.WriteLine(e.Message);
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public T? GetConfig()
|
||||
{
|
||||
return Config;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user