forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNancyEngineExtensions.cs
More file actions
88 lines (81 loc) · 4.09 KB
/
Copy pathNancyEngineExtensions.cs
File metadata and controls
88 lines (81 loc) · 4.09 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
namespace Nancy
{
using System;
using System.Threading;
using Nancy.Helpers;
public static class NancyEngineExtensions
{
/// <summary>
/// Handles an incoming <see cref="Request"/>.
/// </summary>
/// <param name="nancyEngine">The <see cref="INancyEngine"/> instance.</param>
/// <param name="request">An <see cref="Request"/> instance, containing the information about the current request.</param>
/// <returns>A <see cref="NancyContext"/> instance containing the request/response context.</returns>
public static NancyContext HandleRequest(this INancyEngine nancyEngine, Request request)
{
return HandleRequest(nancyEngine, request, context => context);
}
/// <summary>
/// Handles an incoming <see cref="Request"/>.
/// </summary>
/// <param name="nancyEngine">The <see cref="INancyEngine"/> instance.</param>
/// <param name="request">An <see cref="Request"/> instance, containing the information about the current request.</param>
/// <param name="preRequest">Delegate to call before the request is processed</param>
/// <returns>A <see cref="NancyContext"/> instance containing the request/response context.</returns>
public static NancyContext HandleRequest(this INancyEngine nancyEngine, Request request, Func<NancyContext, NancyContext> preRequest)
{
var task = nancyEngine.HandleRequest(request, preRequest, CancellationToken.None);
try
{
task.Wait();
}
catch (Exception ex)
{
var flattenedException = NancyEngine.FlattenException(ex);
throw flattenedException;
}
return task.Result;
}
/// <summary>
/// Handles an incoming <see cref="Request"/> async.
/// </summary>
/// <param name="nancyEngine">The <see cref="INancyEngine"/> instance.</param>
/// <param name="request">An <see cref="Request"/> instance, containing the information about the current request.</param>
/// <param name="preRequest">Delegate to call before the request is processed</param>
/// <param name="onComplete">Delegate to call when the request is complete</param>
/// <param name="onError">Delegate to call when any errors occur</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
public static void HandleRequest(
this INancyEngine nancyEngine,
Request request,
Func<NancyContext, NancyContext> preRequest,
Action<NancyContext> onComplete,
Action<Exception> onError,
CancellationToken cancellationToken)
{
if (nancyEngine == null)
{
throw new ArgumentNullException("nancyEngine");
}
nancyEngine
.HandleRequest(request, preRequest, cancellationToken)
.WhenCompleted(t => onComplete(t.Result), t => onError(t.Exception));
//this.HandleRequest(request, null, onComplete, onError);
}
/// <summary>
/// Handles an incoming <see cref="Request"/> async.
/// </summary>
/// <param name="nancyEngine">The <see cref="INancyEngine"/> instance.</param>
/// <param name="request">An <see cref="Request"/> instance, containing the information about the current request.</param>
/// <param name="onComplete">Delegate to call when the request is complete</param>
/// <param name="onError">Delegate to call when any errors occur</param>
public static void HandleRequest(
this INancyEngine nancyEngine,
Request request,
Action<NancyContext> onComplete,
Action<Exception> onError)
{
HandleRequest(nancyEngine, request, null, onComplete, onError, CancellationToken.None);
}
}
}