【问题标题】:Asp.Net MVC Thread was being aborted during long running operation在长时间运行操作期间,Asp.Net MVC 线程被中止
【发布时间】:2014-04-17 10:02:12
【问题描述】:

我已经实现了一个使用 asp.net MVC 将 excel 文件转换为 .csv 文件的 Web 应用程序。

小文件一切正常。

但是当我尝试运行更大的文件时,我在客户端“ERR_CONNECTION_RESET”收到错误。

我已经做了研发并找到了解决方案,即

即使之后我也遇到了同样的问题。然后我通过引用这个Set Timeout For Controller Action 实现了线程。

现在我得到了“线程正在中止”的异常。

在这里我添加了我的堆栈跟踪。请帮我整理一下...

Exception: 'Thread was being aborted.' : 
StackTrace: 'at System.Data.Common.UnsafeNativeMethods.IRowset.GetData(IntPtr hRow, IntPtr hAccessor, IntPtr pData)
at System.Data.OleDb.OleDbDataReader.GetRowDataFromHandle()
at System.Data.OleDb.OleDbDataReader.GetValueBinding(MetaData info)
at System.Data.OleDb.OleDbDataReader.GetValues(Object[] values)
at  System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at ExcelToCsvConversion.Controllers.HomeController.Check_In_Masters(String sourceFile, String worksheet2, Int32 colNo, String fieldValue)'

这是我的代码....

   public ActionResult ConvertToCsv(FileUpload model)
    {       
        var fileName = model.SourceFile;
        FileUpload fileUpload = new FileUpload();
        try
        {
            string filename = Path.GetFileName(model.SourceFile);
            model.SourceFile = Path.Combine(Server.MapPath("~/App_Data/uploads"), filename);

                model.WorkSheet1 = "Upload file$";

                System.Threading.Tasks.Task.Factory.StartNew(() => ConvertExcelToCSV_LT(model));

                fileUpload.SourceFile = filename;

        }
        catch (Exception e)
        {
            ServerExceptionLog(e);
        }

        return Json(new { filemodel = fileUpload }, JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

  • 请显示一些代码,如何开始转换。你在使用任务吗?您是否设置了“长时间运行”模式?
  • 请检查我有问题更新的代码...

标签: c# asp.net asp.net-mvc multithreading asp.net-mvc-4


【解决方案1】:

你试过玩吗

<httpRuntime executionTimeout="HH:MM:SS" />

默认情况下限制为 110 秒。

【讨论】:

    【解决方案2】:

    在 web.config 中增加上传大小

    <system.web>
      <httpRuntime maxRequestLength="2147483647" />
    </system.web>
    
    <system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="52428800" /> <!--50MB-->
        </requestFiltering>
      </security>
    </system.webServer>
    

    maxRequestLengthInt32 (check here),因此您可以相应地设置大小。
    maxAllowedContentLengthuint (check here)

    【讨论】:

    • 已经尝试了所有这些解决方案。在我的情况下没有人有效。
    【解决方案3】:

    你试过用TaskCreationOptions.LongRunning吗?

    Task.Factory.StartNew(() => ConvertExcelToCSV_LT(model), 
        TaskCreationOptions.LongRunning);
    

    【讨论】:

    • 您是否尝试过相同的方法,例如控制台应用程序?它在那里工作吗?
    • 是的,实际上上面的方法在我的本地机器上运行。但是当我尝试在生产服务器上运行它时,我遇到了异常。
    • 为什么该选项会有所作为?
    • @vtortola:因为它不会在可能因超时而中止的普通任务池线程上进行调度,而是在“后台线程”上......
    猜你喜欢
    • 2019-08-03
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 2012-01-30
    • 1970-01-01
    相关资源
    最近更新 更多