Caching safecontrols definitions

Sep 24, 2008 at 4:07 AM
Hello everyone,
I use WSPBuilder everyday in my SharePoint development and very satisfy with it. Recently, my SharePoint project extent its functionalities to include Enterprise Library. The EntLibs' assemblies are not changed in my solution, however, WSPBuilder parses them every time it builds the solutions. Actually, it does not cost much time, but I don't really want to waste time and CPU for parsing unchanged information every now and then. Therefore, I download the source for WSPBuilder and add the caching of the parsing information. Here is the code:

  • Add to the end of WSP/SafeControlDefinition.cs

        private static SafeControlDefinition[] GetSafeControlDefinition(FileSystemInfo fileHandle)
        {
            string metaFileName = fileHandle.FullName + ".meta";
            if (File.Exists(metaFileName))
            {
                if (File.GetLastWriteTime(metaFileName) >= fileHandle.LastWriteTime)
                {
                    using (var fileStream = new FileStream(metaFileName, FileMode.Open))
                    {
                        var deserializer = new XmlSerializer(typeof (SafeControlDefinition[]));
                        try
                        {
                            var result = (SafeControlDefinition[]) deserializer.Deserialize(fileStream);
                            return result;
                        }
                        catch(InvalidOperationException)
                        {
                            return new SafeControlDefinition[]{};
                        }
                    }
                }
            }
            return null;
        }
       
        private static void SaveSafeControlDefinition(FileSystemInfo fileHandle, SafeControlDefinition[] safeControls)
        {
            string metaFileName = fileHandle.FullName + ".meta";

            if (File.GetLastWriteTime(metaFileName) >= fileHandle.LastWriteTime)
                return;

            using (var fileStream = new FileStream(metaFileName, FileMode.Create))
            {
                if (safeControls != null && safeControls.Length > 0)
                {
                    var deserializer = new XmlSerializer(typeof(SafeControlDefinition[]));
                    deserializer.Serialize(fileStream, safeControls);
                }
                fileStream.Close();
            }
            File.SetLastWriteTime(metaFileName, fileHandle.LastWriteTime);
        }

  • Modify the function CreateSafeControls in WSP/AssemblyReference.cs

        private void CreateSafeControls(AssemblyDefinition assembly, AssemblyFileReference assemblyFileReference)
        {
            try
            {
                // Add the safe controls for the assembly
                SafeControlDefinition[] safeControls = GetSafeControlDefinition(assemblyFileReference.FileHandle);
                if (safeControls == null)
                {
                    safeControls = BuildSafeControlDefinition(assembly);
                }
                if (safeControls != null && safeControls.Length == 0)
                {
                    safeControls = null;
                }
                assemblyFileReference.SafeControls = AppendArray(assemblyFileReference.SafeControls, safeControls);
                SaveSafeControlDefinition(assemblyFileReference.FileHandle, assemblyFileReference.SafeControls);
            }
            catch
            {
                // Throw an exception with a better description
                if (_resoleveException != null)
                {
                    throw _resoleveException;
                }
                else
                {
                    throw;
                }
            }
        }

That's all,

Hoa