2013-01-02 14 views
27

मैंने एक कस्टम एएसपी.NET नियंत्रण लिखा है और मैंने इसे एसिंक लोड इवेंट हैंडलर के लिए अभी अपडेट किया है। करता पहले से ही <%@ Page Async="true" %> टैग हैकौन सा एएसपी.नेट जीवन चक्र घटनाएं एसिंक हो सकती हैं?

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

पेज: अब मैं इस त्रुटि हो रही है। इसलिए मैं इसे लेता हूं कि नियंत्रण में एसिंक लोड इवेंट हैंडलर नहीं हो सकते हैं।

एएसपी.NET वेबफॉर्म जीवनशैली में घटनाओं की एक व्यापक सूची कहां मिल सकती है जिसे एसिंक होने की अनुमति है?

+2

यह आलेख यह आपकी मदद कर सकता है: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx – Aristos

+1

धन्यवाद। निश्चित रूप से एक अच्छा पढ़ा, हालांकि यह मेरे प्रश्न का उत्तर नहीं देता है। –

+0

यह थोड़ा सा समझाता है, और एमएसडीएन की तुलना में बहुत अधिक उदाहरण कोड देता है, लेकिन फिर भी आपके प्रश्न का उत्तर नहीं देता है:/ http://www.asp.net/web-forms/tutorials/aspnet-45/ एसिंक्रोनस-विधियों-इन-एस्पनेट -45 – mirichan

उत्तर

28

डेमियन ASP.NET टीम की ओर से एडवर्ड्स इस जवाब दिया:

ASP.NET टीम से

Async void event handlers in web forms are only supported on certain events, as you've found, but are really only intended for simplistic tasks. We recommend using PageAsyncTask for any async work of any real complexity.

लेवी ब्रॉडरिक इस जवाब दिया:

Async events in web applications are inherently strange beasts. Async void is meant for a fire and forget programming model. This works in Windows UI applications since the application sticks around until the OS kills it, so whenever the async callback runs there is guaranteed to be a UI thread that it can interact with. In web applications, this model falls apart since requests are by definition transient. If the async callback happens to run after the request has finished, there is no guarantee that the data structures the callback needs to interact with are still in a good state. Thus why fire and forget (and async void) is inherently a bad idea in web applications.

That said, we do crazy gymnastics to try to make very simple things like Page_Load work, but the code to support this is extremely complicated and not well-tested for anything beyond basic scenarios. So if you need reliability I’d stick with RegisterAsyncTask.

तो मुझे लगता है मेरे सवाल का जवाब है: "यह गलत सवाल है।"

सही सवाल यह होगा कि "मैं अपने एएसपी.नेट वेब फॉर्म एप्लिकेशन में एसिंक कैसे होना चाहिए?" और इसका जवाब आपके aspx कोड-पीछे फाइल के अंदर इस डालने में है:

this.RegisterAsyncTask(new PageAsyncTask(async cancellationToken => { 
    var result = await SomeOperationAsync(cancellationToken); 
    // do something with result. 
})); 

यह वही चाल ASP.NET कस्टम नियंत्रण के अंदर काम करता है, बस के बजाय this.Page.RegisterAsyncTask का उपयोग करें।

1

यह पृष्ठ बताते हैं कि कैसे जीवन चक्र अतुल्यकालिक पृष्ठों में ईवेंट प्रबंधन ASP.NET 2.0 में तुल्यकालिक पृष्ठों के उन लोगों से अलग है (चित्र 2 विशेष रूप से उपयोगी है):

Wicked Code: Asynchronous Pages in ASP.NET 2.0

इस तो सवाल यह मिल सकता है उपयोग की (यह एक ही त्रुटि संदेश के बारे में बात):

async keyword and choice of the TaskScheduler

+0

धन्यवाद, @ लोकरिम। विशेष रूप से, उपयोगी बिट 'पेज। रजिस्ट्रेशन एसिंक टास्क' था, जो स्पष्ट रूप से मेरे एएसपी.NET कस्टम नियंत्रण को एसिंक ऑपरेशन करने के लिए अनुमति देता है, भले ही एएसपी.नेट मुझे मेरे कस्टम नियंत्रण में "एसिंक शून्य पेज_लोड" का उपयोग न करने दे । धन्यवाद! –

संबंधित मुद्दे