This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNServiceExec.cs
More file actions
190 lines (158 loc) · 5.12 KB
/
NServiceExec.cs
File metadata and controls
190 lines (158 loc) · 5.12 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Reflection;
using SimpleStack.Serializers;
using SimpleStack.Interfaces;
using SimpleStack.Extensions;
using System.Linq.Expressions;
namespace SimpleStack
{
public interface INServiceExec
{
object Execute(IRequestContext requestContext, object instance, object request);
}
public class NServiceRequestExec<TService, TRequest> : INServiceExec
{
static NServiceRequestExec()
{
try
{
NServiceExec<TService>.CreateServiceRunnersFor<TRequest>();
}
catch (Exception ex)
{
ex.Message.Print();
throw;
}
}
public object Execute(IRequestContext requestContext, object instance, object request)
{
return NServiceExec<TService>.Execute(requestContext, instance, request,
typeof(TRequest).Name);
}
}
public static class NServiceExecExtensions
{
public static IEnumerable<MethodInfo> GetActions(this Type serviceType)
{
foreach (var mi in serviceType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (mi.GetParameters().Length != 1)
continue;
var actionName = mi.Name.ToUpper();
if (!HttpMethods.AllVerbs.Contains(actionName) && actionName != ActionContext.AnyAction)
continue;
yield return mi;
}
}
}
public class NServiceExec<TService>
{
private static Dictionary<Type, List<ActionContext>> actionMap
= new Dictionary<Type, List<ActionContext>>();
private static Dictionary<string, InstanceExecFn> execMap
= new Dictionary<string, InstanceExecFn>();
static NServiceExec()
{
foreach (var mi in typeof(TService).GetActions())
{
var actionName = mi.Name.ToUpper();
var args = mi.GetParameters();
var requestType = args[0].ParameterType;
var actionCtx = new ActionContext {
Id = ActionContext.Key(actionName, requestType.Name),
RequestType = requestType,
};
try
{
actionCtx.ServiceAction = CreateExecFn(requestType, mi);
}
catch
{
//Potential problems with MONO, using reflection for fallback
actionCtx.ServiceAction = (service, request) =>
mi.Invoke(service, new[] { request });
}
//TODO: vdaron : Remove Requets Filter support
/*
var reqFilters = new List<IHasRequestFilter>();
var resFilters = new List<IHasResponseFilter>();
foreach (var attr in mi.GetCustomAttributes(false))
{
var hasReqFilter = attr as IHasRequestFilter;
var hasResFilter = attr as IHasResponseFilter;
if (hasReqFilter != null)
reqFilters.Add(hasReqFilter);
if (hasResFilter != null)
resFilters.Add(hasResFilter);
}
if (reqFilters.Count > 0)
actionCtx.RequestFilters = reqFilters.ToArray();
if (resFilters.Count > 0)
actionCtx.ResponseFilters = resFilters.ToArray();
*/
if (!actionMap.ContainsKey(requestType))
actionMap[requestType] = new List<ActionContext>();
actionMap[requestType].Add(actionCtx);
}
}
public static ActionInvokerFn CreateExecFn(Type requestType, MethodInfo mi)
{
var serviceType = typeof(TService);
var serviceParam = Expression.Parameter(typeof(object), "serviceObj");
var serviceStrong = Expression.Convert(serviceParam, serviceType);
var requestDtoParam = Expression.Parameter(typeof(object), "requestDto");
var requestDtoStrong = Expression.Convert(requestDtoParam, requestType);
Expression callExecute = Expression.Call(
serviceStrong, mi, requestDtoStrong);
if (mi.ReturnType != typeof(void))
{
var executeFunc = Expression.Lambda<ActionInvokerFn>
(callExecute, serviceParam, requestDtoParam).Compile();
return executeFunc;
}
else
{
var executeFunc = Expression.Lambda<VoidActionInvokerFn>
(callExecute, serviceParam, requestDtoParam).Compile();
return (service, request) => {
executeFunc(service, request);
return null;
};
}
}
public static List<ActionContext> GetActionsFor<TRequest>()
{
List<ActionContext> requestActions;
return actionMap.TryGetValue(typeof(TRequest), out requestActions)
? requestActions
: new List<ActionContext>();
}
public static void CreateServiceRunnersFor<TRequest>()
{
foreach (var actionCtx in GetActionsFor<TRequest>())
{
if (execMap.ContainsKey(actionCtx.Id)) continue;
var serviceRunner = EndpointHost.CreateServiceRunner<TRequest>(actionCtx);
execMap[actionCtx.Id] = serviceRunner.Process;
}
}
public static object Execute(IRequestContext requestContext,
object instance, object request, string requestName)
{
var actionName = requestContext != null
? requestContext.Get<IHttpRequest>().HttpMethod
: HttpMethods.Post; //MQ Services
InstanceExecFn action;
if (execMap.TryGetValue(ActionContext.Key(actionName, requestName), out action)
|| execMap.TryGetValue(ActionContext.AnyKey(requestName), out action))
{
return action(requestContext, instance, request);
}
var expectedMethodName = actionName.Substring(0, 1) + actionName.Substring(1).ToLower();
throw new NotImplementedException(
"Could not find method named {1}({0}) or Any({0}) on Service {2}"
.Fmt(request.GetType().Name, expectedMethodName, typeof(TService).Name));
}
}
}