/
AppSelfHostBase.cs
176 lines (142 loc) · 5.7 KB
/
AppSelfHostBase.cs
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
using System;
using System.Reflection;
using System.Threading.Tasks;
using System.Collections.Generic;
using ServiceStack.Logging;
using ServiceStack.NetCore;
using ServiceStack.Text;
using ServiceStack.Host;
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.Host.NetCore;
using ServiceStack.Web;
namespace ServiceStack
{
public abstract class AppSelfHostBase : ServiceStackHost
{
protected AppSelfHostBase(string serviceName, params Assembly[] assembliesWithServices)
: base(serviceName, assembliesWithServices)
{
Platforms.PlatformNetCore.HostInstance = this;
}
IApplicationBuilder app;
public virtual void Bind(IApplicationBuilder app)
{
this.app = app;
AppHostBase.BindHost(this, app);
app.Use(ProcessRequest);
}
public override void OnConfigLoad()
{
if (app != null)
{
//Initialize VFS
var env = app.ApplicationServices.GetService<IHostingEnvironment>();
Config.WebHostPhysicalPath = env.WebRootPath ?? env.ContentRootPath;
AppHostBase.RegisterLicenseFromAppSettings(AppSettings);
Config.MetadataRedirectPath = "metadata";
}
}
public virtual Task ProcessRequest(HttpContext context, Func<Task> next)
{
//Keep in sync with AppHostBase.NetCore.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 ServiceStackHost Init()
{
return this; //Run Init() after Bind()
}
protected void RealInit()
{
base.Init();
}
public override ServiceStackHost Start(string urlBase)
{
return Start(new[] { urlBase });
}
public IWebHost WebHost { get; private set; }
public virtual ServiceStackHost Start(string[] urlBases)
{
this.WebHost = ConfigureHost(new WebHostBuilder(), urlBases).Build();
this.WebHost.Start();
return this;
}
public virtual IWebHostBuilder ConfigureHost(IWebHostBuilder host, string[] urlBases)
{
return host.UseKestrel()
.UseContentRoot(System.IO.Directory.GetCurrentDirectory())
.UseWebRoot(System.IO.Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(urlBases);
}
/// <summary>
/// Override to Configure .NET Core dependencies
/// </summary>
public virtual void ConfigureServices(IServiceCollection services) {}
/// <summary>
/// Override to Confgiure .NET Core App
/// </summary>
public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env) {}
public static AppSelfHostBase HostInstance => (AppSelfHostBase)Platforms.PlatformNetCore.HostInstance;
protected class Startup
{
public void ConfigureServices(IServiceCollection services) =>
HostInstance.ConfigureServices(services);
public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
HostInstance.Configure(app, env);
HostInstance.Bind(app);
HostInstance.RealInit();
}
}
public override IRequest TryGetCurrentRequest()
{
return AppHostBase.GetOrCreateRequest(app.ApplicationServices.GetService<IHttpContextAccessor>());
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.WebHost?.Dispose();
this.WebHost = null;
}
base.Dispose(disposing);
}
}
}