WebAPI 异步方法错误
WebAPI async method error
正如您在下面的代码中看到的,当我尝试从数据层异步调用该方法时出现以下两个错误。
如何在不出现这些错误的情况下异步调用方法?
System.Runtime.CompilerServices.IAsyncStateMachine' is not defined or
imported Cannot find all types required by the 'async' modifier. Are
you targeting the wrong framework version, or missing a reference to
an assembly?
APIProject.DAL
public class Account : BaseLayer
{
public static Result SignIn(string userName, string userPass)
{
var inputParameters = new Dictionary<string, object>();
inputParameters.Add("IN_USERNAME", userName);
inputParameters.Add("IN_USERPASS", userPass);
var response = ListStoredProcedure(Procedure.SignIn, inputParameters);
var resultSet = new List<User>();
foreach (DataRow row in response.DataTable.Rows)
{
resultSet.Add(new User
{
UserId = row.GetVarchar("USERID"),
Fullname = row.GetVarchar("FULLNAME")
});
}
return new Result
{
HasResult = response.HasResult,
Message = response.Message,
ResultSet = resultSet
};
}
}
APIProject.WebAPI
using System.Threading.Tasks;
public class AccountController : ApiController
{
public async Task<Result> SignIn(LogInModel model)
{
Result r = APIProject.DAL.Account.SignIn(model.UserName, model.UserPass);
return await Task.Factory.StartNew<Result>(() => r);
}
}
我认为您的代码针对的是旧的 .NET Framework 版本。您至少需要以 .NET 4.5 为目标。如果您不想重新定位您的项目,您可以安装 Microsoft.Bcl.Async
NuGet 包,它允许在旧版本的 .NET 中使用 async\await
。
你收到错误
‘System.Runtime.CompilerServices.IAsyncStateMachine’ is not defined or
imported
当您定位到错误的 .NET Framework 版本时。要使异步开箱即用,您需要将应用程序定位到 .NET 4.5(至少)
您需要将项目更新到 .NET 4.5 或更高版本,和 set httpRuntime.targetFramework
in your web.config
to the appropriate value.
如果您跳过第二步,async
/await
将编译,但可能无法正常工作。
正如您在下面的代码中看到的,当我尝试从数据层异步调用该方法时出现以下两个错误。
如何在不出现这些错误的情况下异步调用方法?
System.Runtime.CompilerServices.IAsyncStateMachine' is not defined or
imported Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly?
APIProject.DAL
public class Account : BaseLayer
{
public static Result SignIn(string userName, string userPass)
{
var inputParameters = new Dictionary<string, object>();
inputParameters.Add("IN_USERNAME", userName);
inputParameters.Add("IN_USERPASS", userPass);
var response = ListStoredProcedure(Procedure.SignIn, inputParameters);
var resultSet = new List<User>();
foreach (DataRow row in response.DataTable.Rows)
{
resultSet.Add(new User
{
UserId = row.GetVarchar("USERID"),
Fullname = row.GetVarchar("FULLNAME")
});
}
return new Result
{
HasResult = response.HasResult,
Message = response.Message,
ResultSet = resultSet
};
}
}
APIProject.WebAPI
using System.Threading.Tasks;
public class AccountController : ApiController
{
public async Task<Result> SignIn(LogInModel model)
{
Result r = APIProject.DAL.Account.SignIn(model.UserName, model.UserPass);
return await Task.Factory.StartNew<Result>(() => r);
}
}
我认为您的代码针对的是旧的 .NET Framework 版本。您至少需要以 .NET 4.5 为目标。如果您不想重新定位您的项目,您可以安装 Microsoft.Bcl.Async
NuGet 包,它允许在旧版本的 .NET 中使用 async\await
。
你收到错误
‘System.Runtime.CompilerServices.IAsyncStateMachine’ is not defined or imported
当您定位到错误的 .NET Framework 版本时。要使异步开箱即用,您需要将应用程序定位到 .NET 4.5(至少)
您需要将项目更新到 .NET 4.5 或更高版本,和 set httpRuntime.targetFramework
in your web.config
to the appropriate value.
如果您跳过第二步,async
/await
将编译,但可能无法正常工作。