17

I have this method in my service:

public virtual async Task<User> FindByIdAsync(string userId)
{
    this.ThrowIfDisposed();
    if (userId == null)
    {
        throw new ArgumentNullException("userId");
    }
    return await this.repository.FindByIdAsync(userId);
}

and then in a view I have this code:

using (var service = new UserService(new CompanyService(), User.Identity.GetUserId()))
{
    var user = service.FindByIdAsync(id);
}

but the user is the Task and not the User. I tried adding await to the service call, but I can't use await unless the current method is async. How can I access the User class?

3
  • 5
    Make the current method async and add await. Commented Jan 2, 2014 at 13:58
  • Can you show the code of the service.FindByIdAsync(id)? Commented Jan 2, 2014 at 14:01
  • 2
    'async' methods are like zombies: they propagate. Async methods callers must be async. Commented Jan 2, 2014 at 14:03

2 Answers 2

15

The best solution is to make the calling method async and then use await, as Bas Brekelmans pointed out.

When you make a method async, you should change the return type (if it is void, change it to Task; otherwise, change it from T to Task<T>) and add an Async suffix to the method name. If the return type cannot be Task because it's an event handler, then you can use async void instead.

If the calling method is a constructor, you can use one of these techniques from my blog. It the calling method is a property getter, you can use one of these techniques from my blog.

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

4 Comments

Say mate... Are you the core developer of the .net framework? You are deeply involved in concurrent development in c#
@IamStalker: Haha, no. I don't work for Microsoft. I just have a lot of experience in concurrent apps so I understand where they're coming from especially with async. There is a fellow named Stephen Toub who does work for Microsoft and has been deeply involved with TPL, Dataflow, and async.
Stephen, The problem I have is that my View is virtual so it calls my service but it is not a function so I can't prepend async :( is there some other work around?
The code calling FindByIdAsync must be in a method... Why can't you make that method async?
13
  • Using this in async methods without special thread-locked object is dangerous
  • If you cannot use await, use a code like following.

    Task<User> task = TaskFindByIdAsync();
    
    task.Wait(); //Blocks thread and waits until task is completed
    
    User resultUser = task.Result;
    

7 Comments

This is a really bad practice. It may be a hidden deadlock. see this
@Olivier Looks like it does not depend so. Only unclear code in task may cause a deadlock
A good habit is to never rely on how methods are implemented when calling them. If the Wait() is on the context thread, and method Waited (not A-waited) uses an 'await', then you will deadlock (because it waits for the main thread to be available).
It's unclear what you mean by 'Using this in async methods without special thread-locked object is dangerous'. Could you please clarify?
Using 'this in a 'readonly' way (i.e., reading fields) is perfectly safe, even multithreaded way. I see no need for locks in the code of the OP.
|

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.