Skip to content

Commit edb0a4a

Browse files
author
Mantas Janulionis
committed
Deprecated DefaultWebApplicationAutoHost
1 parent 5cda696 commit edb0a4a

File tree

1 file changed

+1
-150
lines changed

1 file changed

+1
-150
lines changed
Lines changed: 1 addition & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
using System;
2-
using System.IO;
3-
using System.Linq;
4-
using System.Threading;
52
using System.Web;
6-
using System.Web.Hosting;
7-
using System.Web.Mvc;
8-
using System.Web.Routing;
93
using BetterModules.Core.DataAccess.DataContext.Migrations;
104
using BetterModules.Core.Exceptions;
11-
using BetterModules.Core.Web.Exceptions.Host;
125
using BetterModules.Core.Web.Modules.Registration;
13-
using Common.Logging;
14-
using RazorGenerator.Mvc;
156

167
namespace BetterModules.Core.Web.Environment.Host
178
{
189
/// <summary>
1910
/// Default web host implementation.
2011
/// </summary>
12+
[Obsolete("DefaultWebApplicationHost is deprecated. Consider utilizing DefaultWebApplicationAutoHost")]
2113
public class DefaultWebApplicationHost : IWebApplicationHost
2214
{
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-
3215
/// <summary>
3316
/// Initializes a new instance of the <see cref="DefaultWebApplicationHost" /> class.
3417
/// </summary>
3518
/// <param name="modulesRegistration">The modules registration.</param>
3619
/// <param name="migrationRunner">The migration runner.</param>
3720
public DefaultWebApplicationHost(IWebModulesRegistration modulesRegistration, IMigrationRunner migrationRunner)
3821
{
39-
this.modulesRegistration = modulesRegistration;
40-
this.migrationRunner = migrationRunner;
4122
}
4223

4324
/// <summary>
@@ -48,27 +29,6 @@ public DefaultWebApplicationHost(IWebModulesRegistration modulesRegistration, IM
4829
/// <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>
4930
public virtual void OnApplicationStart(HttpApplication application, bool validateViewEngines = true)
5031
{
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-
}
7232
}
7333

7434
/// <summary>
@@ -77,10 +37,6 @@ public virtual void OnApplicationStart(HttpApplication application, bool validat
7737
/// <param name="application">The host application.</param>
7838
public virtual void OnApplicationEnd(HttpApplication application)
7939
{
80-
Logger.Info("Web host application stopped.");
81-
82-
// Notify.
83-
Events.WebCoreEvents.Instance.OnHostStop(application);
8440
}
8541

8642
/// <summary>
@@ -89,11 +45,6 @@ public virtual void OnApplicationEnd(HttpApplication application)
8945
/// <param name="application">The host application.</param>
9046
public virtual void OnApplicationError(HttpApplication application)
9147
{
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);
9748
}
9849

9950
/// <summary>
@@ -110,14 +61,6 @@ public virtual void OnEndRequest(HttpApplication application)
11061
/// <param name="application">The host application.</param>
11162
public virtual void OnBeginRequest(HttpApplication application)
11263
{
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
12164
}
12265

12366
/// <summary>
@@ -127,8 +70,6 @@ public virtual void OnBeginRequest(HttpApplication application)
12770
/// <exception cref="System.NotImplementedException"></exception>
12871
public virtual void OnAuthenticateRequest(HttpApplication application)
12972
{
130-
// Notify.
131-
Events.WebCoreEvents.Instance.OnHostAuthenticateRequest(application);
13273
}
13374

13475
/// <summary>
@@ -137,103 +78,13 @@ public virtual void OnAuthenticateRequest(HttpApplication application)
13778
/// <param name="application">The application.</param>
13879
public virtual void RestartAndReloadHost(HttpApplication application)
13980
{
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();
15081
}
15182

15283
/// <summary>
15384
/// Terminates current application. The application restarts on the next time a request is received for it.
15485
/// </summary>
15586
public virtual void RestartApplicationHost()
15687
{
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 \bin 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-
}
23788
}
23889
}
23990
}

0 commit comments

Comments
 (0)