/
CustomResponseHandler.cs
56 lines (43 loc) · 1.84 KB
/
CustomResponseHandler.cs
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
using System;
using System.Threading.Tasks;
using ServiceStack.Web;
namespace ServiceStack.Host.Handlers;
public class CustomResponseHandler : HttpAsyncTaskHandler
{
public Func<IRequest, IResponse, object> Action { get; set; }
public CustomResponseHandler(Func<IRequest, IResponse, object> action, string operationName = null)
{
Action = action ?? throw new ArgumentNullException(nameof(action));
RequestName = operationName ?? "CustomResponse";
}
public override void ProcessRequest(IRequest httpReq, IResponse httpRes, string operationName)
{
if (Action == null)
throw new Exception("Action was not supplied to ActionHandler");
if (HostContext.ApplyCustomHandlerRequestFilters(httpReq, httpRes))
return;
httpReq.OperationName ??= RequestName;
var response = Action(httpReq, httpRes);
httpRes.WriteToResponse(httpReq, response);
}
}
public class CustomResponseHandlerAsync : HttpAsyncTaskHandler
{
public Func<IRequest, IResponse, Task<object>> Action { get; set; }
public CustomResponseHandlerAsync(Func<IRequest, IResponse, Task<object>> action, string operationName = null)
{
Action = action ?? throw new ArgumentNullException(nameof(action));
RequestName = operationName ?? "CustomResponse";
}
public override async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
{
if (Action == null)
throw new Exception("Action was not supplied to ActionHandler");
if (HostContext.ApplyCustomHandlerRequestFilters(httpReq, httpRes))
return;
if (httpReq.OperationName == null)
httpReq.OperationName = RequestName;
var response = await Action(httpReq, httpRes);
await httpRes.WriteToResponse(httpReq, response);
}
}