asp.net 删除项目文件/文件夹IIS重启,Session丢失问
编程学习 2021-07-04 22:40www.dzhlxh.cn编程入门
最近在做一个项目,涉及到大量文件中转(先上传到本项目的某个文件夹中,在移动到FTP中),后面发现每次一删除文件之后在做操作都会提示未登录,刚开始以为是WebService Session丢失问题,后面发现缓存也更新了
仔细一看,SSO返回的ticket也不相同,才发现原来IIS重启了,最后解决方案如下:
新建一个类继承IHttpModule
/// <summary>
/// Stops the ASP.NET AppDomain being restarted (which clears
/// Session state, Cache etc.) whenever a folder is deleted.
/// </summary>
public class StopAppDomainRestartOnFolderDeleteModule : IHttpModule
{
private static bool DisableFCNs = false;
public void Init(HttpApplication context)
{
if (DisableFCNs) return;
PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
object o = p.GetValue(null, null);
FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });
DisableFCNs = true;
}
public void Dispose() { }
}
随后在Web.Config中加入Module配置
<!--解决删除项目文件/文件夹引起的IIS重启-->
<add name="stopAppDomainRestartOnFolderDelete" type="DeployAssistant.Facade.Web.StopAppDomainRestartOnFolderDeleteModule,DeployAssistant.Facade"/>
这样每次再删除文件/文件夹AppDomain都不会重启了,Session也不会丢失了。世界也变得更美好了!
PS:Web.Config和bin文件夹下的改动依然会让Web重启,这也是必须保留的!
新建一个类继承IHttpModule
代码如下:
/// <summary>
/// Stops the ASP.NET AppDomain being restarted (which clears
/// Session state, Cache etc.) whenever a folder is deleted.
/// </summary>
public class StopAppDomainRestartOnFolderDeleteModule : IHttpModule
{
private static bool DisableFCNs = false;
public void Init(HttpApplication context)
{
if (DisableFCNs) return;
PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
object o = p.GetValue(null, null);
FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });
DisableFCNs = true;
}
public void Dispose() { }
}
随后在Web.Config中加入Module配置
代码如下:
<!--解决删除项目文件/文件夹引起的IIS重启-->
<add name="stopAppDomainRestartOnFolderDelete" type="DeployAssistant.Facade.Web.StopAppDomainRestartOnFolderDeleteModule,DeployAssistant.Facade"/>
这样每次再删除文件/文件夹AppDomain都不会重启了,Session也不会丢失了。世界也变得更美好了!
PS:Web.Config和bin文件夹下的改动依然会让Web重启,这也是必须保留的!