-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathAppHostBase.NetCore.cs
More file actions
447 lines (369 loc) · 16.9 KB
/
Copy pathAppHostBase.NetCore.cs
File metadata and controls
447 lines (369 loc) · 16.9 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#if NETCORE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ServiceStack.Web;
using ServiceStack.Logging;
using ServiceStack.NetCore;
using ServiceStack.Host;
using ServiceStack.Host.NetCore;
using ServiceStack.Host.Handlers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack.Configuration;
using ServiceStack.IO;
using ServiceStack.Text;
using Microsoft.AspNetCore.Hosting;
#if NETSTANDARD2_0
using IHostApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;
using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#else
using Microsoft.Extensions.Hosting;
using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IWebHostEnvironment;
using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IHostApplicationLifetime;
#endif
namespace ServiceStack
{
public abstract class AppHostBase : ServiceStackHost, IAppHostNetCore, IConfigureServices, IRequireConfiguration
{
protected AppHostBase(string serviceName, params Assembly[] assembliesWithServices)
: base(serviceName, assembliesWithServices)
{
Platforms.PlatformNetCore.HostInstance = this;
//IIS Mapping / sometimes UPPER CASE https://serverfault.com/q/292335
var iisPathBase = Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH");
if (!string.IsNullOrEmpty(iisPathBase) && !iisPathBase.Any(char.IsLower))
iisPathBase = iisPathBase.ToLower();
PathBase = iisPathBase;
}
private string pathBase;
public override string PathBase
{
get => pathBase ?? Config?.HandlerFactoryPath;
set
{
if (!string.IsNullOrEmpty(value))
{
if (value[0] != '/')
throw new Exception("PathBase must start with '/'");
pathBase = value.TrimEnd('/');
}
else
{
pathBase = null;
}
}
}
IApplicationBuilder app;
public IApplicationBuilder App => app;
public IServiceProvider ApplicationServices => app?.ApplicationServices;
public Func<NetCoreRequest, Task> BeforeNextMiddleware { get; set; }
public virtual void Bind(IApplicationBuilder app)
{
this.app = app;
if (!string.IsNullOrEmpty(PathBase))
{
this.app.UsePathBase(PathBase);
}
BindHost(this, app);
app.Use(ProcessRequest);
}
public override void ConfigureLogging()
{
var logFactory = app?.ApplicationServices.GetService<ILoggerFactory>();
if (logFactory != null)
{
NetCoreLogFactory.FallbackLoggerFactory = logFactory;
if (LogManager.LogFactory.IsNullOrNullLogFactory())
LogManager.LogFactory = new NetCoreLogFactory(logFactory);
}
base.ConfigureLogging();
}
public static void BindHost(ServiceStackHost appHost, IApplicationBuilder app)
{
appHost.ConfigureLogging();
appHost.Container.Adapter = new NetCoreContainerAdapter(app.ApplicationServices);
// Auto populate AppSettings with NetCoreAppSettings(IConfiguration)
var configuration = app.ApplicationServices.GetService<IConfiguration>();
if (configuration != null)
{
if (appHost.AppSettings is AppSettings) // override if default
appHost.AppSettings = new NetCoreAppSettings(configuration);
}
else
{
configuration = (appHost.AppSettings as NetCoreAppSettings)?.Configuration;
if (appHost is IRequireConfiguration requiresConfig)
requiresConfig.Configuration = configuration;
}
var appLifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
appLifetime?.ApplicationStopping.Register(appHost.OnApplicationStopping);
}
/// <summary>
/// The FilePath used in Virtual File Sources
/// </summary>
public override string GetWebRootPath()
{
if (app == null)
return base.GetWebRootPath();
return HostingEnvironment.WebRootPath ?? HostingEnvironment.ContentRootPath;
}
private IWebHostEnvironment env;
public IWebHostEnvironment HostingEnvironment =>
env ??= app?.ApplicationServices.GetService<IWebHostEnvironment>();
public override void OnConfigLoad()
{
base.OnConfigLoad();
if (app != null)
{
Config.DebugMode = HostingEnvironment.IsDevelopment();
Config.HandlerFactoryPath = PathBase?.TrimStart('/');
//Initialize VFS
Config.WebHostPhysicalPath = HostingEnvironment.ContentRootPath;
if (VirtualFiles == null)
{
//Set VirtualFiles to point to ContentRootPath (Project Folder)
VirtualFiles = new FileSystemVirtualFiles(HostingEnvironment.ContentRootPath);
}
RegisterLicenseFromAppSettings(AppSettings);
InjectRequestContext = app?.ApplicationServices.GetService<IHttpContextAccessor>() != null;
}
}
public static void RegisterLicenseFromAppSettings(IAppSettings appSettings)
{
//Automatically register license key stored in <appSettings/>
var licenceKeyText = appSettings.GetString(NetStandardPclExport.AppSettingsKey);
if (!string.IsNullOrEmpty(licenceKeyText))
{
LicenseUtils.RegisterLicense(licenceKeyText);
}
}
public Func<HttpContext, Task<bool>> NetCoreHandler { get; set; }
public bool InjectRequestContext { get; set; }
public virtual async Task ProcessRequest(HttpContext context, Func<Task> next)
{
if (NetCoreHandler != null)
{
var handled = await NetCoreHandler(context).ConfigAwait();
if (handled)
return;
}
//Keep in sync with Kestrel/AppSelfHostBase.cs
var operationName = context.Request.GetOperationName() ?? "Home"; //already decoded
var pathInfo = context.Request.Path.HasValue
? context.Request.Path.Value
: "/";
var mode = Config.HandlerFactoryPath;
if (!string.IsNullOrEmpty(mode))
{
//IIS Reports "ASPNETCORE_APPL_PATH" in UPPER CASE
var includedInPathInfo = pathInfo.IndexOf(mode, StringComparison.OrdinalIgnoreCase) == 1;
var includedInPathBase = context.Request.PathBase.HasValue &&
context.Request.PathBase.Value.IndexOf(mode,
StringComparison.OrdinalIgnoreCase) == 1;
if (!includedInPathInfo && !includedInPathBase)
{
await next().ConfigAwait();
return;
}
if (includedInPathInfo)
pathInfo = pathInfo.Substring(mode.Length + 1);
}
RequestContext.Instance.StartRequestContext();
NetCoreRequest httpReq;
IResponse httpRes;
IHttpHandler handler;
try
{
httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
httpReq.RequestAttributes = httpReq.GetAttributes() | RequestAttributes.Http;
httpRes = httpReq.Response;
handler = HttpHandlerFactory.GetHandler(httpReq);
if (InjectRequestContext)
context.Items[Keywords.IRequest] = httpReq;
if (BeforeNextMiddleware != null)
{
var holdNext = next;
next = async () => {
await BeforeNextMiddleware(httpReq).ConfigAwait();
await holdNext().ConfigAwait();
};
}
}
catch (Exception ex) //Request Initialization error
{
var logFactory = context.Features.Get<ILoggerFactory>();
if (logFactory != null)
{
var log = logFactory.CreateLogger(GetType());
log.LogError(default, ex, ex.Message);
}
context.Response.ContentType = MimeTypes.PlainText;
await context.Response.WriteAsync($"{ex.GetType().Name}: {ex.Message}").ConfigAwait();
if (Config.DebugMode)
await context.Response.WriteAsync($"\nStackTrace:\n{ex.StackTrace}").ConfigAwait();
return;
}
if (handler is IServiceStackHandler serviceStackHandler)
{
if (serviceStackHandler is NotFoundHttpHandler)
{
await next().ConfigAwait();
return;
}
if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
operationName = serviceStackHandler.RequestName;
if (serviceStackHandler is RestHandler restHandler)
{
httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
}
try
{
await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName).ConfigAwait();
}
catch (Exception ex)
{
var logFactory = context.Features.Get<ILoggerFactory>();
if (logFactory != null)
{
var log = logFactory.CreateLogger(GetType());
log.LogError(default, ex, ex.Message);
}
}
finally
{
await httpRes.CloseAsync().ConfigAwait();
}
//Matches Exceptions handled in HttpListenerBase.InitTask()
return;
}
await next().ConfigAwait();
}
public override string MapProjectPath(string relativePath)
{
if (HostingEnvironment?.ContentRootPath != null && relativePath?.StartsWith("~") == true)
return Path.GetFullPath(HostingEnvironment.ContentRootPath.CombineWith(relativePath.Substring(1)));
return relativePath.MapHostAbsolutePath();
}
public override IRequest TryGetCurrentRequest()
{
return GetOrCreateRequest(app.ApplicationServices.GetService<IHttpContextAccessor>());
}
/// <summary>
/// Creates an IRequest from IHttpContextAccessor if it's been registered as a singleton
/// </summary>
public static IRequest GetOrCreateRequest(IHttpContextAccessor httpContextAccessor)
{
return GetOrCreateRequest(httpContextAccessor?.HttpContext);
}
public static IRequest GetOrCreateRequest(HttpContext httpContext)
{
if (httpContext != null)
{
if (httpContext.Items.TryGetValue(Keywords.IRequest, out var oRequest))
return (IRequest)oRequest;
var req = httpContext.ToRequest();
httpContext.Items[Keywords.IRequest] = req;
return req;
}
return null;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing) LogManager.LogFactory = null;
}
/// <summary>
/// Requires using ModularStartup
/// </summary>
/// <param name="services"></param>
public virtual void Configure(IServiceCollection services) { }
public IConfiguration Configuration { get; set; }
}
public interface IAppHostNetCore : IAppHost, IRequireConfiguration
{
IApplicationBuilder App { get; }
#if NETSTANDARD2_0
IWebHostEnvironment HostingEnvironment { get; }
#else
IWebHostEnvironment HostingEnvironment { get; }
#endif
}
public static class NetCoreAppHostExtensions
{
/// <summary>
/// Register static callbacks fired just after AppHost.Configure()
/// <param name="beforeConfigure">Register static callbacks fired just before AppHost.Configure()</param>
/// <param name="afterConfigure">Register static callbacks fired just after AppHost.Configure()</param>
/// <param name="afterPluginsLoaded">Register static callbacks fired just after plugins are loaded</param>
/// <param name="afterAppHostInit">Register static callbacks fired after the AppHost is initialized</param>
/// </summary>
public static IWebHostBuilder ConfigureAppHost(this IWebHostBuilder builder,
Action<ServiceStackHost> beforeConfigure = null,
Action<ServiceStackHost> afterConfigure = null,
Action<ServiceStackHost> afterPluginsLoaded = null,
Action<ServiceStackHost> afterAppHostInit = null)
{
HostContext.ConfigureAppHost(
beforeConfigure:beforeConfigure,
afterConfigure:afterConfigure,
afterPluginsLoaded:afterPluginsLoaded,
afterAppHostInit:afterAppHostInit);
return builder;
}
public static IConfiguration GetConfiguration(this IAppHost appHost) =>
((IAppHostNetCore)appHost).Configuration;
public static IApplicationBuilder GetApp(this IAppHost appHost) => ((IAppHostNetCore)appHost).App;
public static IServiceProvider GetApplicationServices(this IAppHost appHost) =>
((IAppHostNetCore)appHost).App.ApplicationServices;
public static IWebHostEnvironment GetHostingEnvironment(this IAppHost appHost) =>
((IAppHostNetCore)appHost).HostingEnvironment;
public static bool IsDevelopmentEnvironment(this IAppHost appHost) =>
appHost.GetHostingEnvironment().EnvironmentName == "Development";
public static bool IsStagingEnvironment(this IAppHost appHost) =>
appHost.GetHostingEnvironment().EnvironmentName == "Staging";
public static bool IsProductionEnvironment(this IAppHost appHost) =>
appHost.GetHostingEnvironment().EnvironmentName == "Production";
public static IApplicationBuilder UseServiceStack(this IApplicationBuilder app, AppHostBase appHost)
{
// Manually simulating Modular Startup when using .NET 6+ top-level statements app builder
if (TopLevelAppModularStartup.Instance != null)
{
TopLevelAppModularStartup.Instance.Configure(app);
}
appHost.Bind(app);
appHost.Init();
return app;
}
public static IApplicationBuilder Use(this IApplicationBuilder app, IHttpAsyncHandler httpHandler)
{
return app.Use(httpHandler.Middleware);
}
public static IHttpRequest ToRequest(this HttpContext httpContext, string operationName = null)
{
var req = new NetCoreRequest(httpContext, operationName, RequestAttributes.None);
req.RequestAttributes = req.GetAttributes() | RequestAttributes.Http;
return req;
}
public static T TryResolve<T>(this IServiceProvider provider) => provider.GetService<T>();
public static T Resolve<T>(this IServiceProvider provider) => provider.GetRequiredService<T>();
public static IHttpRequest ToRequest(this HttpRequest request, string operationName = null) =>
request.HttpContext.ToRequest();
public static T TryResolveScoped<T>(this IRequest req) => (T)((IServiceProvider)req).GetService(typeof(T));
public static object TryResolveScoped(this IRequest req, Type type) => ((IServiceProvider)req).GetService(type);
public static T ResolveScoped<T>(this IRequest req) => (T)((IServiceProvider)req).GetRequiredService(typeof(T));
public static object ResolveScoped(this IRequest req, Type type) =>
((IServiceProvider)req).GetRequiredService(type);
public static IServiceScope CreateScope(this IRequest req) => ((IServiceProvider)req).CreateScope();
public static IEnumerable<object> GetServices(this IRequest req, Type type) =>
((IServiceProvider)req).GetServices(type);
public static IEnumerable<T> GetServices<T>(this IRequest req) => ((IServiceProvider)req).GetServices<T>();
}
}
#endif