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

229 lines
8.0 KiB

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
{
/// <summary>
/// 服务客户端代理
/// </summary>
public class ServiceAgent
{
/// <summary>
/// 用户凭据
/// </summary>
public CredentialInfo ClientCredential = new CredentialInfo();
/// <summary>
/// 服务索引
/// </summary>
private static Dictionary<string, string> ServiceIndexs { get; set; }
#region 初始化
public static void Init()
{
try
{
//加载通用服务1号节点服务列表
CenterGeneralService.GeneralServiceClient serivceClient = new CenterGeneralService.GeneralServiceClient();
string[] list1 = serivceClient.GetServiceList();
ServiceIndexs = new Dictionary<string, string>();
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 执行服务请求
/// <summary>
/// 执行服务请求(结果反序列化)
/// </summary>
/// <typeparam name="ResultModel">返回结果类型</typeparam>
/// <param name="functionName">请求名称</param>
/// <param name="parameters">参数</param>
/// <returns>返回结果</returns>
public ResultModel InvokeServiceFunction<ResultModel>(string functionName, params object[] parameters) where ResultModel : new()
{
string returnResult = null;
ResultModel result = default(ResultModel);
try
{
//执行服务请求
returnResult = this.InvokeServiceFunction(functionName, parameters);
result = JsonConvertHelper.GetDeserialize<ResultModel>(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);
}
}
/// <summary>
/// 执行服务请求
/// </summary>
/// <param name="functionName">请求名称</param>
/// <param name="parameters"></param>
/// <returns>返回结果</returns>
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 刷新数据锁定标志
/// <summary>
/// 刷新数据锁定标志
/// </summary>
public void RefreshDataLock()
{
CenterGeneralService.GeneralServiceClient defaultClient = new CenterGeneralService.GeneralServiceClient();
defaultClient.RefreshDataLock();
}
#endregion
#region 获取服务请求信息
/// <summary>
/// 获取服务请求信息
/// </summary>
/// <returns></returns>
public ServiceRequest GetServiceRequest()
{
return this.GetServiceRequest("");
}
/// <summary>
/// 获取服务请求信息
/// </summary>
/// <param name="functionName">请求名称</param>
/// <returns></returns>
public ServiceRequest GetServiceRequest(string functionName)
{
ServiceRequest request = new ServiceRequest();
request.FunctionName = functionName;
request.UserCredential = this.ClientCredential;
return request;
}
#endregion
#region 获取任务调度服务
/// <summary>
/// 获取任务调度服务
/// </summary>
/// <returns>任务调度服务</returns>
public TaskService.TaskServiceClient GetTaskService()
{
TaskService.TaskServiceClient service = new TaskService.TaskServiceClient();
service.Login(ConfigurationManager.AppSettings["UseCredentialID"]);
return service;
}
#endregion
}
}