I need to call some async operations on my DB in Global.asax. for example in Application_AuthenticateRequest I need to Authenticate user against DB Is it possible with async Tasks?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
- How to dynamically load partial view Via jquery aj
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
- How do do an async ServiceController.WaitForStatus
You have to add the async version of the AuthenticateRequest yourself. Using the following code:
The issue is then, how to implement
BeginAuthenticateRequest
andEndAuthenticateRequest
using the new async/await features of C#. First, let's get our async version of AuthenticateRequest out of the way:What we need to do next is come up with an implementation of BeginAuthenticateRequest and EndAuthenticateRequest. I followed a blog post, but derived my own implementation:
You can read the entire linked article to see how it works, but basically
IAsyncResult
is implemented by Task, so all you have to do is call the callback when done.The last bit is dead easy:
I didn't find a way to using the new C# keyword async and await, but we can still using APM pattern to use async operations in Global.asax because it implemented IHttpAsyncHandler interface. Here is a small code to demo async, here I use a WebRequst as example, in your case please use database operation instead.
There's an easier way to do this now:
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.