Pergunta

I'm trying to write the following method:

    public async Task<string> GetJson(string url, 
                    Dictionary<string,string> parameters = null){

        // parameters?
        if (parameters != null) {
            if (parameters.Count > 0) {
                url += "?";
                foreach(var key in parameters.Keys){

                    // add parameter to url
                    url += String.Format("{0}={1}", key, parameters[key]);

                    // more parameters?
                    if(!parameters.Keys.Last ().Equals(key)){
                        url += "&";
                    }
                }
            }
        }
        // send request
        var uri = new Uri(url);
        var req = new HttpClient();
        Task<string> getJsonTask = req.GetStringAsync(uri);
        // EXCEPTION : 'Cannot await on Task<string>'
        return await getJsonTask;
    }

I get an error at compilation time. I don't think the problem is the syntax. I think is a dependencies issue. My references are:

enter image description here

I added them using Nuget. Any ideas about what could be the issue?

Thanks in advance.

UPDATE

Suggested by Selman22:

    public async Task<string> GetJson(string url, Dictionary<string,string> parameters = null){
        // ...
        return await req.GetStringAsync(uri); // Same exception
    }

Suggested by Kenneth:

    public Task<string> GetJson(string url, Dictionary<string,string> parameters = null){
        // ...
        return req.GetStringAsync(uri); // no problems here
    }

    // Later when trying to invoke GetJson
    public async Task<List<Station>> GetStations(){
        var url = String.Format(
            "?{0}_id{1}&_render=json", 
            DublinBikeDataProvider.BASE_URL,
            DublinBikeDataProvider.STATIONS
        );
        var json = await this._httpClient.GetJson(url); // same exception
        return this.ParseStations(json);
    }
Foi útil?

Solução

Ensure that you have installed the Microsoft.Bcl.Async NuGet library. If you change your PCL target platforms, you should uninstall all NuGet packages and re-install them.

Outras dicas

You cannot await in a method that's not marked as async. In this case, you have two options...

Either change the definition of the method to include the async keyword before the return type :

public async Task<string> GetJson(string url, 
                Dictionary<string,string> parameters = null){
     // .... Your code here.

    return await getJsonTask;
}

Or, change the method to just not await:

public Task<string> GetJson(string url, 
                Dictionary<string,string> parameters = null){
     // .... Your code here.

    return getJsonTask;
}

For debuggability and complexity, do the 2nd one - it will generate the least extra code (which you don't really need). If you think that you are going to change the method soon to have more asynchronous steps in it (except for the single network call), you might wish to keep the async keyword in there.

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