0

I am writting a middleware for Asp.Net Identity UserStore where the caller expects a Task and the Method to be called is NonAsynch.

Task IUserSecurityStampStore<T, string>.SetSecurityStampAsync(T user, string stamp)
{


    var res = Utility.SetSecurityStamp(user, stamp); // needs to be called as Async

    var identityUser = ToIdentityUser(res);

    SetApplicationUser(user, identityUser);

    return ??? ; // How do i get a task to return here ?

}

How do i return a Task out of res and stamp? Tried Task.FromResult but it says only one type argument is allowed.

2
  • Very confusing what you have trouble with... Possibly just Task.FromResult is the solution (if it is the case you may want to edit your question to make it clear that you need synchronous method to be compatible with async siganture/be await-able) Commented Apr 6, 2016 at 2:34
  • @AlexeiLevenkov It says Task.FromResult requires one type argument. I just want to return a Task from the result res and stamp Commented Apr 6, 2016 at 2:38

1 Answer 1

1

So your code could become something like that:

async Task IUserSecurityStampStore<T, string>.SetSecurityStampAsync(T user, string stamp)
{
    var res = await Task.Run(() => Utility.SetSecurityStamp(user, stamp));
    var identityUser = ToIdentityUser(res);
    SetApplicationUser(user, identityUser);
}

Or you can just use Task.FromResult to make your method awaitable :

async Task IUserSecurityStampStore<T, string>.SetSecurityStampAsync(T user, string stamp)
{
    var res = Utility.SetSecurityStamp(user, stamp);
    var identityUser = ToIdentityUser(res);
    SetApplicationUser(user, identityUser);

    await Task.FromResult(0);
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks but the utility method is nonAsync over which i have no control.So can't use await
what is the return type of your Utility.SetSecurityStamp method ? Is it a longt time running process ?
It returns type T and does some DB operations so yes it can be a long time running process.
You can use Task.Run to wrap your non awaitable call. I've edited my post
In 1st example await has to be removed. In the second example what does 0 mean?
|

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.