如何在Startup.Configure中处理异步操作? [英] How do I handle async operations in Startup.Configure?

查看:117
本文介绍了如何在Startup.Configure中处理异步操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET 5应用程序中,我想将Azure中的某些数据加载到Startup.Configure方法中的缓存中. Azure SDK仅公开异步方法.通常,调用异步方法是通过异步方法内部的await完成的,如下所示:

In my ASP.NET 5 app, I want to load some data from Azure into a cache inside my Startup.Configure method. The Azure SDK exposes async methods exclusively. Typically, calling an async method is done via await inside an async method, like this:

public async Task Configure(IApplicationBuilder app, IMemoryCache cache)
{
    Data dataToCache = await DataSource.LoadDataAsync();
    cache.Set("somekey", dataToCache);

    // remainder of Configure method omitted for clarity
}

但是,ASP.NET 5要求Configure方法返回void.我可以使用异步无效方法,但我的理解是异步无效方法仅应用于事件处理程序(按照

However, ASP.NET 5 requires that the Configure method returns void. I could use an async void method, but my understanding is that async void methods are only supposed to be used for event handlers (as per https://msdn.microsoft.com/en-us/magazine/jj991977.aspx among many others).

我当时认为更好的方法是不等待而调用async函数,对返回的Task调用Wait,然后通过Task.Results属性缓存结果,如下所示:

I was thinking that a better way to do this would be to call the async function without await, call Wait on the returned Task, then cache the results via the Task.Results property, like this:

public void Configure(IApplicationBuilder app, IMemoryCache cache)
{
    Task<Data> loadDataTask = DataSource.LoadDataAsync();
    loadDataTask.Wait();
    cache.Set("somekey", loadDataTask.Result);

    // remainder of Configure method omitted for clarity
}

Stephen Walther在博客帖子今年早些时候.但是,从该职位尚不清楚这是否可以接受.是吗?

Stephen Walther used a similar approach in a blog post earlier this year. However, it's unclear from that post if this is considered an acceptable practice. Is it?

如果这被认为是可接受的做法,那么我需要什么-如果有的话-错误处理?我的理解是Task.Wait()将重新引发异步操作引发的所有异常,而我没有提供任何取消异步操作的机制.仅仅调用Task.Wait()就足够了吗?

If this is considered an acceptable practice, what - if any - error handling do I need? My understanding is that Task.Wait() will re-throw any exceptions thrown by the async operation and I haven't provided any mechanism to cancel the async operation. Is simply calling Task.Wait() sufficient?

推荐答案

您链接到的博客中的示例代码仅使用sync-over-async来用示例数据填充数据库;该调用在生产应用中将不存在.

The example code in the blog you linked to was only using sync-over-async to populate a database with example data; that call wouldn't exist in a production app.

首先,我想说的是,如果您真的需要Configure异步,那么您应该向ASP.NET团队提出一个问题,以使其受到关注.对于他们而言,此时(即在发布之前)添加对ConfigureAsync的支持并不是很困难.

First, I'd say that if you truly need Configure to be asynchronous, then you should raise an issue with the ASP.NET team so it's on their radar. It would not be too difficult for them to add support for a ConfigureAsync at this point (that is, before release).

第二,您有两种解决问题的方法.您可以使用task.Wait(或者更好的是,task.GetAwaiter().GetResult(),如果确实发生错误,则可以避免使用AggregateException包装器).或者,您可以缓存 task 而不是任务的 result (如果IMemoryCache更像是一个字典,而不是一些奇怪的serialize-into-binary-array,则可以工作)内存中的东西-我在找你,ASP.NET的早期版本.

Second, you've got a couple of approaches to the problem. You could use task.Wait (or better yet, task.GetAwaiter().GetResult(), which avoids the AggregateException wrapper if an error does occur). Or, you could cache the task rather than the result of the task (which works if IMemoryCache is more of a dictionary than some weird serialize-into-binary-array-in-memory thing - I'm looking at you, previous versions of ASP.NET).

如果这被认为是可以接受的做法,那么我需要什么-如果有的话-错误处理?

If this is considered an acceptable practice, what - if any - error handling do I need?

使用GetAwaiter().GetResult()会导致异常(如果有)从Configure传播出去.我不确定如果配置应用程序失败,ASP.NET将如何响应.

Using GetAwaiter().GetResult() would cause the exception (if any) to propagate out of Configure. I'm not sure how ASP.NET would respond would be if configuring the application failed, though.

我没有提供任何取消异步操作的机制.

I haven't provided any mechanism to cancel the async operation.

我不确定您如何取消" 应用程序的设置,因此我不必担心它的这一部分.

I'm not sure how you can "cancel" the setup of an application, so I wouldn't worry about that part of it.

这篇关于如何在Startup.Configure中处理异步操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆