正如您在下面的代码中看到的,当我尝试从数据层异步调用该方法时,出现以下两个错误。
如何异步调用该方法而不出现这些错误?
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 4.5 或更高版本,和 set httpRuntime.targetFramework
in your web.config
to the appropriate value .
如果跳过第二步,async
/await
将编译,但可能无法正常工作。
关于c# - WebAPI 异步方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36596564/