Click here to Skip to main content
15,867,835 members
Articles / Web Development / ASP.NET

Hack to enforce the cache of an XmlDataSource to invalidate

Rate me:
Please Sign up or sign in to vote.
4.17/5 (4 votes)
15 Jun 2008CPOL2 min read 37.3K   116   15   6
I found that when you change the Data property of an XmlDataSource control via code, it will not automatically invalidate the cache. This is a hack for this problem.

Introduction

We often use the XmlDataSource control together with the TreeView control.

If the data source is static, it works quite well. But, when the data source needs to be dynamic, there seems to be some problems. This article will introduce one such problem and give you a hack solution.

Symptoms

If the data source needs to be dynamic, you may choose to:

  1. Put several XmlDataSource controls on the web form, and switch it by setting different DataSourceIDs.
  2. Put only one XmlDataSource control and change the value of the DataFile or Data property of it, when necessary.

Obviously, the first way is more limited and only applicable for some special situations. It's not a good idea to put a lot of data source controls on one form and, more importantly, the count of the data source controls must be a fixed number.

But, what will happen when you change the value of the DataFile or Data property of the XmlDataSource control? From Microsoft's documentations, you may find the following information:

If you change the value of the DataFile/Data property, the DataSourceChanged event is raised. If caching is enabled and you change the value of DataFile/Data, the cache is invalidated.

Unfortunately, in my test (you may download the samples to see it yourself), I found that, the cache will not be automatically invalidated when you change the value of the Data property of the XmlDataSource control, although DataFile makes no trouble.

Workaround

First, you may choose to change the value of the DataFile property of the XmlDataSource, instead of Data. This is an easy solution.

Otherwise, if you stick to changing the property of Data for some reason (for example, your data source is not a real XML file, but just a dynamically-populated string), you may try the following hack method:

C#
public static void XmlDataSourceCacheHack(XmlDataSource dataSource)
{
    try
    {
        Type t = typeof(XmlDataSource);
        MethodInfo m = t.GetMethod("CreateCacheKey", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
        string key = (string)m.Invoke(dataSource, null);
        PropertyInfo p = t.GetProperty("Cache", 
                         BindingFlags.Instance | BindingFlags.NonPublic);
        object cache = p.GetValue(dataSource, null);

        Type t2 = t.Assembly.GetType("System.Web.UI.DataSourceCache");
        MethodInfo m2 = t2.GetMethod("Invalidate", 
                        BindingFlags.Instance | BindingFlags.Public);
        m2.Invoke(cache, new object[] { key });
    }
    catch
    {
    }
}

(It's sure that "using System.Reflection;" is required at the beginning of the file.)

This is just a hack for this problem. I spent more than one day on debugging to find the source of this strange problem.

(P.S.: If you find a more perfect way to solve it, avoiding Reflection, do tell me. Thanks!)

Thanks for PlamenLeykov

You may surely entirely disable the cache feature of the XmlDataSource. It also works fine. This is another easy solution.

But, if your situation is just like mine: the change possibilities are lower than other postback possibilities (for example, the TreeView is quite big and always triggers postbacks more frequently than data sources change), you will prefer using Reflection than disabling the cache.

Is it a bug from Microsoft?

I'm not sure whether this is a bug from Microsoft, or if it is just applied to the design specification. But, either the implementation is wrong, or the documentation is wrong.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
I am a Chinese software developer, while I am now working in Japan.
If you can read in Chinese, you may visit my blog in Chinese:
http://blog.joycode.com/percyboy/
Thanks!

Comments and Discussions

 
GeneralA better solution Pin
MatesL15-Jun-09 2:09
MatesL15-Jun-09 2:09 
GeneralRe: A better solution Pin
Thomas Levesque10-Nov-09 2:57
professionalThomas Levesque10-Nov-09 2:57 
GeneralHoli sh*t Pin
Xmen Real 14-Feb-09 2:54
professional Xmen Real 14-Feb-09 2:54 
GeneralPerfect! Exactly what I am looking for. Thanks a ton! Pin
Member 62539425-Jul-08 4:41
Member 62539425-Jul-08 4:41 
Smile | :)
GeneralBad technique Pin
PlamenLeykov16-Jun-08 4:25
PlamenLeykov16-Jun-08 4:25 
JokeRe: Bad technique Pin
percyboy16-Jun-08 7:17
percyboy16-Jun-08 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.