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.
278 lines
8.8 KiB
278 lines
8.8 KiB
4 years ago
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Xml.Linq;
|
||
|
using System.Text;
|
||
|
using System.Reflection;
|
||
|
using QMAPP.BLL;
|
||
|
using QMFrameWork.ServiceInterface;
|
||
|
using QMFrameWork.Common.Serialization;
|
||
|
|
||
|
namespace QMFrameWork.ServiceLibrary
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 服务工厂
|
||
|
/// </summary>
|
||
|
public class ServiceFactory
|
||
|
{
|
||
|
private static Hashtable ServiceList = null;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 业务库存储路径
|
||
|
/// </summary>
|
||
|
public static string BusinessDllPath { get; set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// 数据是否锁定
|
||
|
/// </summary>
|
||
|
public static bool IsDataLock { get; set; }
|
||
|
|
||
|
#region 加载配置信息
|
||
|
|
||
|
/// <summary>
|
||
|
/// 加载配置信息
|
||
|
/// </summary>
|
||
|
public static void Configure(string config)
|
||
|
{
|
||
|
if (ServiceList == null)
|
||
|
ServiceList = new Hashtable();
|
||
|
|
||
|
try
|
||
|
{
|
||
|
string file = config;
|
||
|
|
||
|
|
||
|
XElement xel = XElement.Load(file);
|
||
|
|
||
|
var datas = from x in xel.Descendants("ServiceDefinition")
|
||
|
select x;
|
||
|
|
||
|
foreach (XElement d in datas)
|
||
|
{
|
||
|
string serviceName = d.Element("ServiceName").Value;
|
||
|
|
||
|
//判断服务名是否重复
|
||
|
if (ServiceList.ContainsKey(serviceName) == true)
|
||
|
{
|
||
|
//服务名重复
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
//定义服务
|
||
|
ServiceDefinition service = new ServiceDefinition();
|
||
|
service.ServiceName = d.Element("ServiceName").Value;
|
||
|
service.FullLogicClassName = d.Element("FullLogicClassName").Value;
|
||
|
service.AssemblyFile = BusinessDllPath + d.Element("AssemblyFile").Value;
|
||
|
|
||
|
if (d.Element("IsCredential") != null)
|
||
|
{
|
||
|
service.IsCredential = bool.Parse(d.Element("IsCredential").Value);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
service.IsCredential = true;
|
||
|
}
|
||
|
|
||
|
//锁定标记
|
||
|
service.NeedLockFunctions = new Dictionary<string, string>();
|
||
|
if (d.Element("LockFunctions") != null)
|
||
|
{
|
||
|
string[] lockList = d.Element("LockFunctions").Value.ToString().Split(",".ToCharArray());
|
||
|
|
||
|
foreach (string f in lockList)
|
||
|
{
|
||
|
service.NeedLockFunctions.Add(f, f);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ServiceList.Add(service.ServiceName, service);
|
||
|
|
||
|
//定义方法
|
||
|
//service.Functions = new List<ServiceFunctionDefinition>();
|
||
|
//service.Functions.Add(new ServiceFunctionDefinition { RequestFunctionName = "GetList", ClassFunctionName="GetList" });
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw new Exception("加载服务配置信息出现问题!- " + ex.Message);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 获取服务列表
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取服务列表
|
||
|
/// </summary>
|
||
|
/// <returns>服务列表</returns>
|
||
|
public static List<string> GetServiceList()
|
||
|
{
|
||
|
List<string> list = new List<string>();
|
||
|
|
||
|
try
|
||
|
{
|
||
|
foreach (string key in ServiceList.Keys)
|
||
|
{
|
||
|
list.Add(key);
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 创建服务方法
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建服务方法
|
||
|
/// </summary>
|
||
|
/// <param name="request">服务请求</param>
|
||
|
/// <param name="serviceClass">服务类</param>
|
||
|
/// <returns>服务方法</returns>
|
||
|
public static MethodInfo CreateServiceFunction(ServiceRequest request, ref object serviceClass)
|
||
|
{
|
||
|
MethodInfo method = null;
|
||
|
string[] requestValues = request.FunctionName.Split("_".ToArray());
|
||
|
string serviceName = requestValues[0];
|
||
|
string requestFunctionName = requestValues[1];
|
||
|
|
||
|
if (ServiceList.ContainsKey(serviceName) == true)
|
||
|
{
|
||
|
ServiceDefinition service = ServiceList[serviceName] as ServiceDefinition;
|
||
|
|
||
|
if (service.IsCredential == true && request.UserCredential == null)
|
||
|
{
|
||
|
throw new Exception("没有指定用户凭据。");
|
||
|
}
|
||
|
|
||
|
string functionName = requestFunctionName;
|
||
|
|
||
|
//数据锁定
|
||
|
if (IsDataLock == true)
|
||
|
{
|
||
|
if (service.NeedLockFunctions.ContainsKey("all") == true
|
||
|
|| service.NeedLockFunctions.ContainsKey(functionName) == true)
|
||
|
{
|
||
|
throw new Exception("系统已锁定,请求的方法禁止执行!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (service.Functions != null && service.Functions.Count > 0)
|
||
|
{
|
||
|
ServiceFunctionDefinition classFunction = service.Functions.Find(p => p.RequestFunctionName == requestFunctionName);
|
||
|
|
||
|
if (classFunction == null)
|
||
|
{
|
||
|
throw new Exception("请求的方法不存在!");
|
||
|
}
|
||
|
|
||
|
functionName = classFunction.ClassFunctionName;
|
||
|
}
|
||
|
|
||
|
Assembly Asse = Assembly.LoadFrom(service.AssemblyFile);
|
||
|
serviceClass = Asse.CreateInstance(service.FullLogicClassName);
|
||
|
|
||
|
#region 设置登录信息
|
||
|
|
||
|
BaseBLL bll = serviceClass as BaseBLL;
|
||
|
if (bll == null)
|
||
|
{
|
||
|
throw new Exception("逻辑对象没有继承基类BaseBLL");
|
||
|
}
|
||
|
|
||
|
ServiceSession session = null;
|
||
|
if (request.UserCredential != null && request.UserCredential.CredentialID != null)
|
||
|
{
|
||
|
session = SessionManager.GetSession(request.UserCredential.CredentialID);
|
||
|
}
|
||
|
|
||
|
if (session != null)
|
||
|
{
|
||
|
bll.LoginUser = session.UserLogin;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
bll.LoginUser = new QMAPP.Entity.Sys.LoginInfo { UserID = request.UserCredential.UserID };
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
method = serviceClass.GetType().GetMethod(functionName);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
throw new Exception("请求的服务不存在或没有继承逻辑层基类!");
|
||
|
}
|
||
|
|
||
|
|
||
|
return method;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 执行请求
|
||
|
|
||
|
/// <summary>
|
||
|
/// 执行请求
|
||
|
/// </summary>
|
||
|
/// <param name="request">请求</param>
|
||
|
/// <returns>返回结果</returns>
|
||
|
public object InvokeService(ServiceRequest request)
|
||
|
{
|
||
|
MethodInfo method = null;
|
||
|
object result = null;
|
||
|
object serviceClass = null;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//获取服务方法
|
||
|
method = CreateServiceFunction(request, ref serviceClass);
|
||
|
|
||
|
if (method == null)
|
||
|
throw new Exception("获取服务方法失败。");
|
||
|
|
||
|
//参数转换
|
||
|
ParameterInfo[] ps = method.GetParameters();
|
||
|
for (int i = 0; i < request.Parameters.Count; i++)
|
||
|
{
|
||
|
switch (ps[i].ParameterType.FullName)
|
||
|
{
|
||
|
case "System.Boolean":
|
||
|
request.Parameters[i] = bool.Parse(request.Parameters[i].ToString());
|
||
|
break;
|
||
|
case "System.String":
|
||
|
request.Parameters[i] = request.Parameters[i];
|
||
|
break;
|
||
|
default:
|
||
|
request.Parameters[i] = JsonConvertHelper.GetDeserialize(ps[i].ParameterType, request.Parameters[i].ToString());
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
//执行
|
||
|
result = method.Invoke(serviceClass, request.Parameters.ToArray());
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
if (ex.InnerException != null)
|
||
|
throw ex.InnerException;
|
||
|
else
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|