forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainModule.cs
More file actions
67 lines (54 loc) · 2 KB
/
Copy pathMainModule.cs
File metadata and controls
67 lines (54 loc) · 2 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
namespace Nancy.Demo.Async
{
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class MainModule : NancyModule
{
public MainModule()
{
Before += async (ctx, ct) =>
{
this.AddToLog("Before Hook Delay\n");
await Task.Delay(5000);
return null;
};
After += async (ctx, ct) =>
{
this.AddToLog("After Hook Delay\n");
await Task.Delay(5000);
this.AddToLog("After Hook Complete\n");
ctx.Response = this.GetLog();
};
Get["/", true] = async (x, ct) =>
{
this.AddToLog("Delay 1\n");
await Task.Delay(1000);
this.AddToLog("Delay 2\n");
await Task.Delay(1000);
this.AddToLog("Executing async http client\n");
var client = new HttpClient();
var res = await client.GetAsync("http://nancyfx.org");
var content = await res.Content.ReadAsStringAsync();
this.AddToLog("Response: " + content.Split('\n')[0] + "\n");
return (Response)this.GetLog();
};
}
private void AddToLog(string logLine)
{
if (!this.Context.Items.ContainsKey("Log"))
{
this.Context.Items["Log"] = string.Empty;
}
this.Context.Items["Log"] = (string)this.Context.Items["Log"] + DateTime.Now.ToLongTimeString() + " : " + logLine;
}
private string GetLog()
{
if (!this.Context.Items.ContainsKey("Log"))
{
this.Context.Items["Log"] = string.Empty;
}
return (string)this.Context.Items["Log"];
}
}
}