2

The code is below:

public class XiciHttp
{
    public async Task<string> Login(string apiAddress)
    {
        using (HttpClient client = new HttpClient())
        {
            return await client.GetStringAsync(apiAddress);
        }
    }
}


The error happens in here:

await client.GetStringAsync(apiAddress);



The error message is:

Cannot convert expression type 'void' to async method return type 'string'



The situation is:
VS2012 Update 4;
Windows Phone Project (based on WP 7.1)
This class is in a PCL project
The project has refer lots of libs using NuGet (MS Async, HttpClient, Json.Net)


The strange thing is:
When I click "Build Solution", no error happens in the Error List window.
it said:
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========


My Questiohn is:
1. Why
2. How to solve it?

Albert Gao
  • 3,653
  • 6
  • 40
  • 69
  • `using (client = new HttpClient())` This is not a particularly good pattern to use. `using (var client = new HttpClient())` would be better. That aside, is your problem with IntelliSense? Since your solution builds, and all. – ta.speot.is Jan 26 '14 at 09:23
  • Avoid var if you can, better `using (HttpClient client = new HttpClient())`. – pid Jan 26 '14 at 09:24
  • @ta.speot.is i've edit.. @_@ – Albert Gao Jan 26 '14 at 09:26
  • @pid, that's a personal style I'd say. I find it smarter and faster to write and the IDE tells me the rest. – timmkrause Jan 26 '14 at 09:27
  • Does this help you? http://stackoverflow.com/questions/18562613/async-method-returning-taskt-with-generic-constraint-in-c-sharp – pid Jan 26 '14 at 09:28
  • 2
    @pid Why? It's just a matter of opinion. In this case it is clear what the type is, so it's okay to use it. – Silvermind Jan 26 '14 at 09:29
  • are you sure that use system `HttpClient` and not custom class from lib? – Grundy Jan 26 '14 at 09:33
  • It's a nuance, really. I'd advise to avoid var whenever possible because it shows you know what you are doing. It is not a matter of taste or style, it is a matter of [Clean Code](http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882). This explains [why not to use var](http://www.brad-smith.info/blog/archives/336). – pid Jan 26 '14 at 09:34
  • 5
    Usage of `var` isn't related to this question, please don't post irrelevant comments. – Selman Genç Jan 26 '14 at 09:36
  • 2
    If your solution builds try cleaning the solution, closing VS and then opening it again. – ta.speot.is Jan 26 '14 at 09:46

2 Answers2

0

Restarting Visual Studio 2012 solved this error.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Albert Gao
  • 3,653
  • 6
  • 40
  • 69
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – fweigl Jan 26 '14 at 10:43
  • I edited your answer to tone it down a little. – rene Jan 26 '14 at 10:46
  • @Ascorbin ironically enough, this the the author of the question. – Shadow The GPT Wizard Jan 26 '14 at 11:01
  • @Ascorbin The problem is with IntelliSense, there's no real error. See the question: *When I click "Build Solution", no error happens in the Error List window.* IntelliSense stuffed up. Restarting VS fixed it. What do you expect the answer to be? – ta.speot.is Jan 26 '14 at 11:12
-1

How do you call Login() ? The following raises no errors.

private async void GetString()
{
    var str = await Login("http://www.google.com");
}

public async Task<string> Login(string apiAddress)
{
    using (var client = new HttpClient())        
        return await client.GetStringAsync(apiAddress);
}
Pantelis
  • 2,060
  • 3
  • 25
  • 40
  • Neither does the original code. It was an error with IntelliSense. *========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========* – ta.speot.is Jan 26 '14 at 15:16
  • How do you know? The question does not specify how the `Login()` function is being called. And whats with the downvote? – Pantelis Jan 26 '14 at 16:08
  • Because the question says that the IntelliSense error happens here `await client.GetStringAsync(apiAddress);`. Not `Login`... – ta.speot.is Jan 26 '14 at 22:39