-3

I have the method below that is returning this error:

Error   CS1983  The return type of an async method must be void, Task, Task<T>, a task-like type, IAsyncEnumerable<T>, or IAsyncEnumerator<T>

But when I click on the error number link in Visual Studio, it takes me to a Microsoft website that just says:

Sorry, we don't have specifics on this C# error

Here is the method:

private async String SaveAIguess(IFormFile file)
{
    var picture = GetFile(file.FileName);
    await StorageService.Save(picture, file.OpenReadStream());

    var resourceUrl = Path.GetFullPath(file.FileName);
    return resourceUrl;
}

Is there a way to make it asynchronous and still be able to return the resourceUrl?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

3 Answers3

3

Make it return Task<string>:

private async Task<string> SaveAIguess(IFormFile file)
{
    var picture = GetFile(file.FileName);
    await StorageService.Save(picture, file.OpenReadStream());

    var resourceUrl = Path.GetFullPath(file.FileName);
    return resourceUrl;
}

async methods either have no return value or anything that can be encapsulated in a Task<>. It's why to get the actual value you do:

var val = await SaveAIguess(file);

await basically "unwraps" the Task and gives you your string.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

Asynchronous methods must return either Task, Task<?>, or void. If you change your return type to Task<string> it should work as desired. Of course, any method calling your async method must also be asynchronous or it must call it like SaveAIguess(dat).Result to make it synchronous.

So your final code will look like

private async Task<String> SaveAIguess(IFormFile file)
{
    var picture = GetFile(file.FileName);
    await StorageService.Save(picture, file.OpenReadStream());

    var resourceUrl = Path.GetFullPath(file.FileName);
    return resourceUrl;
}

And it can be called like this in another async method:

var str = await SaveAIguess(dat);

Or in a synchronous method

var str = SaveAIGuess(dat).Result
JD Davis
  • 3,517
  • 4
  • 28
  • 61
-2

Yes:

private async Task<String> SaveAIguess(IFormFile file)
nvoigt
  • 75,013
  • 26
  • 93
  • 142