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

133 lines
4.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ServiceModel;
using QMFrameWork.Common.Serialization;
using QMFrameWork.ServiceInterface;
using QMFrameWork.ServiceLibrary;
using QM.Exchange.Core.Services;
using QM.Exchange.Interface;
namespace QM.Exchange.Core
{
/// <summary>
/// 通用服务
/// </summary>
public class GeneralMessageService : IGeneralMessageService
{
/// <summary>
/// 消息接收
/// </summary>
/// <param name="message">消息</param>
public void RequestServer(CommonMessage message)
{
try
{
MessageQueueService.Enqueue(message);
}
catch (Exception e)
{
throw new FaultException(e.Message);
}
}
/// <summary>
/// 接收服务请求
/// </summary>
/// <param name="request">请求(不支持复杂类型)</param>
/// <returns>返回结果(不支持复杂类型)</returns>
public string RecevieRequest(string request)
{
ServiceRequest r = JsonConvertHelper.GetDeserialize<ServiceRequest>(request);
object result = null;
try
{
if (r.FunctionName == "ServiceInit")
return "success";
if (string.IsNullOrEmpty(request) == true)
throw new FaultException("请求头不能为空。");
//登录验证
new AuthorizationManager().IsAuthorization(r);
Stopwatch sw = new Stopwatch();
sw.Start();
result = new ServiceFactory().InvokeService(r);
sw.Stop();
//记录执行日志
this.RecordServiceLog(r, sw.ElapsedMilliseconds);
return JsonConvertHelper.GetSerializes(result);
}
catch (Exception ex)
{
this.RecordServiceLog(r, ex);
throw new FaultException(ex.Message);
}
}
#region 记录服务执行日志
/// <summary>
/// 记录服务执行日志
/// </summary>
/// <param name="request">请求</param>
/// <param name="time">执行时间</param>
private void RecordServiceLog(ServiceRequest request, long time)
{
//记录执行日志
string clientIP = "";
if (request != null && request.UserCredential != null && request.UserCredential.ClientIP != null)
{
clientIP = request.UserCredential.ClientIP;
}
string info = "执行服务" + request.FunctionName + ",完成时间为:" + time + "毫秒";
string userName = "";
if (request != null && request.UserCredential != null)
{
userName = request.UserCredential.UserName;
}
QMFrameWork.Log.LogManager.LogHelper.Info(
new QMFrameWork.Log.LogInfo { ClientIP = clientIP, UserName = userName, Info = info });
}
#endregion
#region 记录服务异常日志
private void RecordServiceLog(ServiceRequest r, Exception ex)
{
string info = "执行服务";
if (r != null)
info += r.FunctionName;
string clientIP = "";
if (r != null && r.UserCredential != null && r.UserCredential.ClientIP != null)
{
clientIP = r.UserCredential.ClientIP;
}
string userName = "";
if (r != null && r.UserCredential != null)
{
userName = r.UserCredential.UserName;
}
QMFrameWork.Log.LogManager.LogHelper.Error(
new QMFrameWork.Log.LogInfo { ClientIP = clientIP, UserName = userName, Info = info, ErrorInfo = ex });
}
#endregion
}
}