วิธีแก้ไข ASP.NET C# เมื่อ Rename หรือ Delete Folder แล้ว Session Timeout

นั่งเสียเวลาเป็นชม.ๆ –” มันอะไรกันนักหนาเนี่ย 55+ ไปได้วิธีมาจากเว็บ Narisa และ Greatfriends ได้ความว่า ใน ASP.NET 2.0 ได้เพิ่มสิ่งที่เรียกว่า File Change Notifications (FCN) หรือ Directory Monitoring เพื่อคอยจับ Directory ทุกครั้งที่มีการ Rename หรือ Delete มันจะ Restart Application ดังนั้น Session State ที่เก็บแบบ InProc จะหายไปด้วย (เออเอากับมัน…)

เกริ่นซะยาวมาดูวิธีแก้กันเลยให้ใส่ Code ด้านล่างนี้ ในไฟล์ “Global.asax.cs” ที่ Events Application_Start() และใช้ using System.Reflection ด้วย ซึ่งการทำงานของมันจะปิด Directory Monitoring ทันทีที่ Application เริ่มทำงาน

System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
object o = p.GetValue(null, null);
System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });

สำหรับคนที่ยังไม่เคยใช้ Global.asax.cs ขออธิบายนิดนึงครับ

ไฟล์ Global.asax.cs จะมีไฟล์ Global.asax Inherits มาเรียกอีกทีนึง และทั้งสองไฟล์จะต้องอยู่บนสุดของเว็บไซต์เท่านั้นนะจ๊ะ ยิ่งอธิบายยิ่ง”งง” ดาวน์โหลดไปเลยดีกว่าตรงนี้ผมใส่ Code ด้านบนไว้ให้เรียบร้อยแล้วครับ
คลิกที่นี่เพื่อดาวน์โหลด Global.asax และ Global.asax.cs

เมื่อเราใส่ Code เรียบร้อยแล้วหากเกิดกรณี Error ประมาณนี้


Security Exception
Description: The application attempted to perform an  operation not allowed by the security policy.  To grant this  application the required permission please contact your system  administrator or change the application's trust level in the  configuration file.
 Exception Details: System.Security.SecurityException:  Request failed.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

ให้ไปที่ไฟล์ web.config แล้วไปเพิ่มตาม Code ด้านล่าง

<system.web>
<trust level="Full" originUrl=""/>
</system.web>

เป็นอันเสร็จเรียบร้อยครับ

ที่มา :
http://www.narisa.com/forums/index.php?showtopic=27585
http://www.greatfriends.biz/webboards/msg.asp?id=106519

You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Powered by WordPress | Shop Free T-Mobile Phones Online | Thanks to Verizon Phones without Contract, MMORPG Games and Sell Car