[성현모] Select기능 추가, ReadUncommitted level 적용
This commit is contained in:
Binary file not shown.
Binary file not shown.
@ -8,6 +8,8 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace HubX.Library.Http.Packet
|
namespace HubX.Library.Http.Packet
|
||||||
{
|
{
|
||||||
|
#region Unique Key
|
||||||
|
//Insert
|
||||||
public class Request_InsertUniqueKey
|
public class Request_InsertUniqueKey
|
||||||
{
|
{
|
||||||
public string Identity { get; set; } = string.Empty;
|
public string Identity { get; set; } = string.Empty;
|
||||||
@ -18,10 +20,42 @@ namespace HubX.Library.Http.Packet
|
|||||||
public string? Data4 { get; set; } = string.Empty;
|
public string? Data4 { get; set; } = string.Empty;
|
||||||
public string? Data5 { get; set; } = string.Empty;
|
public string? Data5 { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Response_InsertUniqueKy
|
public class Response_InsertUniqueKy
|
||||||
{
|
{
|
||||||
public string? Identity { get; set; } = string.Empty;
|
public string? Identity { get; set; } = string.Empty;
|
||||||
public string? Result { get; set; } = EnumResult.Success.ToString();
|
public string? Result { get; set; } = EnumResult.Success.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Select
|
||||||
|
public class Request_SelectUniqueKey
|
||||||
|
{
|
||||||
|
public string Identity { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
public class Response_SelectUniqueKy
|
||||||
|
{
|
||||||
|
public string? Identity { get; set; } = string.Empty;
|
||||||
|
public string? Data1 { get; set; } = string.Empty;
|
||||||
|
public string? Data2 { get; set; } = string.Empty;
|
||||||
|
public string? Data3 { get; set; } = string.Empty;
|
||||||
|
public string? Data4 { get; set; } = string.Empty;
|
||||||
|
public string? Data5 { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Update
|
||||||
|
public class Request_UpdateUniqueKey
|
||||||
|
{
|
||||||
|
public string Identity { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string? Data1 { get; set; } = string.Empty;
|
||||||
|
public string? Data2 { get; set; } = string.Empty;
|
||||||
|
public string? Data3 { get; set; } = string.Empty;
|
||||||
|
public string? Data4 { get; set; } = string.Empty;
|
||||||
|
public string? Data5 { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
public class Response_UpdateUniqueKy
|
||||||
|
{
|
||||||
|
public string? Identity { get; set; } = string.Empty;
|
||||||
|
public string? Result { get; set; } = EnumResult.Success.ToString();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,7 +60,8 @@ namespace HubX.Library.Socket.Packet
|
|||||||
//json 요청이 아니면 변환
|
//json 요청이 아니면 변환
|
||||||
if (isJsonRequest == false)
|
if (isJsonRequest == false)
|
||||||
{
|
{
|
||||||
result = $"{res.Identity},{res.Result}";
|
if(res != null)
|
||||||
|
result = $"{res.Identity},{res.Result}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,5 +72,109 @@ namespace HubX.Library.Socket.Packet
|
|||||||
|
|
||||||
client.Session.Send(Encoding.UTF8.GetBytes(result) ,EnumMessageId.S2C_INSERT_UniqueKey);
|
client.Session.Send(Encoding.UTF8.GetBytes(result) ,EnumMessageId.S2C_INSERT_UniqueKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async void C2S_SELECT_UniqueKeyHandler(PacketSession session, ArraySegment<byte> buffer)
|
||||||
|
{
|
||||||
|
var recvData = Encoding.UTF8.GetString(buffer);
|
||||||
|
|
||||||
|
//json으로 요청인지 확인
|
||||||
|
bool isJsonRequest = true;
|
||||||
|
|
||||||
|
//convert to object
|
||||||
|
var jsonObject = recvData.ToObject<C2S_INSERT_UniqueKey>();
|
||||||
|
//json 요청 아닐때 변환
|
||||||
|
if (jsonObject == null)
|
||||||
|
{
|
||||||
|
var recvDataList = recvData.Split(",");
|
||||||
|
jsonObject = new C2S_INSERT_UniqueKey
|
||||||
|
{
|
||||||
|
Identity = recvDataList[0]
|
||||||
|
};
|
||||||
|
isJsonRequest = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string result = string.Empty;
|
||||||
|
//insert DB
|
||||||
|
if (jsonObject != null)
|
||||||
|
{
|
||||||
|
Request_SelectUniqueKey request = new Request_SelectUniqueKey();
|
||||||
|
request.Identity = jsonObject.Identity;
|
||||||
|
|
||||||
|
SystemX.Core.Communication.Http http = new();
|
||||||
|
var res = await http.PostJsonAsync<Request_SelectUniqueKey, Response_SelectUniqueKy>("https://127.0.0.1:9000/UniqueKey/SelectUniqueKey", request);
|
||||||
|
result = res.ToJson();
|
||||||
|
|
||||||
|
//json 요청이 아니면 변환
|
||||||
|
if (isJsonRequest == false)
|
||||||
|
{
|
||||||
|
if (res != null)
|
||||||
|
result = $"{res.Identity},{res.Data1},{res.Data2},{res.Data3},{res.Data4},{res.Data5}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientSession clientSession = session as ClientSession;
|
||||||
|
Client client = clientSession.Client;
|
||||||
|
if (client == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
client.Session.Send(Encoding.UTF8.GetBytes(result), EnumMessageId.S2C_SELECT_UniqueKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async void C2S_UPDATE_UniqueKeyHandler(PacketSession session, ArraySegment<byte> buffer)
|
||||||
|
{
|
||||||
|
var recvData = Encoding.UTF8.GetString(buffer);
|
||||||
|
|
||||||
|
//json으로 요청인지 확인
|
||||||
|
bool isJsonRequest = true;
|
||||||
|
|
||||||
|
//convert to object
|
||||||
|
var jsonObject = recvData.ToObject<C2S_INSERT_UniqueKey>();
|
||||||
|
//json 요청 아닐때 변환
|
||||||
|
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],
|
||||||
|
};
|
||||||
|
isJsonRequest = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string result = string.Empty;
|
||||||
|
//insert DB
|
||||||
|
if (jsonObject != null)
|
||||||
|
{
|
||||||
|
Request_InsertUniqueKey request = new Request_InsertUniqueKey();
|
||||||
|
request.Identity = jsonObject.Identity;
|
||||||
|
request.Data1 = jsonObject.Data1;
|
||||||
|
request.Data2 = jsonObject.Data2;
|
||||||
|
request.Data3 = jsonObject.Data3;
|
||||||
|
request.Data4 = jsonObject.Data4;
|
||||||
|
request.Data5 = jsonObject.Data5;
|
||||||
|
|
||||||
|
SystemX.Core.Communication.Http http = new();
|
||||||
|
var res = await http.PostJsonAsync<Request_InsertUniqueKey, Response_InsertUniqueKy>("https://127.0.0.1:9000/UniqueKey/InsertUniqueKey", request);
|
||||||
|
result = res.ToJson();
|
||||||
|
|
||||||
|
//json 요청이 아니면 변환
|
||||||
|
if (isJsonRequest == false)
|
||||||
|
{
|
||||||
|
if (res != null)
|
||||||
|
result = $"{res.Identity},{res.Result}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientSession clientSession = session as ClientSession;
|
||||||
|
Client client = clientSession.Client;
|
||||||
|
if (client == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
client.Session.Send(Encoding.UTF8.GetBytes(result), EnumMessageId.S2C_INSERT_UniqueKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,14 +14,7 @@ namespace HubX.Library.Socket.Packet
|
|||||||
{
|
{
|
||||||
// public EnumMessageId MessageId { get; set; }
|
// public EnumMessageId MessageId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class S2C : IMessage
|
|
||||||
{
|
|
||||||
// public EnumMessageId MessageId { get; set; }
|
|
||||||
|
|
||||||
public EnumMessageResult Result { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed class C2S_INSERT_UniqueKey : C2S
|
public sealed class C2S_INSERT_UniqueKey : C2S
|
||||||
{
|
{
|
||||||
public string Identity { get; set; }
|
public string Identity { get; set; }
|
||||||
@ -33,7 +26,12 @@ namespace HubX.Library.Socket.Packet
|
|||||||
public string Data5 { get; set; }
|
public string Data5 { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class S2C_INSERT_UniqueKey : S2C
|
public sealed class C2S_SELECT_UniqueKey : C2S
|
||||||
|
{
|
||||||
|
public string Identity { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class C2S_UPDATE_UniqueKey : C2S
|
||||||
{
|
{
|
||||||
public string Identity { get; set; }
|
public string Identity { get; set; }
|
||||||
|
|
||||||
|
|||||||
@ -28,6 +28,12 @@ namespace HubX.Library.Socket.Packet
|
|||||||
{
|
{
|
||||||
_onRecv.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, MakePacket<C2S_INSERT_UniqueKey>);
|
_onRecv.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, MakePacket<C2S_INSERT_UniqueKey>);
|
||||||
_handler.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, PacketHandler.C2S_INSERT_UniqueKeyHandler);
|
_handler.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, PacketHandler.C2S_INSERT_UniqueKeyHandler);
|
||||||
|
|
||||||
|
_onRecv.Add((ushort)EnumMessageId.C2S_SELECT_UniqueKey, MakePacket<C2S_SELECT_UniqueKey>);
|
||||||
|
_handler.Add((ushort)EnumMessageId.C2S_SELECT_UniqueKey, PacketHandler.C2S_SELECT_UniqueKeyHandler);
|
||||||
|
|
||||||
|
_onRecv.Add((ushort)EnumMessageId.C2S_UPDATE_UniqueKey, MakePacket<C2S_UPDATE_UniqueKey>);
|
||||||
|
_handler.Add((ushort)EnumMessageId.C2S_UPDATE_UniqueKey, PacketHandler.C2S_UPDATE_UniqueKeyHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnRecvPacket(PacketSession session, ArraySegment<byte> buffer)
|
public void OnRecvPacket(PacketSession session, ArraySegment<byte> buffer)
|
||||||
|
|||||||
@ -27,5 +27,17 @@ namespace HubX.Server.Controllers
|
|||||||
|
|
||||||
return Results.Ok(res);
|
return Results.Ok(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IResult> SelectUniqueKey(Request_SelectUniqueKey request)
|
||||||
|
{
|
||||||
|
var guid = Guid.NewGuid();
|
||||||
|
Log4net.WriteLine($"[Requeust]({guid}) UniqueKey/SelectUniqueKey::{request.ToJson()}", LogType.CONTROLLER);
|
||||||
|
|
||||||
|
Response_SelectUniqueKy res = await _uniqueKeyService.Request_SelectUniqueKey(request);
|
||||||
|
Log4net.WriteLine($"[Response]({guid}) UniqueKey/SelectUniqueKey::{res.ToJson()}", LogType.CONTROLLER);
|
||||||
|
|
||||||
|
return Results.Ok(res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
using DB.HubXDB;
|
using DB.HubXDB;
|
||||||
using HubX.Library.Enums;
|
using HubX.Library.Enums;
|
||||||
using HubX.Library.Http.Packet;
|
using HubX.Library.Http.Packet;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Identity.Client.Extensions.Msal;
|
||||||
|
using System.Data;
|
||||||
using SystemX.Core.DB;
|
using SystemX.Core.DB;
|
||||||
|
|
||||||
namespace HubX.Server.Services
|
namespace HubX.Server.Services
|
||||||
@ -39,10 +42,10 @@ namespace HubX.Server.Services
|
|||||||
if (context != null)
|
if (context != null)
|
||||||
{
|
{
|
||||||
using (var transaction = await context.CreateTransactionAsync())
|
using (var transaction = await context.CreateTransactionAsync())
|
||||||
{
|
{
|
||||||
await context.AddAsync(storage);
|
await context.AddAsync(storage);
|
||||||
transactionResult = await context.CloseTransactionAsync(transaction);
|
transactionResult = await context.CloseTransactionAsync(transaction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//db error
|
//db error
|
||||||
@ -59,5 +62,43 @@ namespace HubX.Server.Services
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Response_SelectUniqueKy> Request_SelectUniqueKey(Request_SelectUniqueKey request)
|
||||||
|
{
|
||||||
|
Response_SelectUniqueKy response = new Response_SelectUniqueKy();
|
||||||
|
|
||||||
|
if (request != null)
|
||||||
|
{
|
||||||
|
response.Identity = request.Identity;
|
||||||
|
|
||||||
|
var context = _efCoreService.GetDBContext<HubXContext>();
|
||||||
|
if (context != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted))
|
||||||
|
{
|
||||||
|
var data = context.TStorages.AsNoTracking().First(x=>x.CIdentity == request.Identity);
|
||||||
|
await context.CloseTransactionAsync(transaction);
|
||||||
|
if(data != null)
|
||||||
|
{
|
||||||
|
response.Data1 = data.CData1;
|
||||||
|
response.Data2 = data.CData2;
|
||||||
|
response.Data3 = data.CData3;
|
||||||
|
response.Data4 = data.CData4;
|
||||||
|
response.Data5 = data.CData5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log4net.WriteLine($"Select Unique Key Transaction Error", LogType.Error);
|
||||||
|
Log4net.WriteLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
using Microsoft.EntityFrameworkCore.Storage;
|
using Microsoft.EntityFrameworkCore.Storage;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -74,9 +75,9 @@ namespace SystemX.Core.DB
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Transaction Async
|
#region Transaction Async
|
||||||
public static async Task<IDbContextTransaction> CreateTransactionAsync(this DbContext dbContext)
|
public static async Task<IDbContextTransaction> CreateTransactionAsync(this DbContext dbContext, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
|
||||||
{
|
{
|
||||||
return await dbContext.Database.BeginTransactionAsync();
|
return await dbContext.Database.BeginTransactionAsync(isolationLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<bool> CloseTransactionAsync(this DbContext dbContext, IDbContextTransaction transaction)
|
public static async Task<bool> CloseTransactionAsync(this DbContext dbContext, IDbContextTransaction transaction)
|
||||||
|
|||||||
@ -1,64 +0,0 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
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 class EFCore
|
|
||||||
{
|
|
||||||
private readonly Dictionary<string, DbContext> DicDbContext = new Dictionary<string, DbContext>();
|
|
||||||
|
|
||||||
public EFCore()
|
|
||||||
: base()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
#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);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateDBContext<TDBContext>(DataBase? dbConfig) where TDBContext : DbContext, new()
|
|
||||||
{
|
|
||||||
//var connectionString = dbConfig?.ConvertToConnectionString();
|
|
||||||
|
|
||||||
//var dbContext = new TDBContext();
|
|
||||||
//dbContext.Database.SetConnectionString($"{connectionString}");
|
|
||||||
|
|
||||||
//if (dbContext is not null)
|
|
||||||
// DicDbContext.Add($"{dbConfig?.DBContext}_{dbConfig?.DBID}", dbContext);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public TDBContext? GetDBContext<TDBContext>(int dbID = 1) where TDBContext : DbContext
|
|
||||||
{
|
|
||||||
TDBContext? dBContext = default;
|
|
||||||
|
|
||||||
var dbContextType = typeof(TDBContext);
|
|
||||||
if (DicDbContext.TryGetValue($"{dbContextType.Name}_{dbID}", out var context) == true)
|
|
||||||
{
|
|
||||||
dBContext = context as TDBContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
return dBContext;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -17,7 +17,9 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="log4net" Version="3.0.4" />
|
<PackageReference Include="log4net" Version="3.0.4" />
|
||||||
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.15" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.15" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.15" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user