/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { var requestMessage = context.GetRequestMessage(); //得到消息的唯一标识。 var identity = GetMessageIdentity(requestMessage); #region 删除无效的消息标识以节省资源 for (var i = 0; i < MessageIdentity.Count; i++) { var item = MessageIdentity[i]; //消息处理时间大于30秒则删除该条标识。 if (item.Value.AddSeconds(30) < DateTime.Now) { MessageIdentity.Remove(item); } } #endregion 删除无效的消息标识以节省资源 //如果消息已经被标识为处理则跳过。 if (MessageIdentity.Any(i => i.Key == identity)) return EmptyHandlerMiddleware.Instance.Invoke(context); //标识消息正在处理。 MessageIdentity.Add(new KeyValuePair<string, DateTime>(identity, DateTime.Now)); return Next.Invoke(context); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { var requestMessage = context.GetRequestMessage(); //得到消息的唯一标识。 var identity = GetMessageIdentity(requestMessage); #region 除无效的消息标识以节省资源 for (var i = 0; i < MessageIdentity.Count; i++) { var item = MessageIdentity[i]; //消息处理时间大于30秒则删除该条标识。 if (item.Value.AddSeconds(30) < DateTime.Now) { MessageIdentity.Remove(item); } } #endregion 除无效的消息标识以节省资源 //如果消息已经被标识为处理则跳过。 if (MessageIdentity.Any(i => i.Key == identity)) { return(EmptyHandlerMiddleware.Instance.Invoke(context)); } //标识消息正在处理。 MessageIdentity.Add(new KeyValuePair <string, DateTime>(identity, DateTime.Now)); return(Next.Invoke(context)); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { Context = context; var requestMessage = context.GetRequestMessage(); IResponseMessage responseMessage; switch (requestMessage.MessageType) { case RequestMessageType.Event: responseMessage = OnEventRequest(requestMessage as EventMessageBase); break; case RequestMessageType.Image: responseMessage = OnImageRequest(requestMessage as RequestMessageImage); break; case RequestMessageType.Link: responseMessage = OnLinkRequest(requestMessage as RequestMessageLink); break; case RequestMessageType.Location: responseMessage = OnLocationRequest(requestMessage as RequestMessageLocation); break; case RequestMessageType.ShortVideo: responseMessage = OnShortVideoRequest(requestMessage as RequestMessageShortVideo); break; case RequestMessageType.Text: responseMessage = OnTextRequest(requestMessage as RequestMessageText); break; case RequestMessageType.Video: responseMessage = OnVideoRequest(requestMessage as RequestMessageVideo); break; case RequestMessageType.Voice: responseMessage = OnVoiceRequest(requestMessage as RequestMessageVoice); break; default: throw new NotSupportedException("不支持的请求消息类型:" + requestMessage.MessageType); } if (responseMessage != null) { //基本信息初始化。 responseMessage.CreateTime = DateTime.Now; responseMessage.FromUserName = requestMessage.ToUserName; responseMessage.ToUserName = requestMessage.FromUserName; } context.SetResponseMessage(responseMessage); return(Next.Invoke(context)); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { Context = context; var requestMessage = context.GetRequestMessage(); IResponseMessage responseMessage; switch (requestMessage.MessageType) { case RequestMessageType.Event: responseMessage = OnEventRequest(requestMessage as EventMessageBase); break; case RequestMessageType.Image: responseMessage = OnImageRequest(requestMessage as RequestMessageImage); break; case RequestMessageType.Link: responseMessage = OnLinkRequest(requestMessage as RequestMessageLink); break; case RequestMessageType.Location: responseMessage = OnLocationRequest(requestMessage as RequestMessageLocation); break; case RequestMessageType.ShortVideo: responseMessage = OnShortVideoRequest(requestMessage as RequestMessageShortVideo); break; case RequestMessageType.Text: responseMessage = OnTextRequest(requestMessage as RequestMessageText); break; case RequestMessageType.Video: responseMessage = OnVideoRequest(requestMessage as RequestMessageVideo); break; case RequestMessageType.Voice: responseMessage = OnVoiceRequest(requestMessage as RequestMessageVoice); break; default: throw new NotSupportedException("不支持的请求消息类型:" + requestMessage.MessageType); } if (responseMessage != null) { //基本信息初始化。 responseMessage.CreateTime = DateTime.Now; responseMessage.FromUserName = requestMessage.ToUserName; responseMessage.ToUserName = requestMessage.FromUserName; } context.SetResponseMessage(responseMessage); return Next.Invoke(context); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { _sessionCollection = context.GetDependencyResolver().GetService <IUserSessionCollection>(); var requestMessage = context.GetRequestMessage(); //设置用户会话。 context.SetSession(_sessionCollection.GetOrAdd(requestMessage.FromUserName)); return(Next.Invoke(context)); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { var responseMessage = context.GetResponseMessage(); if (responseMessage == null) { context.ResponseXml = string.Empty; } else { var requestMessage = context.GetRequestMessage(); //基本信息初始化。 responseMessage.CreateTime = DateTime.Now; responseMessage.FromUserName = requestMessage.ToUserName; responseMessage.ToUserName = requestMessage.FromUserName; var dependencyResolver = context.GetDependencyResolver(); var responseMessageFactory = dependencyResolver.GetService <IResponseMessageFactory>(); var content = responseMessageFactory.GetXmlByReponseMessage(responseMessage); #region Encrypt if (!string.IsNullOrWhiteSpace(content)) { var request = context.Request; var encryptType = request.QueryString["encrypt_type"]; if (encryptType != null) { var nonce = request.QueryString["nonce"]; var timestamp = request.QueryString["timestamp"]; var baseInfo = context.GetMessageHandlerBaseInfo(); var appId = baseInfo.AppId; var encodingAesKey = baseInfo.EncodingAesKey; var token = baseInfo.Token; var wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAesKey, appId); wxBizMsgCrypt.EncryptMsg(content, timestamp, nonce, ref content); } } #endregion Encrypt context.ResponseXml = content; } return(Next.Invoke(context)); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { var responseMessage = context.GetResponseMessage(); if (responseMessage == null) { context.ResponseXml = string.Empty; } else { var requestMessage = context.GetRequestMessage(); //基本信息初始化。 responseMessage.CreateTime = DateTime.Now; responseMessage.FromUserName = requestMessage.ToUserName; responseMessage.ToUserName = requestMessage.FromUserName; var dependencyResolver = context.GetDependencyResolver(); var responseMessageFactory = dependencyResolver.GetService<IResponseMessageFactory>(); var content = responseMessageFactory.GetXmlByReponseMessage(responseMessage); #region Encrypt if (!string.IsNullOrWhiteSpace(content)) { var request = context.Request; var encryptType = request.QueryString["encrypt_type"]; if (encryptType != null) { var nonce = request.QueryString["nonce"]; var timestamp = request.QueryString["timestamp"]; var baseInfo = context.GetMessageHandlerBaseInfo(); var appId = baseInfo.AppId; var encodingAesKey = baseInfo.EncodingAesKey; var token = baseInfo.Token; var wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAesKey, appId); wxBizMsgCrypt.EncryptMsg(content, timestamp, nonce, ref content); } } #endregion Encrypt context.ResponseXml = content; } return Next.Invoke(context); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { var requestMessage = context.GetRequestMessage(); //得到消息的唯一标识。 var identity = GetMessageIdentity(requestMessage); try { #region 除无效的消息标识以节省资源 //Fixed issues: 集合已修改;可能无法执行枚举操作。 var repeatMessageList = MessageIdentityCache.Where(m => m.Value.AddSeconds(30) < DateTime.Now).ToList(); foreach (var msg in repeatMessageList) { DateTime value; MessageIdentityCache.TryRemove(msg.Key, out value); } #endregion 除无效的消息标识以节省资源 var cacheMessages = MessageIdentityCache.ToList(); //如果消息已经被标识为处理则跳过。 if (cacheMessages.Any(i => i.Key == identity)) { return(EmptyHandlerMiddleware.Instance.Invoke(context)); } //标识消息正在处理。 MessageIdentityCache.TryAdd(identity, DateTime.Now); } catch { return(EmptyHandlerMiddleware.Instance.Invoke(context)); } return(Next.Invoke(context)); }
/// <summary> /// 调用。 /// </summary> /// <param name="context">处理上下文。</param> /// <returns>任务。</returns> public override Task Invoke(IHandlerContext context) { _sessionCollection = context.GetDependencyResolver().GetService<IUserSessionCollection>(); var requestMessage = context.GetRequestMessage(); //设置用户会话。 context.SetSession(_sessionCollection.GetOrAdd(requestMessage.FromUserName)); return Next.Invoke(context); }