Code stops to execute somewhere in these 6 Lines (Global.asax)



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 }
-1
Coding
5/4/2009 10:50:07 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
2784 Views

Similar Articles

[PageSpeed] 3

Get it on Google Play
Get it on Apple App Store


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
-2
SGWellens
5/4/2009 11:12:02 PM

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;

 
 
}
-2
Coding
5/4/2009 11:23:17 PM

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();


}

2
Coding
5/4/2009 11:35:50 PM

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 ?
-2
Coding
5/5/2009 1:42:50 AM
Reply: