Repository navigation
Expand file tree
/
Copy pathAppHostExtensions.cs
More file actions
186 lines (158 loc) · 6.71 KB
/
Copy pathAppHostExtensions.cs
File metadata and controls
186 lines (158 loc) · 6.71 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Funq;
using ServiceStack.Host;
using ServiceStack.Logging;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
public static class AppHostExtensions
{
private static readonly ILog log = LogManager.GetLogger(typeof(AppHostExtensions));
public static void ConfigureOperation<T>(this IAppHost appHost, Action<Operation> configure)
{
if (!appHost.Metadata.ConfigureOperations.TryGetValue(typeof(T), out var handlers))
handlers = appHost.Metadata.ConfigureOperations[typeof(T)] = new();
handlers.Add(configure);
}
public static void RegisterServices(this IAppHost appHost, Dictionary<Type, string[]> serviceRoutes)
{
if (serviceRoutes == null)
return;
foreach (var registerService in serviceRoutes)
{
appHost.RegisterService(registerService.Key, registerService.Value);
}
}
public static Dictionary<Type, string[]> RemoveService<T>(this Dictionary<Type, string[]> serviceRoutes)
{
serviceRoutes?.TryRemove(typeof(T), out _);
return serviceRoutes;
}
public static void RegisterService<TService>(this IAppHost appHost, params string[] atRestPaths)
{
appHost.RegisterService(typeof(TService), atRestPaths);
}
public static void RegisterRequestBinder<TRequest>(this IAppHost appHost, Func<IRequest, object> binder)
{
appHost.RequestBinders[typeof(TRequest)] = binder;
}
public static void AddPluginsFromAssembly(this IAppHost appHost, params Assembly[] assembliesWithPlugins)
{
var ssHost = (ServiceStackHost)appHost;
foreach (var assembly in assembliesWithPlugins)
{
var pluginTypes =
from t in assembly.GetExportedTypes()
where t.GetInterfaces().Any(x => x == typeof(IPlugin))
select t;
foreach (var pluginType in pluginTypes)
{
try
{
if (pluginType.CreateInstance() is IPlugin plugin)
{
ssHost.LoadPlugin(plugin);
}
}
catch (Exception ex)
{
log.Error("Error adding new Plugin " + pluginType.GetOperationName(), ex);
}
}
}
}
public static T GetPlugin<T>(this IAppHost appHost) where T : class, IPlugin
{
return appHost.Plugins.FirstOrDefault(x => x is T) as T;
}
public static T AssertPlugin<T>(this IAppHost appHost) where T : class, IPlugin
{
var plugin = appHost.Plugins.FirstOrDefault(x => x is T) as T;
if (plugin == null)
throw new NotSupportedException($"Required Plugin '{typeof(T).Name}' was not registered");
return plugin;
}
public static bool HasPlugin<T>(this IAppHost appHost) where T : class, IPlugin
{
return appHost.Plugins.FirstOrDefault(x => x is T) != null;
}
public static bool HasMultiplePlugins<T>(this IAppHost appHost) where T : class, IPlugin
{
return appHost.Plugins.Count(x => x is T) > 1;
}
/// <summary>
/// Get an IAppHost container.
/// Note: Registering dependencies should only be done during setup/configuration
/// stage and remain immutable there after for thread-safety.
/// </summary>
/// <param name="appHost"></param>
/// <returns></returns>
public static Container GetContainer(this IAppHost appHost)
{
var hasContainer = appHost as IHasContainer;
return hasContainer?.Container;
}
public static bool NotifyStartupException(this IAppHost appHost, Exception ex, string target, string method)
{
var ssHost = HostContext.AppHost;
if (ssHost == null) return false;
if (!ssHost.HasStarted)
{
ssHost.OnStartupException(ex, target, method);
}
return !ssHost.HasStarted;
}
public static bool NotifyStartupException(this IAppHost appHost, Exception ex)
{
var ssHost = HostContext.AppHost;
if (ssHost == null) return false;
if (!ssHost.HasStarted)
{
ssHost.OnStartupException(ex);
}
return !ssHost.HasStarted;
}
public static string Localize(this string text, IRequest request=null) =>
HostContext.AppHost?.ResolveLocalizedString(text, request) ?? text;
public static string LocalizeFmt(this string text, IRequest request, params object[] args) =>
HostContext.AppHost?.ResolveLocalizedStringFormat(text, args, request) ?? string.Format(text, args);
public static string LocalizeFmt(this string text, params object[] args) =>
HostContext.AppHost?.ResolveLocalizedStringFormat(text, args, request:null) ?? string.Format(text, args);
public static IAppHost Start(this IAppHost appHost, IEnumerable<string> urlBases)
{
#if !NETCORE
var listener = (ServiceStack.Host.HttpListener.HttpListenerBase)appHost;
listener.Start(urlBases);
#endif
return appHost;
}
public static List<IPlugin> AddIfDebug<T>(this List<IPlugin> plugins, T plugin) where T : class, IPlugin
{
if (HostContext.DebugMode)
plugins.Add(plugin);
return plugins;
}
public static List<IPlugin> AddIfNotExists<T>(this List<IPlugin> plugins, T plugin) where T : class, IPlugin =>
plugins.AddIfNotExists(plugin, null);
public static List<IPlugin> AddIfNotExists<T>(this List<IPlugin> plugins, T plugin, Action<T> configure) where T : class, IPlugin
{
var existingPlugin = plugins.FirstOrDefault(x => x is T) as T;
if (existingPlugin == null)
plugins.Add(plugin);
configure?.Invoke(existingPlugin ?? plugin);
return plugins;
}
public static string ResolveStaticBaseUrl(this IAppHost appHost)
{
return (appHost.Config.WebHostUrl ??
(!string.IsNullOrEmpty(appHost.PathBase)
? ("/" + appHost.PathBase).ReplaceAll("//", "/")
: "")).TrimEnd('/');
}
}
}