为什么我的asp.net应用程序抛出ThreadAbortException?

时间:2021-07-11 20:58:20

self explanatory question.

自我解释的问题。

Why does this thing bubble into my try catch's even when nothing is wrong?

即使没有任何问题,为什么这个东西会冒泡进入我的尝试捕获?

Why is it showing up in my log, hundreds of times?

为什么它出现在我的日志中,数百次?

I know its a newb question, but if this site is gonna get search ranking and draw in newbs we have to ask them

我知道这是一个新问题,但如果这个网站要获得搜索排名并用新手绘制,我们就要问他们

5 个解决方案

#1


19  

This is probably coming from a Response.Redirect call. Check this link for an explanation:

这可能来自Response.Redirect调用。请查看此链接以获取解释:

http://dotnet.org.za/armand/archive/2004/11/16/7088.aspx

http://dotnet.org.za/armand/archive/2004/11/16/7088.aspx

(In most cases, calling Response.Redirect(url, false) fixes the problem)

(在大多数情况下,调用Response.Redirect(url,false)修复了问题)

#2


7  

The most common reason for a ThreadAbortException is calling Response.End, Response.Redirect, or Server.Transfer. Microsoft has published some suggested functions that should be used in stead of those functions.

ThreadAbortException的最常见原因是调用Response.End,Response.Redirect或Server.Transfer。 Microsoft已发布了一些建议的功能,应该使用这些功能来代替这些功能。

#3


1  

As others have said, it occurs when you call Response.End() (which occurs when you call Response.Redirect without passing false as the second parameter). This is working as designed; typically, if you call Response.Redirect, you want the redirect to happen immediately. See this for more information:

正如其他人所说,当你调用Response.End()时会发生这种情况(当你调用Response.Redirect而不传递false作为第二个参数时会发生)。这是按设计工作的;通常,如果您调用Response.Redirect,则希望重定向立即发生。有关更多信息,请参阅此

Response.Redirect and the ThreadAbortException

Response.Redirect和ThreadAbortException

#4


0  

Knowing that there are (at least) three APIs that internally use Thread.Abort, I'd like to answer in more practical terms, how to work out what to do about it.

知道(至少)有三个内部使用Thread.Abort的API,我想更实际地回答一下,如何解决该怎么做。

For us, this error started being logged all-of-a-sudden. What changed? We fixed a bug in some database procedure that was dealing with sitemaps.

对我们来说,这个错误开始被突然记录下来。改变了什么?我们修复了一些处理站点地图的数据库程序中的错误。

The log4net logs showed the X-Forwarded-For header (we're behind an NLB) was Googlebot's IP address, 66.249.78.x which bolstered my theory about the sitemap change leading to Google crawling our site more aggressively looking for images.

log4net日志显示X-Forwarded-For标题(我们落后于NLB)是Googlebot的IP地址66.249.78.x,这加强了我对网站地图变更的理论,导致谷歌更积极地寻找图像。

The first thing was to find out why only the Googlebot was able to cause this problem. No other client was triggering whatever code path uses Response.Redirect, or whatever.

首先要找出为什么只有Googlebot能够导致这个问题。没有其他客户端触发任何代码路径使用Response.Redirect,或其他什么。

So in the HttpApplication.Error handler, I added some code to log extra detailed output with all headers, and most data in the HttpResponse and HttpContext spewed to log.

所以在HttpApplication.Error处理程序中,我添加了一些代码来记录所有标题的额外详细输出,并且HttpResponse和HttpContext中的大多数数据都用于记录。

This let me see that the problem was that Googlebot is using an iPhone user agent string and armed with that, I was able to search the codebase for "iPhone" and come up with:

这让我看到问题是Googlebot正在使用iPhone用户代理字符串并且配备了它,我能够在代码库中搜索“iPhone”并提出:

private void CheckIPhoneAccess() { ... }

And that uses a Redirect.

这使用了重定向。

What to do about it?

该怎么办?

Well, for this aged codebase, it's not worth retro-patching all the Response.Redirect calls, so I'm going to lower the logging level for ThreadAbortException for the application.

好吧,对于这个老化的代码库,不值得对所有Response.Redirect调用进行逆向修补,因此我将降低应用程序的ThreadAbortException的日志记录级别。

I will change the behaviour for Googlebot's mobile crawler, that would not lead to 'lies' about what our site serves to mobiles since it only redirects on the first hit, subsequently it reads a cookie and shows the image. Googlebot does not seem to cache that cookie.

我将更改Googlebot移动抓取工具的行为,这不会导致我们网站为移动设备提供的“谎言”,因为它只会在第一次点击时重定向,随后会读取Cookie并显示图片。 Googlebot似乎没有缓存该Cookie。

It's not perfect, but the site is due to be rebuilt. probably by another team using Scala or something, so in practical terms, I think this is a good choice. I'll add comments and may revisit the issue later, build a Response.SafeRedirect extension that encapsulates this advice:

它并不完美,但该网站将被重建。可能是另一个团队使用Scala或其他东西,所以实际上,我认为这是一个不错的选择。我将添加注释,稍后可能会重新审视该问题,构建一个Response.SafeRedirect扩展,它封装了这个建议:

Why Response.Redirect causes System.Threading.ThreadAbortException?

为什么Response.Redirect导致System.Threading.ThreadAbortException?

Luke

卢克

#5


0  

The reason of why Response.Redirect will give this exception is asp.net internally implement this API with Thread.Abort(). When this method is called, a special ThreadAbortException is thrown.This exception wont be swallowed by any catch block. It will be re thrown at the end of each catch block.

Response.Redirect为什么会给出这个异常的原因是asp.net在内部用Thread.Abort()实现了这个API。调用此方法时,将抛出一个特殊的ThreadAbortException。此异常不会被任何catch块吞噬。它将在每个catch块的末尾重新抛出。

#1


19  

This is probably coming from a Response.Redirect call. Check this link for an explanation:

这可能来自Response.Redirect调用。请查看此链接以获取解释:

http://dotnet.org.za/armand/archive/2004/11/16/7088.aspx

http://dotnet.org.za/armand/archive/2004/11/16/7088.aspx

(In most cases, calling Response.Redirect(url, false) fixes the problem)

(在大多数情况下,调用Response.Redirect(url,false)修复了问题)

#2


7  

The most common reason for a ThreadAbortException is calling Response.End, Response.Redirect, or Server.Transfer. Microsoft has published some suggested functions that should be used in stead of those functions.

ThreadAbortException的最常见原因是调用Response.End,Response.Redirect或Server.Transfer。 Microsoft已发布了一些建议的功能,应该使用这些功能来代替这些功能。

#3


1  

As others have said, it occurs when you call Response.End() (which occurs when you call Response.Redirect without passing false as the second parameter). This is working as designed; typically, if you call Response.Redirect, you want the redirect to happen immediately. See this for more information:

正如其他人所说,当你调用Response.End()时会发生这种情况(当你调用Response.Redirect而不传递false作为第二个参数时会发生)。这是按设计工作的;通常,如果您调用Response.Redirect,则希望重定向立即发生。有关更多信息,请参阅此

Response.Redirect and the ThreadAbortException

Response.Redirect和ThreadAbortException

#4


0  

Knowing that there are (at least) three APIs that internally use Thread.Abort, I'd like to answer in more practical terms, how to work out what to do about it.

知道(至少)有三个内部使用Thread.Abort的API,我想更实际地回答一下,如何解决该怎么做。

For us, this error started being logged all-of-a-sudden. What changed? We fixed a bug in some database procedure that was dealing with sitemaps.

对我们来说,这个错误开始被突然记录下来。改变了什么?我们修复了一些处理站点地图的数据库程序中的错误。

The log4net logs showed the X-Forwarded-For header (we're behind an NLB) was Googlebot's IP address, 66.249.78.x which bolstered my theory about the sitemap change leading to Google crawling our site more aggressively looking for images.

log4net日志显示X-Forwarded-For标题(我们落后于NLB)是Googlebot的IP地址66.249.78.x,这加强了我对网站地图变更的理论,导致谷歌更积极地寻找图像。

The first thing was to find out why only the Googlebot was able to cause this problem. No other client was triggering whatever code path uses Response.Redirect, or whatever.

首先要找出为什么只有Googlebot能够导致这个问题。没有其他客户端触发任何代码路径使用Response.Redirect,或其他什么。

So in the HttpApplication.Error handler, I added some code to log extra detailed output with all headers, and most data in the HttpResponse and HttpContext spewed to log.

所以在HttpApplication.Error处理程序中,我添加了一些代码来记录所有标题的额外详细输出,并且HttpResponse和HttpContext中的大多数数据都用于记录。

This let me see that the problem was that Googlebot is using an iPhone user agent string and armed with that, I was able to search the codebase for "iPhone" and come up with:

这让我看到问题是Googlebot正在使用iPhone用户代理字符串并且配备了它,我能够在代码库中搜索“iPhone”并提出:

private void CheckIPhoneAccess() { ... }

And that uses a Redirect.

这使用了重定向。

What to do about it?

该怎么办?

Well, for this aged codebase, it's not worth retro-patching all the Response.Redirect calls, so I'm going to lower the logging level for ThreadAbortException for the application.

好吧,对于这个老化的代码库,不值得对所有Response.Redirect调用进行逆向修补,因此我将降低应用程序的ThreadAbortException的日志记录级别。

I will change the behaviour for Googlebot's mobile crawler, that would not lead to 'lies' about what our site serves to mobiles since it only redirects on the first hit, subsequently it reads a cookie and shows the image. Googlebot does not seem to cache that cookie.

我将更改Googlebot移动抓取工具的行为,这不会导致我们网站为移动设备提供的“谎言”,因为它只会在第一次点击时重定向,随后会读取Cookie并显示图片。 Googlebot似乎没有缓存该Cookie。

It's not perfect, but the site is due to be rebuilt. probably by another team using Scala or something, so in practical terms, I think this is a good choice. I'll add comments and may revisit the issue later, build a Response.SafeRedirect extension that encapsulates this advice:

它并不完美,但该网站将被重建。可能是另一个团队使用Scala或其他东西,所以实际上,我认为这是一个不错的选择。我将添加注释,稍后可能会重新审视该问题,构建一个Response.SafeRedirect扩展,它封装了这个建议:

Why Response.Redirect causes System.Threading.ThreadAbortException?

为什么Response.Redirect导致System.Threading.ThreadAbortException?

Luke

卢克

#5


0  

The reason of why Response.Redirect will give this exception is asp.net internally implement this API with Thread.Abort(). When this method is called, a special ThreadAbortException is thrown.This exception wont be swallowed by any catch block. It will be re thrown at the end of each catch block.

Response.Redirect为什么会给出这个异常的原因是asp.net在内部用Thread.Abort()实现了这个API。调用此方法时,将抛出一个特殊的ThreadAbortException。此异常不会被任何catch块吞噬。它将在每个catch块的末尾重新抛出。