forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppHostBase.NetCore.cs
More file actions
199 lines (164 loc) · 6.67 KB
/
Copy pathAppHostBase.NetCore.cs
File metadata and controls
199 lines (164 loc) · 6.67 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
#if NETSTANDARD1_6
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using ServiceStack.Text;
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.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack.IO;
using ServiceStack.VirtualPath;
using System.Linq;
using ServiceStack.Configuration;
namespace ServiceStack
{
public abstract class AppHostBase : ServiceStackHost
{
protected AppHostBase(string serviceName, params Assembly[] assembliesWithServices)
: base(serviceName, assembliesWithServices)
{
Platforms.PlatformNetCore.HostInstance = this;
}
IApplicationBuilder app;
public virtual void Bind(IApplicationBuilder app)
{
this.app = app;
BindHost(this, app);
app.Use(ProcessRequest);
}
public static void BindHost(ServiceStackHost appHost, IApplicationBuilder app)
{
var logFactory = app.ApplicationServices.GetService<ILoggerFactory>();
if (logFactory != null)
{
LogManager.LogFactory = new NetCoreLogFactory(logFactory);
}
appHost.Container.Adapter = new NetCoreContainerAdapter(app.ApplicationServices);
}
/// <summary>
/// The FilePath used in Virtual File Sources
/// </summary>
public override string GetWebRootPath()
{
if (app == null)
return base.GetWebRootPath();
var env = app.ApplicationServices.GetService<IHostingEnvironment>();
return env.WebRootPath ?? env.ContentRootPath;
}
public override void OnConfigLoad()
{
if (app != null)
{
//Initialize VFS
var env = app.ApplicationServices.GetService<IHostingEnvironment>();
Config.WebHostPhysicalPath = env.ContentRootPath;
//Set VirtualFiles to point to ContentRootPath (Project Folder)
VirtualFiles = new FileSystemVirtualPathProvider(this, env.ContentRootPath);
RegisterLicenseFromAppSettings(AppSettings);
}
}
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 virtual Task ProcessRequest(HttpContext context, Func<Task> next)
{
//Keep in sync with Kestrel/AppSelfHostBase.cs
var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
var pathInfo = context.Request.Path.HasValue
? context.Request.Path.Value
: "/";
var mode = Config.HandlerFactoryPath;
if (!string.IsNullOrEmpty(mode))
{
if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
return next();
pathInfo = pathInfo.Substring(mode.Length + 1);
}
var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
httpReq.RequestAttributes = httpReq.GetAttributes();
var httpRes = httpReq.Response;
var handler = HttpHandlerFactory.GetHandler(httpReq);
var serviceStackHandler = handler as IServiceStackHandler;
if (serviceStackHandler != null)
{
if (serviceStackHandler is NotFoundHttpHandler)
return next();
if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
operationName = serviceStackHandler.RequestName;
var restHandler = serviceStackHandler as RestHandler;
if (restHandler != null)
{
httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
}
var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
//Matches Exceptions handled in HttpListenerBase.InitTask()
return task;
}
return next();
}
public override string MapProjectPath(string relativePath)
{
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)
{
object oRequest;
if (httpContext.Items.TryGetValue(Keywords.IRequest, out oRequest))
return (IRequest) oRequest;
var req = httpContext.ToRequest();
httpContext.Items[Keywords.IRequest] = req;
return req;
}
return null;
}
}
public static class NetCoreAppHostExtensions
{
public static IApplicationBuilder UseServiceStack(this IApplicationBuilder app, AppHostBase appHost)
{
appHost.Bind(app);
appHost.Init();
return app;
}
public static IApplicationBuilder Use(this IApplicationBuilder app, System.Web.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();
return req;
}
}
}
#endif