gpt4 book ai didi

ASP.NET:是否可以在 Global.asax 中调用异步任务?

转载 作者:行者123 更新时间:2023-12-03 21:19:23 24 4
gpt4 key购买 nike

我需要在 Global.asax 中对我的数据库调用一些异步操作。
例如在 Application_AuthenticateRequest 我需要针对数据库验证用户
异步任务可以吗?

最佳答案

现在有一种更简单的方法可以做到这一点:

    public MvcApplication()
{
var wrapper = new EventHandlerTaskAsyncHelper(DoAsyncWork);
this.AddOnAuthenticateRequestAsync(wrapper.BeginEventHandler, wrapper.EndEventHandler);
}

private async Task DoAsyncWork(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
var ctx = app.Context;

...
await doSomethingAsync();
}

通过这种方法,您可以使用 async 关键字定义一个方法,并使用“EventHandlerTaskAsyncHelper”类包装该方法以生成传递到 AddOnAuthenticateRequestAsync 调用的 BeginEventHandler 和 EndEventHandler 方法。

关于ASP.NET:是否可以在 Global.asax 中调用异步任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11098575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com