.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Making sure debug is false in your web.config

This has happened to me more than once. You are working on a web site and are using xcopy for your deployment, whether it be xcopy from an automated process or a manual process and guess what you forgot to set the debug setting in the web.config to false. How many sites are out there with this setting? Is it that big of a deal? Recently, I discovered that not only is your site compiled for debug but also if you use Microsoft Client Side Ajax library (the Javascript included whenever you add ScriptManager tag) it will never get cached. Yes if you are in debug mode each and every visitor to your site will have to upload the entire Microsoft Ajax Client Library as it will not be cached on the client end.

This post will detail an approach to ensuring that the debug setting in the web.config is always deployed with a setting of false.

Listing 1 contains the source code for a console application. The code is extremely simple.

Listing 1 

   10     public class Program

   11     {

   12         public static void Main(string[] args)

   13         {

   14             //Only do this for web.config

   15             if (args.Length == 1)

   16             {

   17                 CultureInfo ci = Thread.CurrentThread.CurrentCulture;

   18                 string path = args[0] as string;

   19                 if (File.Exists(path) && Path.GetFileName(path).ToLower(ci).Equals("web.config"))

   20                 {

   21                     SetDebugTrue(path);

   22                 }

   23             }

   24         }

   25 

   26         //Find the Compilation XML node and set it's debug attribute to false.

   27         //This should be part of the build process to ensure deployed code is in

   28         //release mode.

   29         private static void SetDebugTrue(string path)

   30         {

   31             XmlDocument doc = new XmlDocument();

   32             doc.Load(path);

   33             XmlElement compile = doc.DocumentElement.SelectSingleNode("system.web/compilation") as XmlElement;

   34             if (compile != null)

   35             {

   36                 compile.SetAttribute("debug", "false");

   37                 doc.Save(path);

   38             }

   39         }

   40 

   41     }

The Main accepts a parameter which contains the path of the web.config file. The debug flag is only set if the file is named web.config. The private method SetDebugTrug finds the system.web/compilation XML element and sets the debug attribute to false and that’s it. But let’s go one step further and write a unit test. In order to test this code we will need to set the Program class to public and the Main method to public. By default the Console Application template project sets both of them to internal which makes them difficult to test.

Listing 2 contains the unit test code. I am using nUnit but feel free to use whatever unit testing framework you are comfortable with.

Listing 2

   12     [TestFixture]

   13     public class SetDebugFalseTest

   14     {

   15         [Test]

   16         public void Test_Debug_Attribute_In_WebConfig_Set_To_False()

   17         {

   18             string[] args = { "web.config" };

   19             Assert.That(GetDebugFlag(), Is.EqualTo("true"),"Bad starting web.config. Try rebulding.");

   20             Program.Main(args);

   21             Assert.That(GetDebugFlag(), Is.EqualTo("false"));

   22         }

   23 

   24         private string GetDebugFlag()

   25         {

   26             XmlDocument doc = new XmlDocument();

   27             doc.Load("web.config");

   28             XmlElement compile = doc.DocumentElement.SelectSingleNode("system.web/compilation") as XmlElement;

   29             return compile.GetAttribute("debug");

   30         }

   31     }

For this test to work you will need to add a web.config file, with debug set to true, to the test project and make sure that the Build Action Copy to Output Directory is set to Copy always. The unit test creates an array of string that is passed to the Main method of the Program class (from listing 1). We  then assert that the debug flag is set to false.

The last step in the process is to make sure that this code is executed whenever the site is deployed. Lately, I have been using CruiseControl.NET for my continuous integration progress. Integrating the above in ccnet is quite simple. Listing 3 shows a fragment of the CruiseControl config file that is responsible for running the above code.

Listing 3

  184 <exec>

  185   <executable>sdf.bat</executable>

  186   <baseDirectory>c:\ccnet\projects\gtanetws\src\ccnet</baseDirectory>

  187 </exec>

Here I am using the ccnet exec task to run a batch file. The batch file contains a single line of code that runs the console application produced from listing 1.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | Tools
Posted by CynotWhyNot on Thursday, April 09, 2009 10:21 AM
Permalink | Comments (71) | Post RSSRSS comment feed

Trouble Finding a Program: Launchy to the Rescue!

I just finished reading PC Worlds 101 Fantastic Freebies and listed are some great free software. In the list is a fantastic tool called Launchy.  Launchy indexes the programs in your start menu and can launch your documents, project files, folders, and bookmarks with just a few keystrokes. It can also be configured to launch programs and documents from directories. To start all you need to do is type the keys Alt+Space. This will launch the application.

launchy1

Just type the program you want to run and Launchy will display matches as you type. In this example I typed exp and Launchy displayed programs that match *exp*. Just scroll to the desired program and press enter. I even typed 2008 and it listed Visual Studio 2008 as one of the programs. How cool is that!

launchy2 

By default Launchy will index programs and documents from your start menu but you can easily include directories. Clicking the star at the top right corner causes the Launchy Options to appear.

launchy3

In the above the C:\Program Files directory and the EXE file extension is included in the indexing.

What a great tool to include in your toolbox!

Guess the movie

Just how dangerous is he? ... Compared to what? The bubonic plague?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Tools
Posted by CynotWhyNot on Monday, April 07, 2008 1:24 PM
Permalink | Comments (72) | Post RSSRSS comment feed

IIS 6.0 Resource Toolkit

Microsoft provides a free download called  The IIS 6.0 Resource Kit Tools. In all there are 13 tools;

  • Apache to IIS 6.0 Migration Tool version 1.0
  • CustomAuth version 1.0
  • IISCertDeploy.vbs version 1.0
  • IIS Host Helper Service version 1.0
  • IISState version 3.0
  • Log Parser version 2.1
  • Metabase Explorer version 1.6
  • Permissions Verifier version 1.0
  • RemapUrl version 1.0
  • SelfSSL version 1.0
  • TinyGet version 5.2
  • Web Capacity Analysis Tool version 5.2
  • WFetch version 1.3

Lately I've been using TinyGet to hit my asp.net sites. This ensures that the pages are parsed and compiled into memory and remain there. Users do not experience the long browser waits while the asp.net runtime compiles the pages for the first time. In order for this to work you need to run TinyGet more frequently than the Idle timeout set in the Application Pool. By default the worker process is shutdown every 20 minutes. Running TinyGet, hitting the site, every 15 minutes, will guarantee that the asp.net application remains in memory. Package the call to TinyGet into a bat file and use the Scheduled Tasks to run it periodically.

A better way would be to pre-compile the site and deploy the compiled version. For small sites, I prefer deploying the source, this way I can do incremental updates to my site, that is, deploy only the changed files. 

Guess the movie

Well hello Mister Fancypants. Well, I've got news for you pal, you ain't leadin' but two things: Jack and shit... and Jack just left town.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: Tools
Posted by CynotWhyNot on Friday, April 04, 2008 4:03 PM
Permalink | Comments (14) | Post RSSRSS comment feed

.NET Developers Toolbox: LINQPad

I'm a bit of a do-it-yourself-er. I've wired up complete basements. Installed kitchen sinks, plumbing and all. Sometimes I have the correct tools, other times not. When I don't have the correct tools I sometimes improvise using tools that were not necessarily designed for the work at hand. Almost invariably it ends up taking more time than if I went out and purchased the tool or even worse the job gets completed on time but is of poor quality.

The same can be said of programming, the right tool for the job at hand is invaluable. Take for example LINQPad. This SQL Management Console like tool but for LINQ allows you to construct LINQ queries and run them directly in the LINQPad IDE. I've tried simple LINQ queries and it works great! Thanks Joseph Albahari, another tool to add to our Toolbox.

Guess the movie

I killed two people. One was... yesterday? He was just a boy and I led him into quicksand. The other was... well, before Aqaba. I had to execute him with my pistol, and there was something about it that I didn't like... I enjoyed it.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: Tools
Posted by CynotWhyNot on Tuesday, March 25, 2008 8:32 AM
Permalink | Comments (9) | Post RSSRSS comment feed