Pergunta

I ran into a peculiar problem. Basically I'm writing a wrapper for an existing asynchronous class that reads data from the server. The original method that is in the library is something like this

public async Task<IEnumerable<PersonData>> GetPersonData();

It works splendidly with this

var people = await GetPersonData();

However, I run into a problem when I try to wrap it. For the sake of simplicity, I've included the initial version I tried to test it which didn't work and furthermore, my actual wrapping method gives a peculiar "Cannot await" error that doesn't indicate the actual reason as to why it can't be done. It is a compiler error, so I must be doing something wrong.

public async Task<IEnumerable<PersonData>> GetWrappedPersonData()
{
    return await clientClass.GetPersonData();
}

And then when I call this in my main class

var wrappedPeople = await wrapper.GetWrappedPersonData();

I get an error of the "await" stating that "Cannot await 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<PersonData>>'

When I hover over the GetWrappedPersonData, it says the method is awaitable. I am confused as to why I can't await this here.

Foi útil?

Solução

I found the source of the problem though, it was the fact that this whole thing consists of a plurality of different projects. The communication module of it contains multiple different libraries to separate service providers. The returned "PersonData" resided in one of these assemblies and was not referenced from the main app project. The error I received didn't really reflect that this was the root cause.

So basically the error I received gave very little hint towards what was actually wrong with the solution. Hope this helps someone else out there receiving the same error. :P

Outras dicas

Looks like a compiler message error though.

But the solution is you should not await in your wrapper :)

If you await it you unwrap the task, and you can't await an already awaited task :)

You need async on your calling method. If that method is Main, then see this answer. https://stackoverflow.com/a/9212343/171121

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top