-
Notifications
You must be signed in to change notification settings - Fork 8
/
ConfigureAwaitWindow.xaml.cs
37 lines (30 loc) · 1.07 KB
/
ConfigureAwaitWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using System.Net.Http;
using System.Windows;
namespace AsyncAwait;
/// <summary>
/// Interaction logic for ConfigureAwaitWindow.xaml
/// </summary>
public partial class ConfigureAwaitWindow : Window
{
private readonly IHttpClientFactory clientFactory;
public ConfigureAwaitWindow()
{
InitializeComponent();
var services = new ServiceCollection();
services.AddHttpClient();
this.clientFactory = services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>();
}
private async void OnFetchButtonClick(object sender, RoutedEventArgs e)
{
using (HttpClient w = this.clientFactory.CreateClient())
using (Stream f = File.Create(fileTextBox.Text))
{
Task<Stream> getStreamTask = w.GetStreamAsync(urlTextBox.Text);
Stream getStream = await getStreamTask.ConfigureAwait(false);
Task copyTask = getStream.CopyToAsync(f);
await copyTask.ConfigureAwait(ConfigureAwaitOptions.None);
}
}
}