A sign you've been working too many hours lately

by Jon Sagara Sunday, August 31 2008 8:29 PM

You get sloppy and write something like this:

if (string.IsNullOrEmpty(campaignId))

{

    throw new Exception(string.Format("Missing campaign ID in the query string: {0}", campaignId));

}

D'oh.

Be the first to rate this post

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

Tags:

Blog

Enumerating HttpModules

by Jon Sagara Sunday, August 24 2008 12:33 PM

One common ASP.NET performance tip is to remove any HttpModules that your application does not use.  You can take a peek at which modules are loaded by the framework on your behalf by examining the framework's Web.config file, but how do you find out which modules are actually loaded in the current context?

Fortunately, the HttpApplication instance provides a Modules collection that you can loop through:

// Get a collection of modules loaded for the current context.

HttpModuleCollection modules = HttpContext.Current.ApplicationInstance.Modules;

 

// Enumerate the loaded HttpModules and do something with them.  For example, you can

//  create a table, listing each module by key and full type name.

// NOTE: the key is the same key specified in Web.config to add the module.

foreach (string moduleKey in modules.Keys)

{

    IHttpModule module = modules[moduleKey];

 

    // Do something with the module key and/or the HttpModule.

}

You can then use the Modules collection create a table like this:  

And boom, there you have it: a list of HttpModules that are loaded for the current context.  You can use this information to determine which HttpModules are needed by your application, and which ones you can safely remove.

Currently rated 5.0 by 2 people

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

Tags: ,

Blog

Organize Usings

by Jon Sagara Sunday, August 24 2008 12:09 PM

In Visual Studio 2008, when you add a new Web form to your project, the "using" section of the code-behind contains a lot noise:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

You may very well need some of those namespace declarations, but odds are high that you don't need them all.  But which ones can you remove without breaking your page?

Visual Studio has a nice feature called Organize Usings, which you access by right-clicking anywhere in the code-behind page.  The menu looks like this:

When you click on "Remove and Sort", it removes any unused "using"s and sorts those that remain. 

So now, instead of the mess that we had before, we have a much cleaner "using" section in our source code file:

using System;

Pretty slick, huh?

Be the first to rate this post

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

Tags: ,

Blog

try/swallow

by Jon Sagara Thursday, August 21 2008 10:33 AM

Dear fellow coders,

Please don't ever do this:

private static void SomeFunc()

{

    try

    {

        DoSomething();

        DoSomethingElse();

    }

    catch

    {

        // eat the exception  <-- NO!  BAD CODER!

    }

}

Love,

Jon

Be the first to rate this post

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

Tags: ,

Blog

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Jon Sagara develops ASP.NET Web applications in Sacramento, CA.