November 2008 - Posts

Disable Session Expiration when using Directory.Delete()

 As you may already know, one of the reasons why the asp.net worker process recycles which kills all the session variables in memory is due to dynamically deleting a folder under the root application folder. You have two options to follow in this case

1- To use out-of-process mode session
2- To use the below code which uses reflection to disable monitoring for the folders inside the application. In this way, once you delete a folder, the Application domain won't restart thus you won't be losing any session variable

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

Do not forget to import System.Reflection Namespace

Hope this helps,

Validate XML Against XSD [XML SCHEMA] using C#

 In this blog post, we will see how to create an XML schema file for an xml file and how to validate if a certain XML complies to our schema.

First of all, open the XML file in Visual Studio. Right click the solution explorer select add Existing file and select the xml file. Open the file and choose from the menu XML --> Create Schema.
Now you have the schema and the XML file, it is time to write some code to validate the xml file.


            try
            {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.Schemas.Add(null, XSDFILEPATH);
                    settings.ValidationType = ValidationType.Schema;
                    XmlDocument document = new XmlDocument();
                    document.Load(XMLFILEPATH);
                    XmlReader rdr = XmlReader.Create(new StringReader(document.InnerXml), settings);
                    while(rdr.Read()){}
            }
            catch
            {
                isValid = false;
            }

if the validation fails, a private field is set to false (isValid) in which you can check at runtime to see if the status of the validation process.

Hope this helps,

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Packet Sniffer