I'm using an asp.net 4.5 web application and asp.net membership. I implement the MembershipProvider interface which has a method:
public override bool ValidateUser(string username, string password)
The code within this method for my implementation has to make two WCF service calls which can be expensive. In the spirit and my desire of learning and using async, I'd like to make the code use async await.
I created my two Tasks that need to call the WCF service methods. What I'm unclear on is how to properly use async from the "ValidateUser" method to call these Tasks. I cannot change this method signature to add async as I would have to then change bool to Task so I'm stuck with a method signature that cannot be adored with the async modifier.
In this case how can I call my two tasks and have them run on a background thread? I know I can use Task.Result but my understanding is that would be a synchronous call and is frowned upon. I'm not sure of the gain by using Task.Result as my intention is to background thread my two WCF service method calls.
Well, you can certainly call the tasks asynchronously inside your membership provider, you just can't call ValidateUser asynchronously.
The old Membership API was created long before Async existed and is not compatible. You could wrap this around an async method. Here's a blog entry about it.
http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx
The gist is something like this (change to use for ValidateUser)
public Task SleepAsync(int millisecondsTimeout)
{
return Task.Run(() => Sleep(millisecondsTimeout));
}