RavenDB LoadAsync.无法捕获异常

RavenDB LoadAsync. Can't catch exception
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我有一个简单的异步方法,该方法使用LoadAsync异步加载实体.我无法捕获异步调用引发的异常.调试只会导致"用户代码未处理AggregateException".我正在使用RavenDB Client 2.0.2375.

如何捕获这些异常?

方法如下:

 private async Task<Dummy> GetDummyAsync(string id)
    {
        using (var session = docStore.OpenAsyncSession())
        {
            try
            {
                var dummy = await session.LoadAsync<Dummy>(id);
                return dummy;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

编辑

这是例外:

用户代码HResult = -2146233088未处理System.AggregateException,消息=发生一个或多个错误.Source = mscorlib StackTrace:位于System.Threading.Tasks.Task.ThrowIfExceptional(Boolean包含System.Threading.Tasks.Task的布尔值1.get_Result 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task()位于Raven.Client.Connection.HttpJsonRequest.<> c_ DisplayClassc.b _9()在c:\ Builds \ RavenDB-Stable \ Raven中c:\ Builds \ RavenDB-Stable \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func 1 getResponse) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 351 at Raven.Client.Connection.HttpJsonRequest.<InternalReadResponseStringAsync>b__8(Task1 task)的第128行.Client.Lightweight \ Connection \ HttpJsonRequest.cs:System.Threading.Tasks.ContinuationResultTaskFromResultTask 2.InnerInvoke() at System.Threading.Tasks.Task.Execute() InnerException: System.Net.WebException HResult=-2146233079 Message=The remote server returned an error: (404) Not Found. Source=System StackTrace: at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1的第128行.FromAsyncCoreLogic(IAsyncResult iar,Func2 endFunction, Action1个endAction,任务为1个承诺,布尔值requireSynchronization)InnerException:

速聊1:
速聊2:
附加调试器的情况下,是否会看到相同的行为?
速聊3:
也许引发了异常OpenAsyncSession?什么是堆栈跟踪?里面有什么InnerExceptions?
速聊4:
例外情况会根据我在做什么而改变.如果我要求的文件无效,它们就是HTTP 404例外.我可以使用UnhandledExceptionTrapper捕获异常,但这实际上不是我想要的.也许我误会了一些东西,但是在凌晨4点,我开始认为乌鸦里有虫子.我明天再深入研究.
速聊5:
内部异常无关紧要.为了进行测试,我只是加载了一个不存在的文档,内部异常是System.Net.WebException:"Message =远程服务器返回了错误:(404)Not Found."
解决过程1

您看到的行为是有效的,并不是因为您无法捕获异常.这是因为已经为您捕获了异常.

在RavenDB中,如果尝试加载不存在的文档,则LoadorLoadAsync方法将返回null.他们不会抛出异常.

在调试时,您会看到WebExceptionHTTP响应以404返回时,在幕后抛出了a .该异常已得到处理.您应该在输出窗口中将其视为"第一次机会异常".

这都是正常现象.如果要检查是否可以捕获异常,请尝试一些实际上会引发异常的事情.可能在尝试将一种类型的文档转换为另一种类型时加载了该类型的文档.那肯定会引发例外.

I have a simple async method that loads an entity asynchronously using LoadAsync. I can't catch exceptions thrown by the async call. Debugging just leads to 'AggregateException was unhandled by user code.' I'm using RavenDB Client 2.0.2375.

How can I catch these exceptions?

Here's the method:

 private async Task<Dummy> GetDummyAsync(string id)
    {
        using (var session = docStore.OpenAsyncSession())
        {
            try
            {
                var dummy = await session.LoadAsync<Dummy>(id);
                return dummy;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

Edit

Here's the exception:

System.AggregateException was unhandled by user code HResult=-2146233088 Message=One or more errors occurred. Source=mscorlib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at Raven.Client.Connection.HttpJsonRequest.<>c_DisplayClassc.b_9() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 128 at Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func1 getResponse) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 351 at Raven.Client.Connection.HttpJsonRequest.<InternalReadResponseStringAsync>b__8(Task1 task) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 128 at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.Tasks.Task.Execute() InnerException: System.Net.WebException HResult=-2146233079 Message=The remote server returned an error: (404) Not Found. Source=System StackTrace: at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task`1 promise, Boolean requiresSynchronization) InnerException:

Talk1:
This will probably help you find the solution: interact-sw.co.uk/iangblog/2010/11/01/csharp5-async-exceptions
Talk2:
Do you see same behavior without attaching debugger ?
Talk3:
Perhaps exception is thrown by OpenAsyncSession? What is stack trace? What is in the InnerExceptions ?
Talk4:
The exceptions change depending on what I'm doing. If I request an invalid document they're HTTP 404 exceptions. I can catch the exceptions with a UnhandledExceptionTrapper but that's really not what I want to do. Maybe I'm misunderstanding something but at 4 in the morning I'm starting to think there's a bug in raven. I'll dig into this more tomorrow.
Talk5:
The inner exception doesn't matter. For testing I just load a document that doesn't exist and the inner exception is a System.Net.WebException: " Message=The remote server returned an error: (404) Not Found."
Solutions1

The behavior your seeing is valid, and is not because you couldn't catch the exception. It's because the exception was already caught for you.

In RavenDB, if you try to load a document that doesn't exist, the Load or LoadAsync methods will return null. They will not throw an exception.

When you are debugging, you are seeing that under the hood a WebException is thrown when the HTTP response comes back as 404. That exception is handled. You should see it in the output window as a "First Chance Exception".

This is all normal behavior. If you want to check that you can catch exceptions, try something that will actually throw an exception. Perhaps load a document of one type while trying to cast it into another. That will certainly throw an exception.

转载于:https://stackoverflow.com/questions/17361752/ravendb-loadasync-cant-catch-exception

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入