【问题标题】:msmq unhandled async exception in .net 4.5.net 4.5 中的 msmq 未处理的异步异常
【发布时间】:2016-07-07 00:31:10
【问题描述】:

我用 MSMQ 做了一些原型,但遇到了奇怪的行为。

我有 MSMQ 服务,当有消息进入队列时,它会向某些 Web 服务发送数据。我正在关闭 Web 服务并希望看到 System.ServiceModel.ServerTooBusyException,以便我的消息将返回到队列并稍后发送。 我在控制台应用程序中托管 MSMQ 服务。

现在,如果我将数据同步发送到 Web 服务,那么我会得到预期的行为。如果我异步发送数据,我的控制台应用程序会因未处理的异常而崩溃 System.ServiceModel.ServerTooBusyException

这是伪代码:

控制台

AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;

using (MSMQServiceHost serviceHost = new MSMQServiceHost(typeof(Service1)))
{
    serviceHost.Open();

     Console.WriteLine("Press any key to stop service...");
     Console.ReadKey(true);
}

private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
   MSMQ.Utils.LoggerHelper.GetLogger().ErrorFormat("CurrentDomainOnUnhandledException. {0}", unhandledExceptionEventArgs);
}

服务

[OperationBehavior(TransactionScopeRequired = true)]
public async  void GetData(int start, int end)
{
    try
    {
      using (WebServiceReference.Service1Client webClient = new WebServiceReference.Service1Client())
      {
        string data = await webClient.GetDataAsync(start);
        //string data = webClient.GetData(start);
        MSMQ.Utils.LoggerHelper.GetLogger().InfoFormat("Received info from web service. Data\t{0}", data);
    }
}
catch (Exception e)
{
    MSMQ.Utils.LoggerHelper.GetLogger().ErrorFormat("{1}\r\n{0}", e.Message, e.GetType().FullName);
    throw;
}

}

在同步调用的情况下,我会得到大量记录的错误,但应用程序不会崩溃。在异步调用的情况下,我会得到相同的记录错误列表以及相同数量的 CurrentDomainOnUnhandledException (System.ServiceModel.ServerTooBusyException)

mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ThrowAsync.AnonymousMethod__5(对象状态)未知 mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 未知 mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() 未知

我的 awaitable 是可观察的 - 我已经订阅了 TaskScheduler.UnobservedTaskException,但那里什么也没有。 在这种情况下如何正确使用异步,以便我从 APM 获得所有好处并且我的应用程序不会崩溃?

【问题讨论】:

    标签: c# exception async-await .net-4.5 msmq


    【解决方案1】:

    我明白了。

    问题在于我的服务操作中的 async 修饰符。正确版本:

    [OperationBehavior(TransactionScopeRequired = true)]
    public async Task GetData(int start, int end)
    {
        try
        {
            await Task.Factory.StartNew(() =>
            {
                using (WebServiceReference.Service1Client webClient = new WebServiceReference.Service1Client())
                {
                     Task<string> data = GetWebClientData(start, webClient);
                     MSMQ.Utils.LoggerHelper.GetLogger().InfoFormat("Received info from web service. Data\t{0}", data.Result);
                }
            }
      }
      catch (Exception e)
      {
          MSMQ.Utils.LoggerHelper.GetLogger().ErrorFormat("{1}\r\n{0}", e.Message, e.GetType().FullName);
         throw;
      }
    }
    
    private static async Task<string> GetWebClientData(int start, WebServiceReference.Service1Client webClient)
    {
        return await webClient.GetDataAsync(start);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      相关资源
      最近更新 更多