1

I'm developing an application that needs to take in a list of objects and validate their properties against database values to ensure everything is valid. However, there can be many objects with many properties that need validation.

My initial idea was to create a helper method for each validation operation, each one opening a connection to the Database Context, performing their required select statements, and then returning the appropriate data.

The final operation within the foreach loop would be to await all of the operations, and then move onto the next object.

However, my initial attempts to use FirstOrDefaultAsync have proven futile as it never seems to return a value, and it doesn't seem compatible with Linq to SQL. So what other options do I have to make these helper methods asynchronous?

My main method looks something like this:

public async Task<List<BulkUserResponse.VerifiedUser>> ValidateBulkUsers(BulkUserResponse userList)
{
    var users = userList.UserList;

    foreach (var user in users)
    {
        var userGlobalLogin = VerifyGlobalLogin(user.GlobalLogin);
        // other variables and helper methods

        await Task.WhenAll(userGlobalLogin);

        user.GlobalLogin = userGlobalLogin.Result;
        // other properties and helper method assignment
    }

    return users;
}

With one of the helper methods looking like this:

public async Task<BulkUserResponse.GlobalLogin> VerifyGlobalLogin(BulkUserResponse.GlobalLogin login)
{
    using (var context = new DbContext())
    {
        var userExists = await context.GlobalLogins.FirstOrDefaultAsync(n => n.LoginName == login.Value) == null;
        login.Valid = userExists;
        login.VerificationMessage = (userExists ? "" : "Login already exists.");

        return login;
    }
}

Initially the helper method looked like:

public async Task<BulkUserResponse.GlobalLogin> VerifyGlobalLogin(BulkUserResponse.GlobalLogin login)
{
    using (var context = new DbContext())
    {
        var userExists = context.GlobalLogins.FirstOrDefault(n => n.LoginName == login.Value) == null;
        login.Valid = userExists;
        login.VerificationMessage = (userExists ? "" : "Login already exists.");

        return login;
    }
}
5
  • Can you show your code usage? Commented Oct 28, 2015 at 13:28
  • is it Linq2Sql or EF? Commented Oct 28, 2015 at 13:29
  • I've updated my initial post with a code sample. Commented Oct 28, 2015 at 13:31
  • Not sure if this is a problem (and if the following is a solution), but trying doing the query on its own (to get the object) and then compare it to null. Commented Oct 28, 2015 at 13:37
  • Well isn't that just something strange. It immediately started working after that. Commented Oct 28, 2015 at 13:57

1 Answer 1

1

it doesn't seem compatible with Linq to SQL.

LINQ to SQL does not support asynchronous queries. However, if you can upgrade to Entity Framework, you can use it.

my initial attempts to use FirstOrDefaultAsync have proven futile as it never seems to return a value

As I describe on my blog, the most common cause of this deadlock is that code further up the call stack is blocking on a task (usually by calling Wait or Result). The most correct solution is to change those blocking calls to use await instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.