/// <summary> /// 消息发布线程准备就绪,开启业务工作线程 /// </summary> protected internal void Ready() { ServiceEventSource ses = this.Context.PublishEventSource; if (ses.EventWork != null) { Task.Factory.StartNew(() => { try { ses.EventWork(); Console.WriteLine("事件源对象的工作任务处理完成,即将停止事件处理。"); } catch (Exception ex) { this.publishResult = ServiceConst.CreateServiceErrorMessage(ex.Message); Console.WriteLine("事件源对象执行事件操作错误,即将停止事件处理!"); Program.Processer_ServiceErrorEvent(this, new ServiceErrorEventArgs(ex, "事件源对象执行事件操作错误")); } published = false; //事件推送线程收到信号,开始工作 base.SetPublishEvent(); System.Threading.Thread.Sleep(1000); ses.DeActive(); lastPublishTime = DateTime.Now; }); } }
private void ProcessServiceError(Exception ex, string errMessage = "") { //Console.WriteLine("执行服务方法错误:{0}", ex.Message); this.Response.Write(ServiceConst.CreateServiceErrorMessage(ex.Message)); this.Response.End(); this.OnServiceError(ex, string.Format( "执行服务方法错误:{0}\r\n源错误信息:{1},\r\n请求的Uri:\r\n{2},\r\n{3}:{4},{5}\r\n", errMessage, ex.Message, this.Request.ServiceUrl, this.Request.ClientIP, this.Request.ClientPort, this.Request.ClientIdentity) ); }
protected override void Process() { string message = this.Message; string identity = this.SubscriberInfo.Identity; string processMesssage = string.Empty; DateTime beginTime = DateTime.Now; processMesssage = string.Format("[{0}]正在处理服务请求--From: {1}:{2},Identity:{3}\r\n>>[PMID:{4}]{5}", beginTime.ToString("yyyy-MM-dd HH:mm:ss.fff"), this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, identity, this.SubscriberInfo.MessageID, this.Message.Length > 256 ? this.Message.Substring(0, 256) : this.Message); Console.WriteLine(processMesssage); int msgId = this.SubscriberInfo.MessageID;//执行完服务方法后,MessageID 可能被另外一个线程改变 //执行服务方法的时候,由服务方法指名是否需要维持会话状态 ServiceContext context = new ServiceContext(message); //如果当前请求是 发布-订阅模式,多个订阅端使用一个发布对象实例 if (context.Request.RequestModel == RequestModel.Publish || context.Request.RequestModel == RequestModel.ServiceEvent) { if (PublisherFactory.Instance.Contains(context)) //已经订阅过 { ServicePublisher publisher = PublisherFactory.Instance.GetPublisher(context); if (publisher.TaskIsRunning) { publisher.SubscriberInfoList.Add(this.SubscriberInfo); processMesssage = string.Format("\r\n[{0}]当前监听器已经加入消息发布线程, {1}:{2},Identity:{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, this.SubscriberInfo.Identity); Console.WriteLine(processMesssage); return; } } } context.Host = Program.Host; context.ServiceErrorEvent += new EventHandler <ServiceErrorEventArgs>(context_ServiceErrorEvent); context.Request.ClientIP = this.SubscriberInfo.FromIP; context.Request.ClientPort = this.SubscriberInfo.FromPort; context.Request.ClientIdentity = this.SubscriberInfo.Identity; context.User = MessageProcessBase.GetServiceIdentity(this.SubscriberInfo._innerListener); context.GetMessageFun = strPara => { MessageListener currLst = MessageCenter.Instance.GetListener(this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort); if (currLst == null) { throw new NullReferenceException("监听器不存在!"); } return(currLst.CallBackFunction(msgId, strPara)); }; context.PreGetMessageFun = strPara => { MessageListener currLst = MessageCenter.Instance.GetListener(this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort); if (currLst == null) { throw new NullReferenceException("监听器不存在!"); } return(currLst.PreCallBackFunction(msgId, strPara)); }; string result = string.Empty; bool noResult = false; context.InitRequestParameters(); if (!context.HasError) { //Console.WriteLine("Process Service begin..."); context.ProcessService(this.SubscriberInfo.SessionID); //Console.WriteLine("Process Service ok..."); result = context.Response.AllText; noResult = context.NoResultRecord(result); } else { result = ServiceConst.CreateServiceErrorMessage(context.ErrorMessage); //base.OnServiceError(context, new ServiceErrorEventArgs(context.ErrorMessage)); } DateTime endTime = DateTime.Now; processMesssage = string.Format("[{0}]请求处理完毕({1}ms)--To: {2}:{3},Identity:{4}\r\n>>[PMID:{5}]消息长度:{6} -------", endTime.ToString("yyyy-MM-dd HH:mm:ss.fff"), endTime.Subtract(beginTime).TotalMilliseconds, this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, identity, this.SubscriberInfo.MessageID, noResult ? "[Empty Result]" : result.Length.ToString("###,###") + "字节"); Console.WriteLine(processMesssage); //此处内容可能很大,不能全程输出 if (context.Response.ResultType == typeof(byte[])) { Console.WriteLine("[byte Content]"); } else { Console.WriteLine("result:{0}", result.Length > 100 ? result.Substring(0, 100) + " ..." : result); } MessageListener currLstn = MessageCenter.Instance.GetListener(this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort); if (currLstn == null) { processMesssage = "Error:监听器未找到,已取消发送消息。请求源:" + this.SubscriberInfo.Message; Console.WriteLine(processMesssage); return; } if (context.Request.RequestModel == RequestModel.GetService) { //对于请求-响应模式,处理完服务以后,始终会回调客户端的方法(如果提供的话) if (MessageCenter.Instance.ResponseMessage(currLstn, msgId, result)) { Console.WriteLine("Reponse Message OK."); } } else { //订阅模式,仅在服务处理有结果的情况下,才给客户端发布数据。 if (!noResult) { if (MessageCenter.Instance.NotifyOneMessage(currLstn, msgId, result)) { Console.WriteLine("Publish Message OK."); } else { Console.WriteLine("Publish no result."); } } if (!context.HasError) { StartPublishWorker(context);//把Host传递进去 processMesssage = string.Format("\r\n[{0}]当前监听器已经加入工作线程, {1}:{2},Identity:{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, this.SubscriberInfo.Identity); Console.WriteLine(processMesssage); } } }