天津投入产出系统后端
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.

170 lines
5.5 KiB

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NSC;
using QMFrameWork.Data;
namespace NSC.Service
{
/// <summary>
/// 功能:基于网络数据流的应用服务引擎
/// 作者:王昊昇
/// 时间:2012.11.8
/// </summary>
public class ApplicationServiceEngine : IDisposable
{
/// <summary>
/// 应用定义列表
/// </summary>
List<ServiceDefination> ApplicationDefineList = null;
/// <summary>
/// 动态库跟路径
/// </summary>
public static string DllRootPath { get; set; }
/// <summary>
/// 装载服务定义
/// </summary>
public void Load()
{
try
{
//获取应用定义
string sql = "SELECT * FROM T_QM_SERVICEDEFINE WHERE SERVICESTATE='0'";
using (IDataSession session = DataFactory.CreateSession())
{
ApplicationDefineList = session.GetList<ServiceDefination>(sql, (new List<DataParameter>()).ToArray()).ToList();
}
}
catch (Exception ex)
{
//记录异常信息
Console.WriteLine(ex.Message);
}
//创建各个应用服务的实例
foreach (var s in ApplicationDefineList)
{
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)
{
//记录异常信息
s.ENTITYSTATE = Common.Data.ServiceEntityState.Unloaded;
}
}
}
/// <summary>
/// 重新装载服务定义
/// </summary>
public void Reload()
{
try
{
List<ServiceDefination> list = null;
//获取应用定义
ApplicationDefineList = new List<ServiceDefination>();
string sql = "SELECT * FROM T_QM_SERVICEDEFINE WHERE SERVICESTATE='0'";
using (IDataSession session = DataFactory.CreateSession())
{
list = session.GetList<ServiceDefination>(sql, (new List<DataParameter>()).ToArray()).ToList();
}
foreach (var s in ApplicationDefineList)
{
var n = list.Find(p => p.SERVICECODE == s.SERVICECODE);
if (n == null)
{
s.SERVICEENTITY = null;
ApplicationDefineList.Remove(s);
}
}
foreach (var n in list)
{
var m = ApplicationDefineList.Find(p => p.SERVICECODE == n.SERVICECODE);
if (m == null)
{
ApplicationDefineList.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(n.DLLPATH, n.CLASSPATH) as IService;
n.ENTITYSTATE = Common.Data.ServiceEntityState.Loaded;
}
}
}
catch (Exception ex)
{
//记录异常信息
throw ex;
}
}
/// <summary>
/// 关闭所有服务
/// </summary>
public void Dispose()
{
ApplicationDefineList.Clear();
ApplicationDefineList = null;
}
/// <summary>
/// 服务处理
/// </summary>
/// <param name="userAuth">客户端的访问级别</param>
/// <param name="input">传入的数据</param>
/// <param name="ignoreCase">是否忽略无服务的错误</param>
/// <returns>返回处理后的数据</returns>
public NetServiceStruct ServiceHandler(Common.Data.UserAuthority userAuth, NetServiceStruct input, bool ignoreCase)
{
var a = ApplicationDefineList.Find(p => p.SERVICECODE == input.ServiceName && p.ENTITYSTATE == Common.Data.ServiceEntityState.Loaded);
if (a == null)
{
if (!ignoreCase)
{
//无服务
throw new Common.Exceptions.NoneServiceException();
}
else
{
return null;
}
}
else
{
return a.SERVICEENTITY.ServiceFunction(userAuth, input);
}
}
}
}