9

I am calling a method in Web API(C#) from repository. The method is repository is not returning anything. It is Void. whay should I return in my API method as a async method cannot have a Void return type.

This is my Async method in API:

    [HttpPost]
    [Route("AddApp")]
    public async Task<?> AddApp([FromBody]Application app)
    {        
        loansRepository.InsertApplication(app);
    }

and this is EntityFrame work Insert in repository(I can cot change this by the way)

     public void InsertApplication(Application app)
    {

        this.loansContext.Application.Add(app);
    }

Sorry I made changes to the question, I am not sure what should I have for ? in the Task

5
  • what should your Task<string> return? Commented May 4, 2016 at 23:30
  • @ Philippe Paré sorry made changes to the Code, I dont have any return type it is void. Commented May 4, 2016 at 23:34
  • 2
    Then simply return Task instead :) Commented May 4, 2016 at 23:35
  • 3
    If the Web API method/action will not involve any async work (e.g. IO), you shouldn't really make it async Commented May 4, 2016 at 23:36
  • Yes @YacoubMassad is right, but if you need it to happen on another thread (if you don't want the code to block there), you can call Task.Run(()=>//Your cp-bound work here, happening on another thread)); Commented May 4, 2016 at 23:40

3 Answers 3

14

If you do not want to return anything then the return type should be Task.

[HttpPost]
[Route("AddApp")]
public async Task AddApp([FromBody]Application app)
{
    // When you mark a method with "async" keyword then:
    // - you should use the "await" keyword as well; otherwise, compiler warning occurs
    // - the real return type will be:
    // -- "void" in case of "Task"
    // -- "T" in case of "Task<T>"
    await loansRepository.InsertApplication(app);
}

public Task InsertApplication(Application app)
{
    this.loansContext.Application.Add(app);

    // Without "async" keyword you should return a Task instance.
    // You can use this below if no Task is created inside this method.
    return Task.FromResult(0);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can also return Task.CompletedTask instead of FromResult(0)
8

Since you "cannot" change the Entity Framework repository, you shouldn't make your action method asynchronous, and you should just return void:

[HttpPost]
[Route("AddApp")]
public void AddApp([FromBody]Application app)
{        
    loansRepository.InsertApplication(app);
}

3 Comments

Stephen is the authority on all things async - I would go with his answer.
How would you then call this method from a C# client?
@FaithWins: You'd post to /AddApp with the serialized Application as the request body. If you want more information, I recommend asking your own question.
-5

The compiler will warn "Return statement missing". Please modify the code to:

 [HttpPost]
 [Route("AddApp")]
 public void AddApp([FromBody]Application app)
 {  
     //add configureAwait(false) as it would give better performance.

   loansRepository.InsertApplication(app)     
 }

6 Comments

This is not an answer to his question, but a comment. Please don't use the answers section for this.
And your remark about ConfigureAwait is completely off. Please read a bit about what it actually does and why you would use it.
The usage of async/await add additional unneccessary overhead for no reason. Consider directly returning to Task.Run result.
My remarks about ConfigureAwait are very valid. Please read about it here link. Also I have first hand information from Microsoft about it.
Stephen Cleary's is the best approach since the repository call is synchronous. But, the question was about how to return void from the async method, I had to wrap it in Task.Run. Task.Run is not a recommended approach in asp.net applications as you are using a thread from the thread pool.
|

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.