Created
April 25, 2016 04:57
-
-
Save mattjcowan/2efdaa74ee4741ead5bf5210b16f4326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adapted from Umbraco and NopCommerce source | |
public static void RestartAppDomain() | |
{ | |
if (GetTrustLevel() > AspNetHostingPermissionLevel.Medium) | |
{ | |
//full trust | |
HttpRuntime.UnloadAppDomain(); | |
TryWriteWebConfig(); | |
TryWriteGlobalAsax(); | |
} | |
else | |
{ | |
//medium trust | |
bool success = TryWriteWebConfig(); | |
if (!success) | |
{ | |
throw new ApplicationException( | |
"The application needs needs to be restarted due to a configuration change, but was unable to do so." + | |
Environment.NewLine + | |
"To prevent this issue in the future, a change to the web server configuration is required:" + | |
Environment.NewLine + | |
"- run the application in a full trust environment, or" + Environment.NewLine + | |
"- give the application write access to the 'web.config' file."); | |
} | |
success = TryWriteGlobalAsax(); | |
if (!success) | |
{ | |
throw new ApplicationException( | |
"The application needs to be restarted due to a configuration change, but was unable to do so." + | |
Environment.NewLine + | |
"To prevent this issue in the future, a change to the web server configuration is required:" + | |
Environment.NewLine + | |
"- run the application in a full trust environment, or" + Environment.NewLine + | |
"- give the application write access to the 'Global.asax' file."); | |
} | |
} | |
} | |
#region Private Methods | |
private static AspNetHostingPermissionLevel? _trustLevel = null; | |
/// <summary> | |
/// Finds the trust level of the running application (http://blogs.msdn.com/dmitryr/archive/2007/01/23/finding-out-the-current-trust-level-in-asp-net.aspx) | |
/// </summary> | |
/// <returns>The current trust level.</returns> | |
private static AspNetHostingPermissionLevel GetTrustLevel() | |
{ | |
if (!_trustLevel.HasValue) | |
{ | |
//set minimum | |
_trustLevel = AspNetHostingPermissionLevel.None; | |
//determine maximum | |
foreach (var trustLevel in | |
new[] | |
{ | |
AspNetHostingPermissionLevel.Unrestricted, | |
AspNetHostingPermissionLevel.High, | |
AspNetHostingPermissionLevel.Medium, | |
AspNetHostingPermissionLevel.Low, | |
AspNetHostingPermissionLevel.Minimal | |
}) | |
{ | |
try | |
{ | |
new AspNetHostingPermission(trustLevel).Demand(); | |
_trustLevel = trustLevel; | |
break; //we've set the highest permission we can | |
} | |
catch (System.Security.SecurityException) | |
{ | |
continue; | |
} | |
} | |
} | |
return _trustLevel.Value; | |
} | |
private static bool TryWriteWebConfig() | |
{ | |
try | |
{ | |
//Do an explicit write | |
var webConfigFile = MapPath("~/web.config"); | |
var contents = File.ReadAllText(webConfigFile); | |
contents = contents.Substring(0, contents.IndexOf("</configuration>", StringComparison.OrdinalIgnoreCase) + 16) + "\n<!-- restart at " + DateTime.Now.ToString("u") + " -->"; | |
File.WriteAllText(webConfigFile, contents); | |
//File.SetLastWriteTimeUtc(webConfigFile, DateTime.UtcNow); | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
private static bool TryWriteGlobalAsax() | |
{ | |
try | |
{ | |
File.SetLastWriteTimeUtc(MapPath("~/global.asax"), DateTime.UtcNow); | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment