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
{
///
/// 服务工厂
///
public class ServiceFactory
{
private static Hashtable ServiceList = null;
///
/// 业务库存储路径
///
public static string BusinessDllPath { get; set; }
///
/// 数据是否锁定
///
public static bool IsDataLock { get; set; }
#region 加载配置信息
///
/// 加载配置信息
///
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();
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();
//service.Functions.Add(new ServiceFunctionDefinition { RequestFunctionName = "GetList", ClassFunctionName="GetList" });
}
}
catch (Exception ex)
{
throw new Exception("加载服务配置信息出现问题!- " + ex.Message);
}
}
#endregion
#region 获取服务列表
///
/// 获取服务列表
///
/// 服务列表
public static List GetServiceList()
{
List list = new List();
try
{
foreach (string key in ServiceList.Keys)
{
list.Add(key);
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 创建服务方法
///
/// 创建服务方法
///
/// 服务请求
/// 服务类
/// 服务方法
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 执行请求
///
/// 执行请求
///
/// 请求
/// 返回结果
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
}
}