FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ServiceStack/src/ServiceStack/AppHostBase.NetCore.cs at master · csharpbuilder/ServiceStack · GitHub
csharpbuilder
/
ServiceStack
Public
forked from
ServiceStack/ServiceStack
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ServiceStack
/
src
/
ServiceStack
/
AppHostBase.NetCore.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (222 loc) · 9.13 KB
Breadcrumbs
ServiceStack
/
src
/
ServiceStack
/
AppHostBase.NetCore.cs
Copy path
File metadata and controls
263 lines (222 loc) · 9.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#if
NETSTANDARD2_0
using
System
;
using
System
.
IO
;
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
.
AspNetCore
.
Hosting
;
using
Microsoft
.
Extensions
.
DependencyInjection
;
using
ServiceStack
.
Configuration
;
using
ServiceStack
.
IO
;
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
(
)
{
base
.
OnConfigLoad
(
)
;
if
(
app
!=
null
)
{
//Initialize VFS
var
env
=
app
.
ApplicationServices
.
GetService
<
IHostingEnvironment
>
(
)
;
Config
.
WebHostPhysicalPath
=
env
.
ContentRootPath
;
Config
.
DebugMode
=
env
.
IsDevelopment
(
)
;
//Set VirtualFiles to point to ContentRootPath (Project Folder)
VirtualFiles
=
new
FileSystemVirtualFiles
(
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
Func
<
HttpContext
,
Task
<
bool
>
>
NetCoreHandler
{
get
;
set
;
}
public
virtual
async
Task
ProcessRequest
(
HttpContext
context
,
Func
<
Task
>
next
)
{
if
(
NetCoreHandler
!=
null
)
{
var
handled
=
await
NetCoreHandler
(
context
)
;
if
(
handled
)
return
;
}
//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
)
)
{
var
includedInPathInfo
=
pathInfo
.
IndexOf
(
mode
,
StringComparison
.
Ordinal
)
==
1
;
var
includedInPathPase
=
context
.
Request
.
PathBase
.
HasValue
&&
context
.
Request
.
PathBase
.
Value
.
IndexOf
(
mode
,
StringComparison
.
Ordinal
)
==
1
;
if
(
!
includedInPathInfo
&&
!
includedInPathPase
)
{
await
next
(
)
;
return
;
}
if
(
includedInPathInfo
)
pathInfo
=
pathInfo
.
Substring
(
mode
.
Length
+
1
)
;
}
RequestContext
.
Instance
.
StartRequestContext
(
)
;
NetCoreRequest
httpReq
;
IResponse
httpRes
;
System
.
Web
.
IHttpHandler
handler
;
try
{
httpReq
=
new
NetCoreRequest
(
context
,
operationName
,
RequestAttributes
.
None
,
pathInfo
)
;
httpReq
.
RequestAttributes
=
httpReq
.
GetAttributes
(
)
|
RequestAttributes
.
Http
;
httpRes
=
httpReq
.
Response
;
handler
=
HttpHandlerFactory
.
GetHandler
(
httpReq
)
;
}
catch
(
Exception
ex
)
//Request Initialization error
{
var
logFactory
=
context
.
Features
.
Get
<
ILoggerFactory
>
(
)
;
if
(
logFactory
!=
null
)
{
var
log
=
logFactory
.
CreateLogger
(
GetType
(
)
)
;
log
.
LogError
(
default
(
EventId
)
,
ex
,
ex
.
Message
)
;
}
context
.
Response
.
ContentType
=
MimeTypes
.
PlainText
;
await
context
.
Response
.
WriteAsync
(
$
"
{
ex
.
GetType
(
)
.
Name
}
:
{
ex
.
Message
}
"
)
;
if
(
Config
.
DebugMode
)
await
context
.
Response
.
WriteAsync
(
$
"
\n
StackTrace:
\n
{
ex
.
StackTrace
}
"
)
;
return
;
}
if
(
handler
is
IServiceStackHandler
serviceStackHandler
)
{
if
(
serviceStackHandler
is
NotFoundHttpHandler
)
{
await
next
(
)
;
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
)
;
}
catch
(
Exception
ex
)
{
var
logFactory
=
context
.
Features
.
Get
<
ILoggerFactory
>
(
)
;
if
(
logFactory
!=
null
)
{
var
log
=
logFactory
.
CreateLogger
(
GetType
(
)
)
;
log
.
LogError
(
default
(
EventId
)
,
ex
,
ex
.
Message
)
;
}
}
finally
{
httpRes
.
Close
(
)
;
}
//Matches Exceptions handled in HttpListenerBase.InitTask()
return
;
}
await
next
(
)
;
}
public
override
string
MapProjectPath
(
string
relativePath
)
{
var
env
=
app
?
.
ApplicationServices
.
GetService
<
IHostingEnvironment
>
(
)
;
if
(
env
?
.
ContentRootPath
!=
null
&&
relativePath
?
.
StartsWith
(
"~"
)
==
true
)
return
Path
.
GetFullPath
(
env
.
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
)
;
LogManager
.
LogFactory
=
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
(
)
|
RequestAttributes
.
Http
;
return
req
;
}
public
static
IHttpRequest
ToRequest
(
this
HttpRequest
request
,
string
operationName
=
null
)
=>
request
.
HttpContext
.
ToRequest
(
)
;
}
}
#endif
Back
|
FazBrowse Home
|
New Git URL