Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步操作完成,但结果不会发送到浏览器_C#_Asp.net Mvc 3_Asynccontroller - Fatal编程技术网

C# 异步操作完成,但结果不会发送到浏览器

C# 异步操作完成,但结果不会发送到浏览器,c#,asp.net-mvc-3,asynccontroller,C#,Asp.net Mvc 3,Asynccontroller,我想实现一个webchat 后端是一个双WCF通道。双通道在控制台或winforms中工作, 它实际上在网络上工作。我至少可以发送和接收消息 作为一个基地,我用 因此,异步操作完成 调试结果时,我看到消息已准备好发送到浏览器 [AsyncTimeout(ChatServer.MaxWaitSeconds * 1020)] // timeout is a bit longer than the internal wait public void IndexAsync() { ChatSessi

我想实现一个webchat

后端是一个双WCF通道。双通道在控制台或winforms中工作, 它实际上在网络上工作。我至少可以发送和接收消息

作为一个基地,我用 因此,异步操作完成

调试结果时,我看到消息已准备好发送到浏览器

[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);
  }
}
但实际上什么也没有发送

查看Fiddler,我看到了请求,但从未得到响应

[SessionState(SessionStateBehavior.ReadOnly)]  
public class ChatController : AsyncController
我还必须将SessionStateBehavior设置为只读,否则异步操作将阻塞整个页面

编辑: 以下是CheckFormMessageAsync:

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);
    }
  }
}
public void checkformessageasync(操作消息)
{
if(onMessages==null)
抛出新的ArgumentNullException(“onMessages”);
Task Task=Task.Factory.StartNew(状态=>
{
List msgs=新列表();
ManualResetEventSlim等待=新的ManualResetEventSlim(错误);
动作回调=状态为动作;
if(回调!=null)
{
IDisposable subscriber=m_messages.Subscribe(chatMessage=>
{
添加(聊天信息);
等等,Set();
});
成功;
使用(用户)
{
//等待新消息的最长时间(秒)
success=wait.wait(TimeSpan.FromSeconds(ChatServer.MaxWaitSeconds));
}
如果(成功)此.SafeCallOnMessages(回调,msgs);
否则为.SafeCallOnMessages(回调,null);
}        
},在讯息上);
}
私有void SafeCallOnMessages(操作消息、列表消息)
{
if(onMessages!=null)
{
如果(消息==null)
消息=新列表();
尝试
{
onMessages(消息);
}
捕获(例外情况除外)
{
this.Logger.ErrorException(“未能调用消息回调。”,ex);
}
}
}
这与引用的博客文章中的想法相同

编辑2:
顺便说一句,当没有收到任何消息时,等待超时开始起作用,响应返回。所以它似乎在某个地方崩溃了。知道如何记录这个吗?

我将jQUERY请求(参见原始博客文章)从post更改为GET。这就解决了问题。

您设置了一个断点,并验证它是否到达调用
this.AsyncManager.OutstandingOperations.Decrement()的代码?在
CheckFormMessageAsync
中发布相关代码,并在回调中添加日志记录。这与您的问题无关,但在
IndexAsync
的catch子句中,您应该减少异步操作计数器。如果您实际上没有对回调进行排队,那么它将永远不会被调用。或者,您可以选择仅在从
checkformessageasync
成功返回后增加它。谢谢。我在递增/递减调用前后添加了日志消息。这就是我得到的.WebProject.Controllers.ChatController |增量未完成操作WebProject.Controllers.ChatController |向sip发送消息:xxx@xxxx.extChatDAL |通知响应:WebProject.DataAccessLayer.UnifiedMessageGatewayService.ChatResponse ChatSession |收到消息。WebProject.Controllers.ChatController |递减未完成操作WebProject.Controllers.ChatController |异步回调已完成。WebProject.Controllers.ChatController异步请求已完成。信息数量:1