简体   繁体   English

RavenDB LoadAsync。 无法捕捉异常

[英]RavenDB LoadAsync. Can't catch exception

I have a simple async method that loads an entity asynchronously using LoadAsync. 我有一个简单的异步方法,使用LoadAsync异步加载实体。 I can't catch exceptions thrown by the async call. 我无法捕获异步调用抛出的异常。 Debugging just leads to 'AggregateException was unhandled by user code.' 调试只会导致'AggregateException未被用户代码处理。' I'm using RavenDB Client 2.0.2375. 我正在使用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. 用户代码未处理System.AggregateException错误HResult = -2146233088消息=发生了一个或多个错误。 Source=mscorlib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.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(Func 1 getResponse) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\HttpJsonRequest.cs:line 351 at Raven.Client.Connection.HttpJsonRequest.<InternalReadResponseStringAsync>b__8(Task 1 task) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\HttpJsonRequest.cs:line 128 at 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.TaskFactory Source = mscorlib StackTrace:位于System.Threading.Tasks.Task的System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions),位于Raven的1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.get_Result()的1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 。 Client.Connection.HttpJsonRequest。<> c_ DisplayClassc.b _9()位于c:\\ Builds \\ RavenDB-Stable \\ Raven.Client.Lightweight \\ Connection \\ HttpJsonRequest.cs:第128行,位于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(Task 1任务)在c:\\构建\\ RavenDB-Stable \\ Raven.Client.Lightweight \\ Connection \\ HttpJsonRequest.cs:System.Threading.Tasks.ContinuationResultTaskFromResultTask的第128行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.TaskFactory 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.TaskFactory 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.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar, Func 2 endFunction, Action 1 endAction, Task`1 promise, Boolean requiresSynchronization) InnerException: 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.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar,Func 2 endFunction, Action 1 endAction,Task`1 promise,Boolean requiresSynchronization)InnerException:

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 . 在RavenDB中,如果尝试加载不存在的文档,则LoadLoadAsync方法将返回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. 在进行调试时,您会发现当HTTP响应返回404时,会引发WebException 。处理该异常。 You should see it in the output window as a "First Chance Exception". 您应该在输出窗口中将其视为“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. 这肯定会引发例外。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM