Many years later... it seems at least worth mentioning that what OP requested was a way to convert an asynchronous call to a synchronous one that waits for a result to return before proceeding, just as in olden times.
Putting a synchronous wrapper on an asynchronous call is easy if you really want/need it. The downside is that it will block the thread on which the synchronous wait executes. Just as in olden times.
It has already been pointed out that usually it is better to modify the design to keep asynchronous things asynchronous. I agree with this sentiment.
The original code did not behave as OP expected because await returns control to the caller (the album constructor) immediately, while scheduling a continuation for the remainder of the code in the initialize() method so that Parse(SourceStream) will execute when the awaited task completes. The accepted answer suggests one way to avoid synchronous waiting and to instead take advantage of the inherent asynchrony of the underlying data request. Other async approaches exist that could preserve the OP's desire to populate the song list within the album constructor. Either way, async all the way down would be a better approach than using a sync wrapper for this application.
Nonetheless, there are situations in which it is actually desirable or necessary to wait synchronously for the results of an async call. Here are two ways to accomplish this in the context of OP's code.
First Method:
// Task.GetAwaiter().GetResult() waits synchronously // and preserves exceptions thrown by the wrapped function. private void Initialize() // no longer async { HttpClient cli = new HttpClient(); // Original code returns control to the caller immediately: // Stream SourceStream = await HttpClient.GetStreamAsync("http://contoso.com"); // Instead do this to block synchronously until the result is available: Task songStreamTask = HttpClient.GetStreamAsync("http://contoso.com"); Stream SourceStream = songStreamTask.GetAwaiter().GetResult(); this.Songs = Parse(SourceStream); }
Second method:
// Task.Wait() waits synchronously and collects exceptions, // returning them in an Aggregate Exception private void Initialize() // no longer async { HttpClient cli = new HttpClient(); Task songStreamTask = HttpClient.GetStreamAsync("http://contoso.com"); Stream SourceStream = songStreamTask.Wait(); this.Songs = Parse(SourceStream); }
Both examples shown will block the thread until the task is complete, returning the result just as if GetStreamAsync were a synchronous call. Most of the time this is not something you want to do. If you allow the UI thread to execute a blocking call to a web method, you may find out exactly why async methods were invented. Be careful what you ask for...
awaitdoesn't work like that. it starts an asynchronous operation. you need toawaitfor the result in your code.awaititself doesn't start the asynchronous operation.awaitjust hands control back and schedules a continuation (if it needs to, of course).GetStreamAsyncis what starts the operation. That could happen a long way before theawaitexpression - in the asynchronous task-based pattern, tasks are "hot", unlike in F# for example. Theawaitexpression only schedules the continuation where necessary - it doesn't start the operation.