I have an ASP.NET app that gets its data form an external service, via HttpWebRequest. Currently the request is made on the Page_Load event. After that event finishes I have my objects in the page and I output their data in the HTML. Sometimes the site hangs and throws timeout exceptions. Should I move the web request to an开发者_JAVA百科 event prior to Page_Load ?
Well, I think it depends on what information you need for the page.
Check this page for when data is available in the page event model and see when all of the data you need for the request is available. Also, when you get the response, what are you going to do with it? Put it in session? Just write out HTML?
You're probably better off handling the timeout gracefully rather than burning a web server thread waiting for an external call to return.
You may also want to consider an async request for this data. James' response is a great suggestion. You may want to check out the link he included.
If your looking to avoid the loading and the site can function without it, you might be better served making it an AsynchHttpWebRequest.
See this question for more details
精彩评论