using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Text; using NSC; using QMFrameWork.Data; namespace NSC.Service { /// /// 功能:基于网络数据流的应用服务引擎 /// 作者:王昊昇 /// 时间:2012.11.8 /// public class ApplicationServiceEngine : IDisposable { /// /// 应用定义列表 /// List ApplicationDefineList = null; /// /// 动态库跟路径 /// public static string DllRootPath { get; set; } /// /// 装载服务定义 /// public void Load() { try { //获取应用定义 string sql = "SELECT * FROM T_QM_SERVICEDEFINE WHERE SERVICESTATE='0'"; using (IDataSession session = DataFactory.CreateSession()) { ApplicationDefineList = session.GetList(sql, (new List()).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; } } } /// /// 重新装载服务定义 /// public void Reload() { try { List list = null; //获取应用定义 ApplicationDefineList = new List(); string sql = "SELECT * FROM T_QM_SERVICEDEFINE WHERE SERVICESTATE='0'"; using (IDataSession session = DataFactory.CreateSession()) { list = session.GetList(sql, (new List()).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; } } /// /// 关闭所有服务 /// public void Dispose() { ApplicationDefineList.Clear(); ApplicationDefineList = null; } /// /// 服务处理 /// /// 客户端的访问级别 /// 传入的数据 /// 是否忽略无服务的错误 /// 返回处理后的数据 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); } } } }