MVC – Task-based Asynchronous Pattern (TAP) – Async Controller and SessionLess Controller
In async programming, there are 3 different models for different scenarios :
- Asynchronous Programming Model – APM
- Task Based Asynchronous Programming Model – TAPM
- Evented Asynchronous Programming Model – EAPM
We can use all models in .net framework. It provides related libraries in their own namespaces. The Task-based Asynchronous Pattern (TAP) is based on System.Threading.Tasks.Task and System.Threading.Tasks.Task<TResult> types in the System.Threading.Tasks namespaces.
When we’re developing web application, if we need to develop async applications on web side, we can use this libraries to reflect async behavior. For Instance, if we want to communicate external services or channels at the same time, we might use async controller to avoid operation blocks. When we’re reflecting this behavior in an application, We should think tasks in action level. I mean, surely according to our scenario, if we seperate all actions as different tasks, it can be more useful and manageble.
In .Net Framework MVC Applications, We can use Async Controllers and async actions. For instance please check code below :
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
public class HomeController : AsyncController
{
public NorthwindEntities NorthwindContext { get; set; }
public HomeController()
{
NorthwindContext = new NorthwindEntities();
}
public async Task<ActionResult> Index()
{
var indexViewModel = new IndexViewModel();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
indexViewModel.Categories = await Categories();
indexViewModel.Shippers = await Shippers();
indexViewModel.Currencies = await Currencies();
stopwatch.Stop();
indexViewModel.TotalTime = stopwatch.Elapsed;
return View(indexViewModel);
}
public async Task<List<SelectListItem>> Shippers()
{
var shippers = await NorthwindContext.Shippers.Select(x => new SelectListItem()
{
Text = x.CompanyName,
Value = x.ShipperID.ToString()
}).ToListAsync();
return shippers;
}
public async Task<List<SelectListItem>> Categories()
{
var shippers = await NorthwindContext.Categories.Select(x => new SelectListItem()
{
Text = x.CategoryName,
Value = x.CategoryID.ToString()
}).ToListAsync();
return shippers;
}
public async Task<string> Currencies()
{
WebClient client = new WebClient();
var exchange = await client.DownloadStringTaskAsync("http://www.tcmb.gov.tr/kurlar/today.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(exchange);
string USDSELL = xmlDoc.SelectSingleNode("Tarih_Date/Currency[@Kod='USD']/BanknoteSelling").InnerXml;
string USDBUY = xmlDoc.SelectSingleNode("Tarih_Date/Currency[@Kod='USD']/BanknoteBuying").InnerXml;
return $"USD Banknote Selling :{USDSELL} - Banknote Buying :{USDBUY}";
}
}
public class IndexViewModel
{
public TimeSpan TotalTime { get; set; }
public List<SelectListItem> Categories { get; set; }
public List<SelectListItem> Shippers { get; set; }
public string Currencies { get; set; }
}
|
And also, if you need to run multiple requests concurrently, use SessionLess controller :
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[SessionState(SessionStateBehavior.Disabled)]
public class HomeController : Controller
{
public NorthwindEntities NorthwindContext { get; set; }
public HomeController()
{
NorthwindContext = new NorthwindEntities();
}
public async Task<ActionResult> Index()
{
var indexViewModel = new IndexViewModel();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
indexViewModel.Categories = await Categories();
indexViewModel.Shippers = await Shippers();
indexViewModel.Currencies = await Currencies();
stopwatch.Stop();
indexViewModel.TotalTime = stopwatch.Elapsed;
return View(indexViewModel);
}
public async Task<List<SelectListItem>> Shippers()
{
var shippers = await NorthwindContext.Shippers.Select(x => new SelectListItem()
{
Text = x.CompanyName,
Value = x.ShipperID.ToString()
}).ToListAsync();
return shippers;
}
public async Task<List<SelectListItem>> Categories()
{
var shippers = await NorthwindContext.Categories.Select(x => new SelectListItem()
{
Text = x.CategoryName,
Value = x.CategoryID.ToString()
}).ToListAsync();
return shippers;
}
public async Task<string> Currencies()
{
WebClient client = new WebClient();
var exchange = await client.DownloadStringTaskAsync("http://www.tcmb.gov.tr/kurlar/today.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(exchange);
string USDSELL = xmlDoc.SelectSingleNode("Tarih_Date/Currency[@Kod='USD']/BanknoteSelling").InnerXml;
string USDBUY = xmlDoc.SelectSingleNode("Tarih_Date/Currency[@Kod='USD']/BanknoteBuying").InnerXml;
return $"USD Banknote Selling :{USDSELL} - Banknote Buying :{USDBUY}";
}
}
public class IndexViewModel
{
public TimeSpan TotalTime { get; set; }
public List<SelectListItem> Categories { get; set; }
public List<SelectListItem> Shippers { get; set; }
public string Currencies { get; set; }
}
|
AST
This entry was posted in .Net Framework, ASP.Net, Design Patterns, MVC, Server Side Web Programming,Web Programming and tagged async, asynchronous operations, controller, mvc 5, sessionless, tap, task based asynchronous pattern on July 4, 2016.