Reputation: 8245
I'm working off a tutorial here. Based on this, I have the following code:
namespace LoganMVC.Controllers
{
public class UsersController : AsyncController
{
//
// GET: /Users/
public async Task<ActionResult> Index()
{
UserModel u = new UserModel();
var users = await u.GetUsers();
return View(users);
}
}
}
namespace LoganMVC.Models
{
public class UserModel
{
public async Task<List<User>> GetUsers()
{
WebUser u = new WebUser();
List<User> users = await u.GetUsers();
return users;
}
}
}
namespace Logan.Web.Data
{
public class WebUser
{
WebDBContext db = new WebDBContext();
public async Task<List<User>> GetUsers()
{
var query = from u in db.Users
orderby u.Username
select u;
List<User> data = await query.Include(x => x.Role).ToListAsync();
return data;
// this returned when I had it synchronous
//return db.Users.Include(x => x.Role).ToList();
}
}
}
On every public async Task<T>
function, the function name is underlined red with the following error:
Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly?
Further, where I use .ToListAsync()
, I get this error:
'System.Linq.IQueryable<Logan.Web.Objects.User> dos not contain a definition for 'ToListAsync' and no extension method 'ToListAsync' accepting a first argument of type 'System.Link.IQueryable<Logan.Web.Objects.User>' could be found (are you missing a using directive or an assembly reference?)
As you can see, the article doesn't cover what using directives or assembly references I'd need so I've basically just added directives as VS allows me to.
Can anyone clear up what I'm missing or not understanding here?
After updating to .NET 4.5, I have one more error:
Error 2 Predefined type 'System.Runtime.CompilerServices.IAsyncStateMachine' is not defined or imported D:\Development\Logan\LoganWeb\Logan.Web.Data\CSC Logan.Web.Data
Upvotes: 0
Views: 673
Reputation: 149598
As per comments, you're using the .NET 4.0 framework which doesn't support the async/await
keywords.
You can either upgrade you framework version to 4.5, or you can get the Microsoft.Bcl.Async package from nuget which adds those abilities to .NET 4.0
Upvotes: 1