【问题标题】:System.Threading.ThreadAbortException occurred in mscorlib.dll occuring persitently持续发生的 mscorlib.dll 中发生 System.Threading.ThreadAbortException
【发布时间】:2013-10-08 12:42:45
【问题描述】:

我是 .net 新手, 我使用以下代码从 gridview 转移到 excel:

         protected void toexcelbutton_Click(object sender, EventArgs e)
        {
       Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "attendancedatereport.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.AllowSorting = false;
    GridView1.DataBind();
    HtmlForm htmfrm = new HtmlForm();
    GridView1.Parent.Controls.Add(htmfrm);
    htmfrm.Attributes["runat"] = "server";
    htmfrm.Controls.Add(GridView1);
    htmfrm.RenderControl(htw);
    Response.Write(sw.ToString());

    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

当我运行这段代码时,我得到一个异常:

在 mscorlib.dll 中发生了“System.Threading.ThreadAbortException”类型的第一次机会异常。

但是,当我在另一个页面中为另一个 gridview 运行相同的代码时,它可以完美运行。,我尝试了,添加

EnableEventValidation="false"

到 aspx 页面,并且

<pre lang="cs">public override void VerifyRenderingInServerForm(Control control)
{
   /* Verifies that the control is rendered */
}</pre>

到 aspx.cs 页面,

我尝试清除临时文件,但我仍然收到相同的错误,我尝试了 5 个多小时,但没有任何效果。

然后我尝试选择 Debug -> Exceptions 菜单项,并在出现的对话框中选中“Common Language Runtime Exceptions,

我得到这个错误:

拒绝访问路径“C:\Users\abcd\AppData\Local\Temp\Temporary ASP.NET Files\mark\3f229106\f785abea\App_Web_0h5ppn4m.dll”。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    是的,它会因为这个而发生:

    Response.End();
    

    作为documented:

    为了模仿 ASP 中 End 方法的行为,此方法尝试引发 [ThreadAbortException] 异常。如果此尝试成功,调用线程将被中止,这对您站点的性能不利。在这种情况下,调用 End 方法后不会执行任何代码。

    所以你的Response.Clear 调用毫无意义——你应该期望看到ThreadAbortException,在这种情况下你可以忽略它。

    如果你需要打电话给Response.Clear,应该在你写信之前给它打电话——但我怀疑你可以完全删除它。

    【讨论】:

    • @user2740323:这并不意味着它是正确的......你应该始终确保你理解你正在使用的任何代码,并验证它是否有意义。
    • 即使我删除了 respose.clear();和 response.end() 我知道这个错误:System.Web.dll 中发生了“System.Web.HttpUnhandledException”类型的第一次机会异常
    • @user2740323:我不建议你删除Response.End,只删除Response.ClearHttpUnhandledException 应该有一个内部异常 - 提高诊断技能非常重要,这样你就可以知道出了什么问题。确保您有适当的日志记录,以便您可以看到完整的堆栈跟踪。
    • 我是 .net 新手,请告诉我如何执行日志记录
    • @user2740323:不——我建议你退后一步,从一本好书或教程中了解 ASP.NET。尝试在 Stack Overflow 评论线程中针对甚至与日志记录无关的问题这样做是不合适的。不过,您可能想看看 ELMAH:code.google.com/p/elmah
    【解决方案2】:

    首先,我会使用像 EPPlus(GPL) 这样的 excel 库,我强烈推荐它。

    那么创建真正的excel文件并将其写入Response就这么简单,例如:

    Dim pck = New ExcelPackage()
    Dim ws = pck.Workbook.Worksheets.Add("Worksheet-Name")
    ws.Cells("A1").LoadFromDataTable(dt, True, OfficeOpenXml.Table.TableStyles.Medium1)
    Response.Clear()
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Response.AddHeader("content-disposition", "attachment;  filename=ExcelFileName.xlsx")
    Response.BinaryWrite(pck.GetAsByteArray())
    

    这是另一个例子:http://epplus.codeplex.com/wikipage?title=WebapplicationExample


    颠倒Response.Clear + Response.EndClear 应该是第一个,End 最后一个:

    try {
        Response.Clear();
        // ...
    } catch (Exception ex) {
        //log error
    }
    // use HttpContext.Current.ApplicationInstance.CompleteRequest(); instead  
    Response.End();  
    

    HttpResponse.End

    将所有当前缓冲的输出发送到客户端,停止执行 页面,并引发 EndRequest 事件。

    请注意,如果您尝试渲染 GridView,还应使用覆盖 VerifyRenderingInServerForm 以避免异常。

    public override void VerifyRenderingInServerForm(Control control)
    {
      /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
         server control at run time. */
    }
    

    GridView must be placed inside a form tag with runat="server" even after the GridView is within a form tag


    编辑

    但是你可以用HttpContext.Current.ApplicationInstance.CompleteRequest();代替Response.End

    http://scottstoecker.wordpress.com/2012/01/12/fixing-threadabortexception-when-using-response-end/

    【讨论】:

    • 我尝试使用:HttpContext.Current.ApplicationInstance.CompleteRequest();现在我得到“在 System.Web.dll 中发生了“System.Web.HttpUnhandledException”类型的第一次机会异常”
    • @user2740323:你是否也覆盖了VerifyRenderingInServerForm
    • 是的,我也添加了 EnableEventValidation="false" 到页面指令@Tim Schmelter
    • 我无法理解的是,相同的代码在其他 3 个页面中运行良好,但仅在此页面中显示错误。
    • @user2740323:您注意到ThreadAbortExceptionHttpUnhandledException 是不同的例外吗?所以可能你已经解决了一个问题。你的HttpUnhandledException 提供更多信息吗?您是否使用调试器遍历每一行并检查所有相关变量(例如 gridview 的数据源)?你在哪一行得到了异常?
    猜你喜欢
    • 2010-12-30
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多