Skip to content

Commit 09249b2

Browse files
author
Anton Wieslander
committed
async await
1 parent 1799246 commit 09249b2

23 files changed

+460
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "WebApp\WebApp.csproj", "{7F157BFA-1063-42D8-9E8A-6C74C7B630F5}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhereDoesItStart", "WhereDoesItStart\WhereDoesItStart.csproj", "{23C26454-BEA0-4080-B77E-F4D0338BAD24}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApp", "WpfApp\WpfApp.csproj", "{F5C53ADB-5A4E-4D8D-97F7-5070AF2CFCC0}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{7F157BFA-1063-42D8-9E8A-6C74C7B630F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{7F157BFA-1063-42D8-9E8A-6C74C7B630F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{7F157BFA-1063-42D8-9E8A-6C74C7B630F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{7F157BFA-1063-42D8-9E8A-6C74C7B630F5}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{23C26454-BEA0-4080-B77E-F4D0338BAD24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{23C26454-BEA0-4080-B77E-F4D0338BAD24}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{23C26454-BEA0-4080-B77E-F4D0338BAD24}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{23C26454-BEA0-4080-B77E-F4D0338BAD24}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{F5C53ADB-5A4E-4D8D-97F7-5070AF2CFCC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{F5C53ADB-5A4E-4D8D-97F7-5070AF2CFCC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{F5C53ADB-5A4E-4D8D-97F7-5070AF2CFCC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{F5C53ADB-5A4E-4D8D-97F7-5070AF2CFCC0}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {827DE696-34F9-410F-84C9-EF06D2337BA5}
36+
EndGlobalSection
37+
EndGlobal
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace WebApp.Controllers
10+
{
11+
public class AvoidStateMachine : Controller
12+
{
13+
public async Task<ActionResult> Index()
14+
{
15+
var a = await InputOutputN();
16+
return View(a);
17+
}
18+
19+
20+
public async Task<string> InputOutputN()
21+
{
22+
var client = new HttpClient();
23+
var content = await client.GetStringAsync("some site");
24+
// do something with the content
25+
26+
return content;
27+
}
28+
29+
public Task<string> InputOutputNA()
30+
{
31+
var client = new HttpClient();
32+
return client.GetStringAsync("some site");
33+
}
34+
35+
public Task<string> InputOutput()
36+
{
37+
var message = "Hello World";
38+
return Task.FromResult(message);
39+
}
40+
41+
public Task InputOutputC()
42+
{
43+
return Task.CompletedTask;
44+
}
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace WebApp.Controllers
10+
{
11+
public class DoesntMatterWhichThread : Controller
12+
{
13+
public IActionResult Index()
14+
{
15+
return View();
16+
}
17+
18+
public async Task<string> InputOutput()
19+
{
20+
var client = new HttpClient();
21+
var content = await client.GetStringAsync("some site")
22+
.ConfigureAwait(false);
23+
24+
return content;
25+
}
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace WebApp.Controllers
10+
{
11+
public class DontAsyncInConstructor : Controller
12+
{
13+
public IActionResult Index()
14+
{
15+
return View();
16+
}
17+
18+
public Task<string> InputOutput()
19+
{
20+
21+
}
22+
}
23+
24+
public class SomeObject
25+
{
26+
public SomeObject()
27+
{
28+
//never do async here.
29+
}
30+
31+
public static async Task<SomeObject> Create()
32+
{
33+
return new SomeObject();
34+
}
35+
}
36+
37+
public class SomeObjectFactory
38+
{
39+
public SomeObjectFactory()
40+
{
41+
42+
}
43+
44+
public async Task<SomeObject> Create()
45+
{
46+
return new SomeObject();
47+
}
48+
}
49+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace WebApp.Controllers
10+
{
11+
public class DontBlockTheThread : Controller
12+
{
13+
public IActionResult Index()
14+
{
15+
var task = InputOutput();
16+
17+
//BAD
18+
var a = task.Result;
19+
20+
//BAD
21+
task.Wait();
22+
23+
//BAD
24+
task.GetAwaiter().GetResult();
25+
26+
//solution is to propagate async await task throught your code
27+
28+
return View();
29+
}
30+
31+
public Task<string> InputOutput()
32+
{
33+
var client = new HttpClient();
34+
return client.GetStringAsync("some site");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)