更新时间:2023-09-21 11:45:58
现在有一种更简便的方法:
There's an easier way to do this now:
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'类包装该方法以生成BeginEventHandler和EndEventHandler方法,以传递给AddOnAuthenticateRequestAsync调用.
With this approach, you define a method using the async keyword and wrap that method using the 'EventHandlerTaskAsyncHelper' class to generate BeginEventHandler and EndEventHandler methods to pass into the AddOnAuthenticateRequestAsync call.