Asynchronousoperationiscompleted,buttheresultwillnotbesenttothebrowser我想实现在线聊天。后端是双WCF通道。双通道在控制台或winform中工作,它实际上在网络上工作。我至少可以发送和接收消息。作为基础,我使用了这篇博文并完成了异步操作。当我调试结果时,我看到消息都已准备好发送到浏览器。[AsyncTimeout(ChatServer.MaxWaitSeconds*1020)]//超时时间比内部等待长一点publicvoidIndexAsync(){ChatSessionchatSession=this.GetChatSession();if(chatSession!=null){this.AsyncManager.OutstandingOperations.Increment();尝试{chatSession.CheckForMessagesAsync(msgs=>{this.AsyncManager.Parameters["response"]=newChatResponse{Messages=msgs};this.AsyncManager.OutstandingOperations.Decrement();});}catch(Exceptionex){Logger.ErrorException("无法检查消息。",ex);}}}publicActionResultIndexCompleted(ChatResponseresponse){try{if(response!=null){Logger.Debug("异步请求完成。消息数:{0}",response.Messages.Count);}JsonResultretval=this.Json(response);Logger.Debug("渲染响应:{0}",retval.);返回retval;}catch(Exceptionex){Logger.ErrorException("渲染响应失败。",ex);返回this.Json(null);}}但现实上没有发送任何内容检查Fiddler,我看到了请求,但我从未收到回复[SessionState(SessionStateBehavior.ReadOnly)]publicclassChatController:AsyncController我还必须将SessionStateBehaviour设置为Readonly,否则异步操作会阻塞整个页面。编辑:这是CheckForMessagesAsync:publicvoidCheckForMessagesAsync(ActiononMessages){if(onMessages==null)thrownewArgumentNullException("onMessages");任务task=Task.Factory.StartNew(state=>{Listmsgs=newList();ManualResetEventSlimwait=newManualResetEventSlim(false);Action
callback=stateasAction
;if(callback!=null){IDisposablesubscriber=m_messages.Subscribe(chatMessage=>{msgs.Add(chatMessage);wait.Set();});boolsuccess;using(subscriber){//等待新消息的最大秒数success=wait.Wait(TimeSpan.FromSeconds(ChatServer.MaxWaitSeconds));}if(成功)this.SafeCallOnMessages(callback,msgs);elsethis.SafeCallOnMessages(callback,null);}},onMessages);}privatevoidSafeCallOnMessages(Action
onMessages,Listmessages){if(onMessages!=null){if(messages==null)messages=newList();尝试{onMessages(消息);}赶上(异常前){this.Logger.ErrorException("调用OnMessages回调失败。",ex);这与提到的博客文章EDIT2中的想法相同:顺便说一下,当什么都没有收到时,等待超时开始工作,响应返回所以它似乎在某处崩溃。任何想法如何记录这个?我将jQUERY请求(请参阅原始博客文章)从POST更改为GET。这解决了它。以上就是C#学习教程:异步操作完成,但不会将结果发送到浏览器分享所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
