/
PlatformNet.HostConfig.cs
198 lines (167 loc) · 6.68 KB
/
PlatformNet.HostConfig.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#if !NETCORE
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Configuration;
using System.Xml.Linq;
using ServiceStack.Configuration;
namespace ServiceStack.Platforms;
public partial class PlatformNet : Platform
{
const string NamespacesAppSettingsKey = "servicestack.razor.namespaces";
public override void InitHostConfig(HostConfig config)
{
if (config.HandlerFactoryPath == null)
{
InferHttpHandlerPath(config);
}
}
public override HashSet<string> GetRazorNamespaces()
{
var razorNamespaces = new HashSet<string>();
//Infer from <system.web.webPages.razor> - what VS.NET's intelli-sense uses
var configPath = GetAppConfigPath();
if (configPath != null)
{
var xml = configPath.ReadAllText();
var doc = XElement.Parse(xml);
doc.AnyElement("system.web.webPages.razor")
.AnyElement("pages")
.AnyElement("namespaces")
.AllElements("add").ToList()
.ForEach(x => razorNamespaces.Add(x.AnyAttribute("namespace").Value));
}
//E.g. <add key="servicestack.razor.namespaces" value="System,ServiceStack.Text" />
if (ConfigUtils.GetNullableAppSetting(NamespacesAppSettingsKey) != null)
{
ConfigUtils.GetListFromAppSetting(NamespacesAppSettingsKey)
.ForEach(x => razorNamespaces.Add(x));
}
return razorNamespaces;
}
public override string GetAppConfigPath()
{
if (ServiceStackHost.Instance == null) return null;
var configPath = "~/web.config".MapHostAbsolutePath();
if (File.Exists(configPath))
return configPath;
configPath = "~/Web.config".MapHostAbsolutePath(); //*nix FS FTW!
if (File.Exists(configPath))
return configPath;
var appHostDll = new FileInfo(ServiceStackHost.Instance.GetType().Assembly.Location).Name;
configPath = $"~/{appHostDll}.config".MapAbsolutePath();
return File.Exists(configPath) ? configPath : null;
}
private static System.Configuration.Configuration GetAppConfig()
{
Assembly entryAssembly;
//Read the user-defined path in the Web.Config
if (HostContext.IsAspNetHost)
return WebConfigurationManager.OpenWebConfiguration("~/");
if ((entryAssembly = Assembly.GetEntryAssembly()) != null)
return ConfigurationManager.OpenExeConfiguration(entryAssembly.Location);
return null;
}
private static void InferHttpHandlerPath(HostConfig config)
{
try
{
var webConfig = GetAppConfig();
if (webConfig == null) return;
SetPathsFromConfiguration(config, webConfig, null);
if (config.MetadataRedirectPath == null)
{
foreach (ConfigurationLocation location in webConfig.Locations)
{
SetPathsFromConfiguration(config, location.OpenConfiguration(), (location.Path ?? "").ToLower());
if (config.MetadataRedirectPath != null) { break; }
}
}
if (HostContext.IsAspNetHost && config.MetadataRedirectPath == null)
{
throw new ConfigurationErrorsException(
"Unable to infer ServiceStack's <httpHandler.Path/> from the Web.Config\n"
+ "Check with https://docs.servicestack.net/create-your-first-webservice to ensure you have configured ServiceStack properly.\n"
+ "Otherwise you can explicitly set your httpHandler.Path by setting: EndpointHostConfig.ServiceStackPath");
}
}
catch (Exception) { }
}
private static void SetPathsFromConfiguration(HostConfig config, System.Configuration.Configuration webConfig, string locationPath)
{
if (webConfig == null)
return;
//standard config
if (webConfig.GetSection("system.web/httpHandlers") is HttpHandlersSection handlersSection)
{
for (var i = 0; i < handlersSection.Handlers.Count; i++)
{
var httpHandler = handlersSection.Handlers[i];
if (!httpHandler.Type.StartsWith("ServiceStack"))
continue;
SetPaths(config, httpHandler.Path, locationPath);
break;
}
}
//IIS7+ integrated mode system.webServer/handlers
var pathsNotSet = config.MetadataRedirectPath == null;
if (pathsNotSet)
{
var webServerSection = webConfig.GetSection("system.webServer");
var rawXml = webServerSection?.SectionInformation.GetRawXml();
if (!string.IsNullOrEmpty(rawXml))
{
SetPaths(config, ExtractHandlerPathFromWebServerConfigurationXml(rawXml), locationPath);
}
//In some MVC Hosts auto-inferencing doesn't work, in these cases assume the most likely default of "/api" path
pathsNotSet = config.MetadataRedirectPath == null;
if (pathsNotSet)
{
var isMvcHost = Type.GetType("System.Web.Mvc.Controller") != null;
if (isMvcHost)
{
SetPaths(config, "api", null);
}
}
}
}
private static string ExtractHandlerPathFromWebServerConfigurationXml(string rawXml)
{
try
{
return XDocument.Parse(rawXml).Root.Element("handlers")
?.Descendants("add")
?.Where(handler => EnsureHandlerTypeAttribute(handler).StartsWith("ServiceStack"))
.Select(handler => handler.Attribute("path").Value)
.FirstOrDefault();
}
catch (Exception ex)
{
HostContext.AppHost?.OnStartupException(ex);
return null;
}
}
private static string EnsureHandlerTypeAttribute(XElement handler)
{
if (handler.Attribute("type") != null && !String.IsNullOrEmpty(handler.Attribute("type").Value))
{
return handler.Attribute("type").Value;
}
return string.Empty;
}
private static void SetPaths(HostConfig config, string handlerPath, string locationPath)
{
if (handlerPath == null) return;
if (locationPath == null)
{
handlerPath = handlerPath.Replace("*", string.Empty);
}
config.HandlerFactoryPath = locationPath ??
(string.IsNullOrEmpty(handlerPath) ? null : handlerPath);
config.MetadataRedirectPath = "~/metadata";
}
}
#endif