I have put this code in the Global.asax file where I use a timer of 10 seconds to execute the function t_Elapsed.
However when I run the code, it seems that the code "Gets stuck" somewhere in the lines before the f.Demand() or on that line because if I take all these lines away, the code that comes under will execute. The "Folder1" exists, so that should not be any problem. I really wonder why this doesn´t work and what could cause it ?
<%@ Application Language="C#" %>
<script runat="server">
protected void Application_Start(object sender, EventArgs e){
//Code that runs on Application StartUp
System.Timers.Timer t = new System.Timers.Timer(10000);t.Enabled = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));
f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));
f.Demand();
//Code that comes below this Line will Not execute because it seems that something is getting stuck in the code above
}
script>
protected void Application_Start(object sender, EventArgs e) { //Code that runs on Application StartUp System.Timers.Timer t = new System.Timers.Timer(10000); t.Enabled = true; t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); } void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("RouletteDataBase")); f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("RouletteDataBase")); f.Demand(); //Code that runs on Application StartUp }
After the function executes, where you create Timer t, t goes out of scope and ceases to exist. Try putting the timer in the Application object collection:
Application["MyTimer"] = t;
Steve Wellens
My blog
I am not sure if I follow, do you meen that t goes out of scope after this line ---> t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
So I should write like below ? Though I am not really sure about the logic for that additional line. It seems that "MyTimer" is a keyvalue but I am not sure if I will refer to that somewhere...
To mention is if I take all the lines that has with System.Security.Permissions to do, then other code will work well with the timer. So in a way it seems that the timer do work anyway.
protected void Application_Start(object sender, EventArgs e){
//Code that runs on Application StartUp
System.Timers.Timer t = new System.Timers.Timer(10000); t.Enabled = true;t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
Application["MyTimer"] = t;
}
I did 2 tests to put the code like below and then using my timer in the first post. It seems that the timer I use works but something is breaking the code during the Security.Permissions lines. I beleive I do the Permissions correct.
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e){
System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));
f.Demand();
//The code below this line will Not execute at all here. It seems that something is "break" the code above
}
Then I tried to put the code that will execute in the beginning, before the Security.Permission lines and that code will now execute with 10 seconds intervals.
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e){
//This code will now execute with 10 seconds interval but not the below code
System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));f.AddPathList(System.Security.Permissions.FileIOPermissionAccess.AllAccess | System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));
f.Demand();
}
I think I have found the problem why the code does not execute and that is for this line:
System.Security.Permissions.FileIOPermission f = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess, Server.MapPath("Folder1"));
I have googled around and found this citat:
"The Global.asax file does not have the Page object as its default object. This means that you cannot simply call a method such as MapPath and expect it to work. Fortunately, in most cases, you can use a simple workaround. Instead of calling the MapPath method, you can call the Context.MapPath method"
It seems that Server.MapPath() cant be used. I am using the same code in a aspx.cs where I call this folder and that is not a problem. I have tried to change it to Context.Server.MapPath() and that does not work. I cant write Context.MapPath() as MapPath here isn´t a property.
It seems that you have to call a directory in another way when working with the Globax.asax but I dont know what to use ?