Asp.Net MVC Thread在长时间运行期间被中止

时间:2014-04-17 10:02:13

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

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

小文件一切正常。

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

我做过R& D并找到了解决方案,即

即便如此,我也面临同样的问题。然后我通过引用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);
    }

3 个答案:

答案 0 :(得分:2)

你试过玩

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

默认情况下,您的播放时间限制为110秒。

答案 1 :(得分:1)

在web.config中增加上传大小

<system.web>
  <httpRuntime maxRequestLength="2147483647" />
</system.web>

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

maxRequestLengthInt32check here),因此您可以相应地设置尺寸。
maxAllowedContentLengthuintcheck here

答案 2 :(得分:0)

您是否使用TaskCreationOptions.LongRunning尝试了?

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