mattias mattias - 1 year ago 103
ASP.NET (C#) Question

How to await async method inside ExecutePostProcessingAsync

I use the code from to upload blobs to azure. With the method ExecutePostProcessingAsync() I would like to call a method that resizes images and that uses async code.

The method i want to use it in looks(shrinked) like this:

public override Task ExecutePostProcessingAsync()
//some code

//I would like to await the image resizer method here before going any further

//Some more code here before returning
return base.ExecutePostProcessingAsync();

If i add async to the method like this: public override async Task ExecutePostProcessingAsync()

5+ errors will pop up with reference issues and also the error:

"is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task'?"


Is there anyway to await an method inside this method? Any help or input appreciated, thanks!

Answer Source

The async keyword enables the use of the await keyword. With that said, the correct syntax is to not return the Task that represents the operation, but rather simply await it. Consider the following:

public override async Task ExecutePostProcessingAsync()
    await resizer.ScaleImageAsync();
    await base.ExecutePostProcessingAsync();

Notice how instead of trying to return the Task that represents the base execution, i.e.; base.ExecutePostProcessingAsync() we simply await instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download