using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using QMFrameWork.Common.Serialization;
using QMFrameWork.ServiceInterface;
namespace QMAPP.ServicesAgent
{
///
/// 服务客户端代理
///
public class ServiceAgent
{
///
/// 用户凭据
///
public CredentialInfo ClientCredential = new CredentialInfo();
///
/// 服务索引
///
private static Dictionary ServiceIndexs { get; set; }
#region 初始化
public static void Init()
{
try
{
//加载通用服务1号节点服务列表
CenterGeneralService.GeneralServiceClient serivceClient = new CenterGeneralService.GeneralServiceClient();
string[] list1 = serivceClient.GetServiceList();
ServiceIndexs = new Dictionary();
foreach (string serviceName in list1)
{
ServiceIndexs.Add(serviceName, "GeneralService1");
}
new ServiceAgent().InvokeServiceFunction("ServiceInit");
}
catch (TimeoutException e)
{
throw new Exception("WCF服务通讯超时,请检查网络连接是否正常!", e);
}
catch (System.ServiceModel.FaultException e)
{
throw new Exception("WCF服务内部错误,请联系管理员上报错误!", e);
}
catch (System.ServiceModel.EndpointNotFoundException e)
{
throw new Exception(string.Format("WCF服务通讯错误,请检查网络连接是否正常!"), e);
}
catch (Exception e)
{
throw new Exception(string.Format("未知的WCF服务错误:\r\n{0}", e.Message), e);
}
}
#endregion
#region 执行服务请求
///
/// 执行服务请求(结果反序列化)
///
/// 返回结果类型
/// 请求名称
/// 参数
/// 返回结果
public ResultModel InvokeServiceFunction(string functionName, params object[] parameters) where ResultModel : new()
{
string returnResult = null;
ResultModel result = default(ResultModel);
try
{
//执行服务请求
returnResult = this.InvokeServiceFunction(functionName, parameters);
result = JsonConvertHelper.GetDeserialize(returnResult);
return result;
}
catch (TimeoutException e)
{
throw new Exception("WCF服务通讯超时,请检查网络连接是否正常!", e);
}
catch (System.ServiceModel.FaultException e)
{
throw new Exception("WCF服务内部错误,请联系管理员上报错误!", e);
}
catch (System.ServiceModel.EndpointNotFoundException e)
{
throw new Exception(string.Format("WCF服务通讯错误,请检查网络连接是否正常!"), e);
}
catch (Exception e)
{
throw new Exception(string.Format("未知的WCF服务错误:\r\n{0}", e.Message), e);
}
}
///
/// 执行服务请求
///
/// 请求名称
///
/// 返回结果
public string InvokeServiceFunction(string functionName, params object[] parameters)
{
string returnResult = null;
ServiceRequest request = null;//服务请求
try
{
request = this.GetServiceRequest();
//设置方法名称
request.FunctionName = functionName;
request.Parameters = parameters.ToList();
request.UserCredential = this.ClientCredential;
//执行服务请求
string[] requestValues = request.FunctionName.Split("_".ToArray());
string serviceName = requestValues[0];
string serviceHost = "";
if (ServiceIndexs != null && serviceName != "ServiceInit")
{
if (ServiceIndexs.ContainsKey(serviceName) == false)
{
throw new Exception("指定的服务不存在。");
}
serviceHost = ServiceIndexs[serviceName];
}
switch (serviceHost.ToLower())
{
case "generalservice1":
CenterGeneralService.GeneralServiceClient serivceClient = new CenterGeneralService.GeneralServiceClient();
returnResult = serivceClient.RecevieRequest(JsonConvertHelper.GetSerializes(request));
break;
default:
CenterGeneralService.GeneralServiceClient defaultClient = new CenterGeneralService.GeneralServiceClient();
returnResult = defaultClient.RecevieRequest(JsonConvertHelper.GetSerializes(request));
break;
}
return returnResult;
}
catch (TimeoutException e)
{
throw new Exception("WCF服务通讯超时,请检查网络连接是否正常!", e);
}
catch (System.ServiceModel.FaultException e)
{
throw new Exception("WCF服务内部错误,请联系管理员上报错误!", e);
}
catch (System.ServiceModel.EndpointNotFoundException e)
{
throw new Exception(string.Format("WCF服务通讯错误,请检查网络连接是否正常!"), e);
}
catch (Exception e)
{
throw new Exception(string.Format("未知的WCF服务错误:\r\n{0}", e.Message), e);
}
}
#endregion
#region 刷新数据锁定标志
///
/// 刷新数据锁定标志
///
public void RefreshDataLock()
{
CenterGeneralService.GeneralServiceClient defaultClient = new CenterGeneralService.GeneralServiceClient();
defaultClient.RefreshDataLock();
}
#endregion
#region 获取服务请求信息
///
/// 获取服务请求信息
///
///
public ServiceRequest GetServiceRequest()
{
return this.GetServiceRequest("");
}
///
/// 获取服务请求信息
///
/// 请求名称
///
public ServiceRequest GetServiceRequest(string functionName)
{
ServiceRequest request = new ServiceRequest();
request.FunctionName = functionName;
request.UserCredential = this.ClientCredential;
return request;
}
#endregion
#region 获取任务调度服务
///
/// 获取任务调度服务
///
/// 任务调度服务
public TaskService.TaskServiceClient GetTaskService()
{
TaskService.TaskServiceClient service = new TaskService.TaskServiceClient();
service.Login(ConfigurationManager.AppSettings["UseCredentialID"]);
return service;
}
#endregion
}
}