C# 从ActionResults调用异步方法

C# 从ActionResults调用异步方法,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个ActionResult方法,它上载一个文件并调用CSVReader()读取文件并将其写入数据库的方法。但是,当代码运行时,我得到了一个错误,该错误与ActionResult方法不异步有关。这是我的代码:(有很多) 公共类文件 { 公共对象id{get;set;} 公共字符串产品名称{get;set;} 公共字符串部分_编号{get;set;} 公共字符串数量{get;set;} 公共字符串customer{get;set;} 公共字符串引用{get;set;} 公共字符串联系人{get

我有一个
ActionResult
方法,它上载一个文件并调用
CSVReader()读取文件并将其写入数据库的方法。但是,当代码运行时,我得到了一个错误,该错误与
ActionResult
方法不异步有关。这是我的代码:(有很多)

公共类文件
{
公共对象id{get;set;}
公共字符串产品名称{get;set;}
公共字符串部分_编号{get;set;}
公共字符串数量{get;set;}
公共字符串customer{get;set;}
公共字符串引用{get;set;}
公共字符串联系人{get;set;}
公共字符串引号_编号{get;set;}
公共字符串customer_po{get;set;}
公共字符串顺序\u日期{get;set;}
//公共字符串计数{get;set;}
}
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
[HttpPost]
公共操作结果索引(HttpPostedFileBase文件)
{
如果(file!=null&&file.ContentLength>0)
{
var fileName=System.IO.Path.GetFileName(file.fileName);
var path=System.IO.path.Combine(((“C:\\Dev\\ProductionOrderWebApp\\Uploads”),文件名);
file.SaveAs(路径);
CSVReader(文件名);
}
返回操作(“索引”);
}
公共静态异步void CSVReader(字符串文件名)
{
StreamReader oStreamReader=新的StreamReader(文件名);
DataTable oDataTable=null;
int RowCount=0;
字符串[]ColumnNames=null;
字符串[]oStreamDataValues=null;
//使用while循环读取流数据直到结束
而(!oStreamReader.EndOfStream)
{
字符串oStreamRowData=oStreamReader.ReadLine().Trim();
如果(oStreamRowData.Length>0)
{
oStreamDataValues=oStreamRowData.Split(',');
//如果第一行包含列名,我们将对
//列名由
//读取第一行和RowCount-0将仅为true一次
如果(行计数==0)
{
行数=1;
ColumnNames=oStreamRowData.Split(',');
oDataTable=新数据表();
//使用foreach遍历所有列名
foreach(列名称中的字符串csvcolumn)
{
DataColumn oDataColumn=新的DataColumn(csvcolumn.ToUpper(),typeof(string));
//将empty.string的默认值设置为新创建的列
oDataColumn.DefaultValue=string.Empty;
//将新创建的列添加到表中
oDataTable.Columns.Add(oDataColumn);
}
}
其他的
{
//使用与oDataTable相同的架构创建新的DataRow
DataRow oDataRow=oDataTable.NewRow();
//使用foreach遍历所有列名
for(int i=0;i

我已经让它在MVC(控制台)之外工作,但是
ActionResult
抛出一个异步错误。如何调用
CSVReader()方法而不抛出错误?

最好在整个调用堆栈中使用async/Wait,这在MVC中是可能的。将您的操作标记为async,然后使用wait关键字等待来自CsvReader方法的结果

// changed signature to async and Task<ActionResult>
[HttpPost]
public async Task<ActionResult> Index(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        var fileName = System.IO.Path.GetFileName(file.FileName);
        var path = System.IO.Path.Combine(("C:\\Dev\\ProductionOrderWebApp\\Uploads"), fileName);
        file.SaveAs(path);
        // add await
        await CSVReader(fileName);
    }
    return RedirectToAction("Index");
}

// did not check the code in the method but the signature should return type Task
public static async Task CSVReader(string fileName) {/*existing/unchanged code*/}
//将签名更改为异步和任务
[HttpPost]
公共异步任务索引(HttpPostedFileBase文件)
{
if(file!=null&&file.ContentLength
// changed signature to async and Task<ActionResult>
[HttpPost]
public async Task<ActionResult> Index(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        var fileName = System.IO.Path.GetFileName(file.FileName);
        var path = System.IO.Path.Combine(("C:\\Dev\\ProductionOrderWebApp\\Uploads"), fileName);
        file.SaveAs(path);
        // add await
        await CSVReader(fileName);
    }
    return RedirectToAction("Index");
}

// did not check the code in the method but the signature should return type Task
public static async Task CSVReader(string fileName) {/*existing/unchanged code*/}