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

Alma
  • 3,780
  • 11
  • 42
  • 78

3 Answers3

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);
}
Gabor
  • 3,021
  • 1
  • 11
  • 20
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);
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
-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)     
 }
suulisin
  • 1,414
  • 1
  • 10
  • 17
  • This is not an answer to his question, but a comment. Please don't use the answers section for this. – Leon Cullens May 05 '16 at 01:20
  • 1
    And your remark about ConfigureAwait is completely off. Please read a bit about what it actually does and why you would use it. – Leon Cullens May 05 '16 at 01:21
  • The usage of `async/await` add additional unneccessary overhead for no reason. Consider directly returning to `Task.Run` result. – Aron May 05 '16 at 01:29
  • My remarks about ConfigureAwait are very valid. Please read about it here [link](http://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code/13494570#13494570). Also I have first hand information from Microsoft about it. – Soma Yarlagadda May 05 '16 at 01:32
  • 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. – Soma Yarlagadda May 05 '16 at 01:45
  • how is it answer ? – Nazmul Hasan May 05 '16 at 09:54