using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NSC;
using QMFrameWork.Data;
namespace NSC.Service
{
public class SystemServiceEngine : IDisposable
{
///
/// 服务定义列表
///
List ServiceDefineList = null;
///
/// 动态库跟路径
///
public static string DllRootPath { get; set; }
///
/// 装载服务定义
///
public void Load()
{
try
{
string sql = "SELECT * FROM T_QM_SYSSERVICEDEFINE WHERE SERVICESTATE='0'";
using (IDataSession session = DataFactory.CreateSession())
{
ServiceDefineList = session.GetList(sql, (new List()).ToArray()).ToList();
}
}
catch (Exception ex)
{
//记录异常信息
Console.WriteLine(ex.Message);
}
//创建各个服务的实例
foreach (var s in ServiceDefineList)
{
try
{
string path = "";
if(s.DLLPATH.IndexOf(@":\")<0)
{
path = DllRootPath + s.DLLPATH;
}
else
{
path = s.DLLPATH;
}
s.ENTITYSTATE = Common.Data.ServiceEntityState.Loading;
s.SERVICEENTITY = Common.AssemblyUtil.AssemblyFactory.CreateInstanceAnywhere(path, s.CLASSPATH) as IService;
s.ENTITYSTATE = Common.Data.ServiceEntityState.Loaded;
}
catch (Exception ex)
{
//记录异常信息
Console.WriteLine(ex.Message);
}
}
}
///
/// 重新装载服务定义
///
public void Reload()
{
try
{
List list = new List();
string sql = "SELECT * FROM T_QM_SYSSERVICEDEFINE WHERE SERVICESTATE='0'";
using (IDataSession session = DataFactory.CreateSession())
{
list = session.GetList(sql, (new List()).ToArray()).ToList();
}
foreach (var s in ServiceDefineList)
{
var n = list.Find(p => p.SERVICECODE == s.SERVICECODE);
if (n == null)
{
s.SERVICEENTITY = null;
ServiceDefineList.Remove(s);
}
}
foreach (var n in list)
{
var m = ServiceDefineList.Find(p => p.SERVICECODE == n.SERVICECODE);
if (m == null)
{
ServiceDefineList.Add(n);
string path = "";
if (n.DLLPATH.IndexOf(@":\") < 0)
{
path = DllRootPath + n.DLLPATH;
}
else
{
path = n.DLLPATH;
}
n.ENTITYSTATE = Common.Data.ServiceEntityState.Loading;
n.SERVICEENTITY = Common.AssemblyUtil.AssemblyFactory.CreateInstanceAnywhere(path, n.CLASSPATH) as IService;
n.ENTITYSTATE = Common.Data.ServiceEntityState.Loaded;
}
}
}
catch (Exception ex)
{
//记录异常信息
throw ex;
}
}
///
/// 关闭所有服务
///
public void Dispose()
{
ServiceDefineList.Clear();
ServiceDefineList = null;
}
///
/// 服务处理
///
/// 客户端的访问级别
/// 传入的数据
///
internal NetServiceStruct ServiceHandler(Common.Data.UserAuthority userAuth, NetServiceStruct input, bool ignoreCase)
{
var s = ServiceDefineList.Find(p => p.SERVICECODE == input.ServiceName && p.ENTITYSTATE == Common.Data.ServiceEntityState.Loaded);
if (s != null)
{
return s.SERVICEENTITY.ServiceFunction(userAuth, input);
}
else
{
if (!ignoreCase)
{
throw new Common.Exceptions.NoneServiceException();
}
else
{
return null;
}
}
}
}
}