简体   繁体   English

异步操作完成,但结果未发送到浏览器

[英]Async operation completes, but result is not send to browser

I want to implement a webchat.我想实现一个网络聊天。

The backend is a dual WCF channel.后端是双 WCF 通道。 Dual channel works in the console or winforms, and it actually works on the web.双通道在控制台或winforms中工作,实际上在web上工作。 I can at least send and receive messages.我至少可以发送和接收消息。

As a base I used this blog post so, the async operation completes.作为基础,我使用了这篇博文,因此异步操作完成。

When i debug the result, I see that the messages are all ready to send to the browser.当我调试结果时,我看到消息都准备好发送到浏览器了。

[AsyncTimeout(ChatServer.MaxWaitSeconds * 1020)] // timeout is a bit longer than the internal wait
public void IndexAsync()
{
  ChatSession chatSession = this.GetChatSession();
  if (chatSession != null)
  {
    this.AsyncManager.OutstandingOperations.Increment();
    try
    {
      chatSession.CheckForMessagesAsync(msgs =>
      {
        this.AsyncManager.Parameters["response"] = new ChatResponse { Messages = msgs };
        this.AsyncManager.OutstandingOperations.Decrement();
      });
    }
    catch (Exception ex)
    {
      Logger.ErrorException("Failed to check for messages.", ex);
    }
  }
}

public ActionResult IndexCompleted(ChatResponse response)
{
  try
  {
    if (response != null)
    {
      Logger.Debug("Async request completed. Number of messages: {0}", response.Messages.Count);
    }
    JsonResult retval = this.Json(response);
    Logger.Debug("Rendered response: {0}", retval.);
    return retval;
  }
  catch (Exception ex)
  {
    Logger.ErrorException("Failed rendering the response.", ex);
    return this.Json(null);
  }
}

But nothing is actually sent.但实际上没有发送任何内容。

Checking Fiddler, I see the request but I never get a response.检查提琴手,我看到了请求,但我从来没有得到回应。

[SessionState(SessionStateBehavior.ReadOnly)]  
public class ChatController : AsyncController

I also had to set the SessionStateBehaviour to Readonly, otherwise the async operation would block the whole page.我还必须将 SessionStateBehaviour 设置为只读,否则异步操作会阻塞整个页面。

EDIT: Here is the CheckForMessagesAsync:编辑:这是 CheckForMessagesAsync:

public void CheckForMessagesAsync(Action<List<ChatMessage>> onMessages)
{
  if (onMessages == null)
    throw new ArgumentNullException("onMessages");

  Task task = Task.Factory.StartNew(state =>       
  {
    List<ChatMessage> msgs = new List<ChatMessage>();
    ManualResetEventSlim wait = new ManualResetEventSlim(false);
    Action<List<ChatMessage>> callback = state as Action<List<ChatMessage>>;
    if (callback != null)
    {
      IDisposable subscriber = m_messages.Subscribe(chatMessage =>
      {
        msgs.Add(chatMessage);
        wait.Set();
      });
      bool success;
      using (subscriber)
      {
        // Wait for the max seconds for a new msg
        success = wait.Wait(TimeSpan.FromSeconds(ChatServer.MaxWaitSeconds));              
      }
      if (success) this.SafeCallOnMessages(callback, msgs);
      else this.SafeCallOnMessages(callback, null);
    }        
  }, onMessages);
}
private void SafeCallOnMessages(Action<List<ChatMessage>> onMessages, List<ChatMessage> messages)
{
  if (onMessages != null)
  {
    if (messages == null)
      messages = new List<ChatMessage>();
    try
    {
      onMessages(messages);
    }
    catch (Exception ex)
    {
      this.Logger.ErrorException("Failed to call OnMessages callback.", ex);
    }
  }
}

it`s the same idea as the in the refered blog post这与引用的博客文章中的想法相同

EDIT2: Btw, when nothing is received, so the wait timeout comes into play, the reponse returns. EDIT2:顺便说一句,当什么都没有收到时,等待超时开始起作用,响应返回。 so it seems to crash somewhere.所以它似乎在某个地方崩溃了。 any idea how to log this?知道如何记录吗?

I changed the jQUERY request (see original blog post) from POST to GET.我将 jQUERY 请求(参见原始博客文章)从 POST 更改为 GET。 That fixes it.这解决了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在异步操作完成之前从方法返回的不良做法? - Bad practice to return from method before async operation completes? 将异步操作结果返回到原始线程 - Return async operation result to the original thread 为什么可观察的“缓存”是异步操作的结果? - Why is my Observable 'caching' the result of an Async operation? 无法使用Async DocumentDB操作CreateDocumentAsync的结果 - Unable to use result of Async DocumentDB operation CreateDocumentAsync 通过Ajax调用MVC异步操作,启动任务,在任务完成之前发送ajax响应? - Call MVC async action through Ajax, start task, send the ajax response before the task completes? 如果操作首先完成,则使用RegisterWaitForSingleObject - Using RegisterWaitForSingleObject if operation completes first 创建一个IObservable并立即返回缓存的异步操作的结果 - Create an IObservable and return the result of a cached async operation immediately 如何使等待异步任务获得结果以进一步操作xamarin表格? - How to make wait async task for result for further operation xamarin forms? 异步方法中的同步操作 - 如何修复输出不正确和完整的结果? - Sync operation in async method - how to fix result not outputted correct and complete? Task.Result/wait(..) 如果等待的任务链具有“解包”任务,则无限期等待,而如果使用“async/await”则成功完成 - Task.Result/wait(..) is indefinitely waits if waited on chain of tasks have 'unwrapped' task, whereas successfully completes if 'async/await' is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM