ASP.NET Debug Build Assembly Checker Page

By Peter Bromberg

Very often ASP.NET applications have several developers working on the project, and debug builds of assemblies mistakenly get deployed into production. This slows down your application and consumes extra memory. Here is a script-only page you can drop into your website that will show any assemblies that were built in debug mode.

Copy the code below into a blank text document and save it as "DebugBuild.aspx":

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Debug Build Checker</title>
    <script runat=server language="C#">
        protected void Page_Load( object sender, EventArgs e)
        {
             DoDebugCheck();
         }

           public class Info
    {
        public string AssemblyName { get; set; }
        public bool IsDebug { get; set; }
    }

         private bool IsAssemblyDebugBuild(string filepath)
        {
             return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
        }
         private bool IsAssemblyDebugBuild(Assembly assembly)
         {
             foreach (var attribute in assembly.GetCustomAttributes(false))
            {
                 var debuggableAttribute = attribute as DebuggableAttribute;
                 if (debuggableAttribute != null)
                 {
              
                    return
                        debuggableAttribute.DebuggingFlags.HasFlag(
                            DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints) &&
                          debuggableAttribute.DebuggingFlags.HasFlag(
                              DebuggableAttribute.DebuggingModes.DisableOptimizations);
        
                 }
             }
             return false;
        }

         private void DoDebugCheck()
        {
             var infos = new List<Info>();
             string path = Server.MapPath("~/") + @"\bin\";             
                 var di = new DirectoryInfo(path);
               FileInfo[] files = di.GetFiles();
                 foreach(FileInfo fi in files)
                 {
                     if(  (fi.Name.ToLower().Contains(".dll") ||
                         fi.Name.ToLower().Contains(".exe")) &&
                          (!fi.Name.ToLower( ).Contains(".config") && !fi.Name.ToLower( ).Contains(".manifest")) )
                     {
                          string name = fi.Name;
                         bool isDebug = IsAssemblyDebugBuild(Path.Combine(path, fi.Name));
                         if (isDebug)
                         {
                              var ai = new Info() {IsDebug = isDebug, AssemblyName = name};
                              infos.Add(ai);
                         }
                     }
                 }
                  this.GridView1.DataSource = infos;
             GridView1.DataBind();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>DEBUG BUILD CHECKER</h1>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=true>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

ASP.NET Debug Build Assembly Checker Page  (921 Views)