-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestProcessor.cs
More file actions
100 lines (87 loc) · 2.88 KB
/
RequestProcessor.cs
File metadata and controls
100 lines (87 loc) · 2.88 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace httpTester
{
public abstract class BaseRequestProcessor
{
public int SleepTime
{
get;
set;
}
protected HttpContext _context;
public BaseRequestProcessor(HttpContext context)
{
_context = context;
}
public abstract void Render();
}
public class RequestProcessorAsync : BaseRequestProcessor
{
public RequestProcessorAsync (HttpContext context)
:base(context)
{
}
public override void Render()
{
var st = _context.Request.QueryString["SleepTime"];
if (st != null)
{
this.SleepTime = Convert.ToInt32(st);
_context.Response.Write("SleepTime = "+this.SleepTime+Environment.NewLine);
}
else
{
_context.Response.Write(_context.Request.QueryString);
}
_context.Response.Write("Begin Async Call" + Environment.NewLine);
//AttachedToParent empêche la requête HTTP de se finir avant la tâche.
var res = Task.Factory.StartNew(DoLongMethod,TaskCreationOptions.AttachedToParent).ContinueWith((t) =>
{
_context.Response.Write("Return to Main Thread" + Environment.NewLine);
},TaskContinuationOptions.AttachedToParent
);
_context.Response.Write("Main Method ended"+Environment.NewLine);
}
private void DoLongMethod()
{
Thread.Sleep(SleepTime);
_context.Response.StatusCode = 254;
_context.Response.Write("Async Call Ended" + Environment.NewLine);
}
}
public class RequestProcessor : BaseRequestProcessor
{
public RequestProcessor(HttpContext context)
:base(context)
{
}
public override void Render()
{
var st = _context.Request.QueryString["SleepTime"];
if (st != null)
{
this.SleepTime = Convert.ToInt32(st);
_context.Response.Write("SleepTime = "+this.SleepTime+Environment.NewLine);
}
else
{
_context.Response.Write(_context.Request.QueryString);
}
_context.Response.Write("Begin Sync Call" + Environment.NewLine);
DoLongMethod();
_context.Response.Write("Main Method ended"+Environment.NewLine);
}
private void DoLongMethod()
{
Thread.Sleep(SleepTime);
_context.Response.StatusCode = 254;
_context.Response.Write("Long Method Ended" + Environment.NewLine);
}
}
}