Global.asax, Caching, LoadXml problems.

G

Guest

I've got a problem with an app I've been working on, the Caching object and
events not firing correctly. In a nutshell: When I'm debugging, and I set a
breakpoint in the removed item call back, the code works correctly. If there
is no debugger it doesn't fire, which causes the app to have problems.

Here's what the code does: The TemplateData class inherits from the DataSet
Object, in the constructor it loads itself with Data from an XML file. I want
this to only happen at Application Start, OR if the file is changed, I want
it to reload the data. I add the TemplateData object to the cache and wire up
a dependency on the xml file that it's loading. If I don't have a break point
either in the Global.asax RefreshDataSetMethod, OR in the constructor for the
TemplateData class, the data in the Cache does not get populated correctly.
That is to say when I go to get the TemplateData out of the cache, I get a
TemplateData object out, however none of the tables are populated.

As I was writing this I did some more testing and it appears that there are
certain times when the TemplateData is unable to load the file because some
other process has a hold on it. It appears that if you change the xml to a
new value, then change it back to the original values (with notepad) then
this crops up.

This is on Server 2003. Framework 1.1 VS2003.

Any ideas are GREATLY appreciated.

Here's the code for the Global.asax
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.Caching;
using System.Data;

namespace Foo
{
/// <summary>
/// Summary description for Global.
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

private static Cache _appCache = null;
private static string _path = null;

// Event Handler for removing the data from the cache.

static void RefreshDataSet (String key, Object item,
CacheItemRemovedReason reason)
{
// Template Data is a class that inherits from the dataset object
TemplateData td = new
TemplateData(_path);
LoadCache(td);

}

static void LoadCache(TemplateData td)
{
// Add the template to the cache, with no time expiration, just a file
dependency.
_appCache.Insert("td", td, new CacheDependency(_path),
Cache.NoAbsoluteExpiration , Cache.NoSlidingExpiration ,
CacheItemPriority.Normal, new CacheItemRemovedCallback(RefreshDataSet));

}


public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
// Figure out where we are in the world
_appCache =
System.Web.HttpContext.Current.Cache;
_path = Server.MapPath("ExpressTemplates.xml");

// Load the TemplateData
TemplateData td = new TemplateData();

LoadCache(td);

}

protected void Session_Start(Object sender, EventArgs e)
{

}

protected void Application_BeginRequest(Object sender, EventArgs e)
{

}

protected void Application_EndRequest(Object sender, EventArgs e)
{

}

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{

}

protected void Application_Error(Object sender, EventArgs e)
{

}

protected void Session_End(Object sender, EventArgs e)
{

}

protected void Application_End(Object sender, EventArgs e)
{

}



#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}

Here's the code for the TemplateData class

using System;
using System.Data;
using System.Xml;
using System.Collections;
using System.Web;

namespace FOO
{
/// <summary>
/// Summary description for TemplateData.
/// </summary>
public class TemplateData :DataSet
{
public TemplateData()
{
this.ReadXml(HttpContext.Current.Server.MapPath("ExpressTemplates.xml"));
}

public TemplateData(string path)
{
this.ReadXml(path);
}

}
}


Here's the code in a page load where it tries to retrieve the data from the
cache.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Caching;

namespace FOO
{
/// <summary>
/// Summary description for ListExpressTemplates.
/// </summary>
public class ListExpressTemplates : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
protected TemplateTable tt;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected StyleList sl;

private void Page_Load(object sender, System.EventArgs e)
{
//The system will use the offset and numberToDisplay Querystring values
to determine what to display.

// Get the Template data from the Cache
TemplateData td =
(TemplateData)Cache["td"];


sl = new StyleList(td);

int offset = Convert.ToInt32(Request.QueryString["offset"]);
// numberToDisplay is determined by rows * cols
//int numberToDisplay =
Convert.ToInt32(Request.QueryString["numberToDisplay"]);
int rows = Convert.ToInt32(Request.QueryString["rows"]);
int cols = Convert.ToInt32(Request.QueryString["cols"]);

if (rows ==0)
{
rows = 3;
}
if (cols == 0)
{
cols = 3;
}
int filterValue = Convert.ToInt32(Request.QueryString["filter"]);


if (!Page.IsClientScriptBlockRegistered("Resubmit"))
{
Page.RegisterClientScriptBlock("Resubmit", reSubmitCode(rows, cols,
filterValue));
}

Filter filterToApply = (Filter)filterValue;


TemplateList templateList = new TemplateList(filterToApply);
tt = new TemplateTable(rows, cols, offset, templateList, true,
filterValue);
tt.CellPadding = 0;
tt.CellSpacing = 20;
tt.filterDropDown.SelectedIndex = filterValue;
tt.GridLines = GridLines.None;
PlaceHolder1.Controls.Add(tt);

DropDownList1.DataSource = sl.DataLoad();
DropDownList1.DataValueField = "Name";
DropDownList1.DataTextField = "FriendlyName";

DataBind();


}

private string reSubmitCode(int rows, int cols, int filterValue)
{
StringBuilder sbScript = new StringBuilder();
sbScript.Append("<script language=\"javascript\">");
sbScript.Append("function reSubmit(caller){");
sbScript.Append("var value = caller.value;");
sbScript.Append("window.open(\"ListExpressTemplates.aspx?rows=" + rows +
"&cols=" + cols + "&filter=\"+ value" + ",\"_self\")");
sbScript.Append("} ");
sbScript.Append("</script>");
return sbScript.ToString();
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top