1
1
using System ;
2
- using System . IO ;
3
- using System . Linq ;
4
- using System . Threading ;
5
2
using System . Web ;
6
- using System . Web . Hosting ;
7
- using System . Web . Mvc ;
8
- using System . Web . Routing ;
9
3
using BetterModules . Core . DataAccess . DataContext . Migrations ;
10
4
using BetterModules . Core . Exceptions ;
11
- using BetterModules . Core . Web . Exceptions . Host ;
12
5
using BetterModules . Core . Web . Modules . Registration ;
13
- using Common . Logging ;
14
- using RazorGenerator . Mvc ;
15
6
16
7
namespace BetterModules . Core . Web . Environment . Host
17
8
{
18
9
/// <summary>
19
10
/// Default web host implementation.
20
11
/// </summary>
12
+ [ Obsolete ( "DefaultWebApplicationHost is deprecated. Consider utilizing DefaultWebApplicationAutoHost" ) ]
21
13
public class DefaultWebApplicationHost : IWebApplicationHost
22
14
{
23
- /// <summary>
24
- /// Current class logger.
25
- /// </summary>
26
- private static readonly ILog Logger = LogManager . GetCurrentClassLogger ( ) ;
27
-
28
- private readonly IWebModulesRegistration modulesRegistration ;
29
-
30
- private readonly IMigrationRunner migrationRunner ;
31
-
32
15
/// <summary>
33
16
/// Initializes a new instance of the <see cref="DefaultWebApplicationHost" /> class.
34
17
/// </summary>
35
18
/// <param name="modulesRegistration">The modules registration.</param>
36
19
/// <param name="migrationRunner">The migration runner.</param>
37
20
public DefaultWebApplicationHost ( IWebModulesRegistration modulesRegistration , IMigrationRunner migrationRunner )
38
21
{
39
- this . modulesRegistration = modulesRegistration ;
40
- this . migrationRunner = migrationRunner ;
41
22
}
42
23
43
24
/// <summary>
@@ -48,27 +29,6 @@ public DefaultWebApplicationHost(IWebModulesRegistration modulesRegistration, IM
48
29
/// <exception cref="CoreException">ViewEngines.Engines collection doesn't contain any precompiled MVC view engines. Each module uses precompiled MVC engines for rendering views. Please check if Engines list is not cleared manualy in global.asax.cx</exception>
49
30
public virtual void OnApplicationStart ( HttpApplication application , bool validateViewEngines = true )
50
31
{
51
- try
52
- {
53
- Logger . Info ( "Web application host starting..." ) ;
54
-
55
- if ( validateViewEngines && ! ViewEngines . Engines . Any ( engine => engine is CompositePrecompiledMvcEngine ) )
56
- {
57
- throw new CoreException ( "ViewEngines.Engines collection doesn't contain precompiled composite MVC view engine. Application modules use precompiled MVC views for rendering. Please check if Engines list is not cleared manualy in global.asax.cx" ) ;
58
- }
59
-
60
- modulesRegistration . RegisterKnownModuleRoutes ( RouteTable . Routes ) ;
61
- MigrateDatabase ( ) ;
62
-
63
- // Notify.
64
- Events . WebCoreEvents . Instance . OnHostStart ( application ) ;
65
-
66
- Logger . Info ( "Web application host started." ) ;
67
- }
68
- catch ( Exception ex )
69
- {
70
- Logger . Fatal ( "Failed to start host application." , ex ) ;
71
- }
72
32
}
73
33
74
34
/// <summary>
@@ -77,10 +37,6 @@ public virtual void OnApplicationStart(HttpApplication application, bool validat
77
37
/// <param name="application">The host application.</param>
78
38
public virtual void OnApplicationEnd ( HttpApplication application )
79
39
{
80
- Logger . Info ( "Web host application stopped." ) ;
81
-
82
- // Notify.
83
- Events . WebCoreEvents . Instance . OnHostStop ( application ) ;
84
40
}
85
41
86
42
/// <summary>
@@ -89,11 +45,6 @@ public virtual void OnApplicationEnd(HttpApplication application)
89
45
/// <param name="application">The host application.</param>
90
46
public virtual void OnApplicationError ( HttpApplication application )
91
47
{
92
- var error = application . Server . GetLastError ( ) ;
93
- Logger . Fatal ( "Unhandled exception occurred in web host application." , error ) ;
94
-
95
- // Notify.
96
- Events . WebCoreEvents . Instance . OnHostError ( application ) ;
97
48
}
98
49
99
50
/// <summary>
@@ -110,14 +61,6 @@ public virtual void OnEndRequest(HttpApplication application)
110
61
/// <param name="application">The host application.</param>
111
62
public virtual void OnBeginRequest ( HttpApplication application )
112
63
{
113
- #if DEBUG
114
- // A quick way to restart an application host.
115
- // This is not going to affect production as it is compiled only in the debug mode.
116
- if ( application . Request [ "restart" ] == "1" )
117
- {
118
- RestartAndReloadHost ( application ) ;
119
- }
120
- #endif
121
64
}
122
65
123
66
/// <summary>
@@ -127,8 +70,6 @@ public virtual void OnBeginRequest(HttpApplication application)
127
70
/// <exception cref="System.NotImplementedException"></exception>
128
71
public virtual void OnAuthenticateRequest ( HttpApplication application )
129
72
{
130
- // Notify.
131
- Events . WebCoreEvents . Instance . OnHostAuthenticateRequest ( application ) ;
132
73
}
133
74
134
75
/// <summary>
@@ -137,103 +78,13 @@ public virtual void OnAuthenticateRequest(HttpApplication application)
137
78
/// <param name="application">The application.</param>
138
79
public virtual void RestartAndReloadHost ( HttpApplication application )
139
80
{
140
- RestartApplicationHost ( ) ;
141
-
142
- Thread . Sleep ( 500 ) ;
143
-
144
- UriBuilder uri = new UriBuilder ( application . Request . Url ) ;
145
- uri . Query = string . Empty ;
146
-
147
- application . Response . ClearContent ( ) ;
148
- application . Response . Write ( string . Format ( "<script type=\" text/javascript\" >window.location = '{0}';</script>" , uri ) ) ;
149
- application . Response . End ( ) ;
150
81
}
151
82
152
83
/// <summary>
153
84
/// Terminates current application. The application restarts on the next time a request is received for it.
154
85
/// </summary>
155
86
public virtual void RestartApplicationHost ( )
156
87
{
157
- try
158
- {
159
- HttpRuntime . UnloadAppDomain ( ) ;
160
- }
161
- catch
162
- {
163
- try
164
- {
165
- bool success = TryTouchBinRestartMarker ( ) || TryTouchWebConfig ( ) ;
166
-
167
- if ( ! success )
168
- {
169
- throw new RestartApplicationException ( "Failed to terminate host application." ) ;
170
- }
171
- }
172
- catch ( Exception ex )
173
- {
174
- throw new RestartApplicationException ( "Failed to terminate host application." , ex ) ;
175
- }
176
- }
177
- }
178
-
179
- /// <summary>
180
- /// Tries to update last write time for web configuration file to restart application.
181
- /// </summary>
182
- /// <returns><c>true</c> if web.config file updated successfully; otherwise, <c>false</c>. </returns>
183
- private bool TryTouchWebConfig ( )
184
- {
185
- try
186
- {
187
- File . SetLastWriteTimeUtc ( HostingEnvironment . MapPath ( "~/web.config" ) , DateTime . UtcNow ) ;
188
- return true ;
189
- }
190
- catch ( Exception ex )
191
- {
192
- Logger . Warn ( "Failed to touch web host application web.config file." , ex ) ;
193
- return false ;
194
- }
195
- }
196
-
197
- /// <summary>
198
- /// Tries the touch restart marker in ~/bin/restart folder.
199
- /// </summary>
200
- /// <returns><c>true</c> if file updated successfully; otherwise, <c>false</c>. </returns>
201
- private bool TryTouchBinRestartMarker ( )
202
- {
203
- try
204
- {
205
- var binMarker = HostingEnvironment . MapPath ( "~/bin/restart" ) ;
206
- Directory . CreateDirectory ( binMarker ) ;
207
-
208
- using ( var stream = File . CreateText ( Path . Combine ( binMarker , "marker.txt" ) ) )
209
- {
210
- stream . WriteLine ( "Restarted on '{0}'" , DateTime . UtcNow ) ;
211
- stream . Flush ( ) ;
212
- }
213
-
214
- return true ;
215
- }
216
- catch ( Exception ex )
217
- {
218
- Logger . Warn ( "Failed to touch web host application \b in folder." , ex ) ;
219
- return false ;
220
- }
221
- }
222
-
223
- /// <summary>
224
- /// Updates database.
225
- /// </summary>
226
- private void MigrateDatabase ( )
227
- {
228
- try
229
- {
230
- var descriptors = modulesRegistration . GetModules ( ) . Select ( m => m . ModuleDescriptor ) . ToList ( ) ;
231
- migrationRunner . MigrateStructure ( descriptors ) ;
232
- }
233
- catch ( Exception ex )
234
- {
235
- Logger . Error ( ex ) ;
236
- }
237
88
}
238
89
}
239
90
}
0 commit comments