Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Restarts the Web Application
- /// Requires either Full Trust (HttpRuntime.UnloadAppDomain)
- /// or Write access to web.config.
- /// </summary>
- public static bool RestartWebApplication()
- {
- var logger = LogManager.GetLogger("HttpProsperoApplication.RestartWebApplication");
- bool rtn = false;
- try
- {
- logger.Info("Restarting AppPool with UnloadAppDomain");
- // *** This requires full trust so this will fail in many scenarios
- HttpRuntime.UnloadAppDomain();
- rtn = true;
- }
- catch (Exception ex)
- {
- logger.ErrorException("Cannot recycle AppPool with HttpRuntime.UnloadAppDomain", ex);
- try
- {
- logger.Info("Restarting AppPool modifying web.config");
- // *** Couldn't unload with Runtime - let's try modifying web.config
- string configPath = HttpContext.Current.Server.MapPath(@"~\web.config");
- File.SetLastWriteTimeUtc(configPath, DateTime.UtcNow);
- rtn = true;
- }
- catch (Exception ex2)
- {
- logger.ErrorException("Cannot recycle AppPool modifying web.config", ex2);
- }
- }
- return rtn;
- }
Advertisement
Add Comment
Please, Sign In to add comment