You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
6.0 KiB
166 lines
6.0 KiB
|
|
namespace EnterprizeV4.Models
|
|
{
|
|
using CounsellorBL;
|
|
using CounsellorBL.BLStructure;
|
|
using CounsellorBL.Helper;
|
|
using DefenseWeb.Models;
|
|
using MonumentDefine;
|
|
using OT.COM.LogisticsUtil;
|
|
using OT.COM.SignalerMessage;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public class CmdService : AApiServiceBase
|
|
{
|
|
static readonly ConcurrentDictionary<string, Type> _dicTypeMapping = new ConcurrentDictionary<string, Type>();
|
|
|
|
public CResponsePack GetData(CReqestPack i_crpRequest, ControllDataPack i_cdp)
|
|
{
|
|
List<CResponseMessage> lcrmr = new List<CResponseMessage>();
|
|
|
|
foreach (CReqestItem cri in i_crpRequest.reqs)
|
|
{
|
|
CRequestMessage crm = cri.req;
|
|
crm.token = i_crpRequest.token;
|
|
|
|
try
|
|
{
|
|
crm.clientip = i_cdp.GetIP;
|
|
crm.system_param.Add(typeof(ControllDataPack).Name, i_cdp);
|
|
CResponseMessage crp = HandleRequest(crm);
|
|
#if DEBUG
|
|
if (!string.IsNullOrEmpty(crp.msg))
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"***************** {nameof(GetData)} module={crm.module} method={crm.method} Fail!! msg={crp.msg}");
|
|
}
|
|
#endif
|
|
|
|
lcrmr.Add(crp);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lcrmr.Add(new CErrorResponseMessage(Util.GetLastExceptionMsg(ex)));
|
|
}
|
|
}
|
|
|
|
return new CResponsePack() { reps = lcrmr };
|
|
}
|
|
|
|
protected override CResponseMessage HandleRequest(CRequestMessage i_joRequest)
|
|
{
|
|
CResponseMessage crmRes;
|
|
try
|
|
{
|
|
do
|
|
{
|
|
if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.method) || string.IsNullOrEmpty(i_joRequest.module))
|
|
{
|
|
crmRes = new CErrorResponseMessage(MessageWording.MTHOD_MISS, i_joRequest);
|
|
break;
|
|
}
|
|
|
|
|
|
string sModuleName = null;
|
|
string[] saModule = i_joRequest.module.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
|
Type tCur = null;
|
|
const string SERVICE = "Service";
|
|
sModuleName = saModule[^1];
|
|
|
|
if(!sModuleName.EndsWith(SERVICE))
|
|
{
|
|
sModuleName += SERVICE;
|
|
}
|
|
|
|
if (_dicTypeMapping.ContainsKey(sModuleName) )
|
|
{
|
|
tCur = _dicTypeMapping[sModuleName];
|
|
}
|
|
else
|
|
{
|
|
Logger.Debug("Not in _dicTypeMapping");
|
|
tCur = GetCustomTypeByTypeName(sModuleName, typeof(ServiceBase));
|
|
if (tCur == null)
|
|
{
|
|
AssemblyHelper.Load(Assembly.GetExecutingAssembly(), "CounsellorBL.*.dll");
|
|
|
|
tCur = GetCustomTypeByTypeName(sModuleName, typeof(ServiceBase));
|
|
}
|
|
|
|
if (tCur != null)
|
|
{
|
|
Logger.Debug("Load assembly success and add to _dicTypeMapping");
|
|
_dicTypeMapping.TryAdd(sModuleName, tCur);
|
|
}
|
|
else
|
|
{
|
|
Logger.Debug("Load assembly fail.");
|
|
crmRes = new CErrorResponseMessage("Find Type fail", i_joRequest);
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
object oModule = ClassHelper.GetInstByType(tCur);
|
|
|
|
if (oModule == null)
|
|
{
|
|
crmRes = new CErrorResponseMessage("Create service fail", i_joRequest);
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
if (!(oModule is ServiceBase bls))
|
|
{
|
|
crmRes = new CErrorResponseMessage(BLWording.COVERT_FAIL, i_joRequest);
|
|
break;
|
|
}
|
|
bls.OriRequest = i_joRequest;
|
|
crmRes = bls.Entry(i_joRequest);
|
|
|
|
|
|
}
|
|
while (false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
crmRes = new CErrorResponseMessage(Util.GetLastExceptionMsg(ex), i_joRequest);
|
|
}
|
|
return crmRes;
|
|
}
|
|
|
|
public Type GetCustomTypeByTypeName(string i_sTypeName, Type i_tBaseClass = null)
|
|
{
|
|
Type type = null;
|
|
string codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
|
|
codeBase = codeBase.Substring(0, codeBase.LastIndexOf("/"));
|
|
|
|
Assembly[] assemblyArray = (from f in AppDomain.CurrentDomain.GetAssemblies()
|
|
where !f.IsDynamic
|
|
&& f.FullName.IndexOf("CounsellorBL", StringComparison.OrdinalIgnoreCase) != -1
|
|
&& f.CodeBase != null
|
|
&& f.CodeBase.StartsWith(codeBase, StringComparison.OrdinalIgnoreCase)
|
|
select f).ToArray<Assembly>();
|
|
|
|
|
|
|
|
foreach (Assembly assembly2 in assemblyArray)
|
|
{
|
|
|
|
type = i_tBaseClass != null ?
|
|
assembly2.GetTypes().FirstOrDefault(x => x.IsClass && x.IsSubclassOf(i_tBaseClass) && x.Name == i_sTypeName) :
|
|
assembly2.GetTypes().FirstOrDefault(x => x.IsClass && x.Name == i_sTypeName);
|
|
if (type != null)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return type;
|
|
}
|
|
|
|
}
|
|
}
|