<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Terrapin Station</title>
	<atom:link href="http://terrapinstation.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://terrapinstation.wordpress.com</link>
	<description>Adventures in coding</description>
	<lastBuildDate>Mon, 19 Dec 2011 21:33:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='terrapinstation.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Terrapin Station</title>
		<link>http://terrapinstation.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://terrapinstation.wordpress.com/osd.xml" title="Terrapin Station" />
	<atom:link rel='hub' href='http://terrapinstation.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Retrieving CPU Usage in .NET</title>
		<link>http://terrapinstation.wordpress.com/2008/07/28/retrieving-cpu-usage-in-net/</link>
		<comments>http://terrapinstation.wordpress.com/2008/07/28/retrieving-cpu-usage-in-net/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 17:26:29 +0000</pubDate>
		<dc:creator>terrapinstation</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CPU Load]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://terrapinstation.wordpress.com/?p=12</guid>
		<description><![CDATA[Here&#8217;s some simple and effective code to fetch CPU usage / load percentage in either ASP.NET or a .NET forms application. WIth the forms application, we&#8217;ll set it up to auto refresh every half second. You can set it to longer or shorter if you want. Some load balancers can use a simple ASP.NET page [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=12&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s some simple and effective code to fetch CPU usage / load percentage in either ASP.NET or a .NET forms application. WIth the forms application, we&#8217;ll set it up to auto refresh every half second. You can set it to longer or shorter if you want.</p>
<p>Some <a href="http://en.wikipedia.org/wiki/Load_balancing_(computing)">load balancers</a> can use a simple ASP.NET page like this that displays a server&#8217;s processor load to help decide which server to shuttle a request to. This is what I have used the simple ASP.NET implementation for.</p>
<p>For each of these implementations, you will need to add a reference to System.Management to your project.</p>
<p><strong>ASP.NET CPU Load Percentage (C#)<br />
</strong></p>
<pre>using System;
using System.Management;

public partial class cputime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ObjectQuery qry = new ObjectQuery("select * from Win32_Processor");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(qry);

        int load = 0;
        int numCpus = 0;
        foreach (ManagementObject mgmt in searcher.Get())
        {
            load += Convert.ToInt32(mgmt["LoadPercentage"]);
            numCpus++;
        }

        Response.Write(String.Format("{0}", load/numCpus));
    }
}</pre>
<p><strong>.NET Forms Application CPU Load Percentage (C#)</strong></p>
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Threading;

namespace CPULoadPercentage
{
    public partial class Form1 : Form
    {
        delegate void SetTextCallback(string text);

        public Form1()
        {
            InitializeComponent();
            Thread loadThread = new Thread(new ThreadStart(outputLoad));
            loadThread.Start();
        }

        private void outputLoad()
        {
            ObjectQuery qry = new ObjectQuery("select * from Win32_Processor");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(qry);
            int load;
            int numCpus;
            while (true)
            {
                Thread.Sleep(10); //milliseconds
                load = 0;
                numCpus = 0;
                foreach (ManagementObject mgmt in searcher.Get())
                {
                    load += Convert.ToInt32(mgmt["LoadPercentage"]);
                    numCpus++;
                }
                setText(String.Format("{0}%",load / numCpus));
            }
        }

        private void setText(string text)
        {
            if (lblLoad.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(setText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                lblLoad.Text = text;
            }
        }
    }
}</pre>
<p>If you want to change how often the label refreshes with the CPU load, just change the Thread.Sleep(10) to however many <a href="http://en.wikipedia.org/wiki/Millisecond">milliseconds</a> you want between refreshes. Remember &#8211; there are 1000 milliseconds per second.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terrapinstation.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terrapinstation.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terrapinstation.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terrapinstation.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terrapinstation.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terrapinstation.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terrapinstation.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terrapinstation.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terrapinstation.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terrapinstation.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=12&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terrapinstation.wordpress.com/2008/07/28/retrieving-cpu-usage-in-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">AJ Rabe</media:title>
		</media:content>
	</item>
		<item>
		<title>Caching in ASP.NET</title>
		<link>http://terrapinstation.wordpress.com/2008/06/13/caching-in-aspnet/</link>
		<comments>http://terrapinstation.wordpress.com/2008/06/13/caching-in-aspnet/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 20:07:16 +0000</pubDate>
		<dc:creator>terrapinstation</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://terrapinstation.wordpress.com/?p=10</guid>
		<description><![CDATA[Without a doubt, caching can greatly improve performance on your website, or any other application. But if it isn&#8217;t done correctly, it can work against you. Luckily, ASP.NET has a great built in cache manager in System.Web.Caching. It&#8217;s very easy to use, and it seamlessly handles (most of) the pitfalls that can get you into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=10&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Without a doubt, caching can greatly improve performance on your website, or any other application. But if it isn&#8217;t done correctly, it can work against you.</p>
<p>Luckily, ASP.NET has a great built in cache manager in <a title="System.Web.Caching" href="http://msdn.microsoft.com/en-us/library/system.web.caching.aspx">System.Web.Caching</a>. It&#8217;s very easy to use, and it seamlessly handles (most of) the pitfalls that can get you into trouble. What&#8217;s nice about it is that the ASP.NET worker process will scavenge through your cache if it&#8217;s running low on memory.</p>
<p>So <strong>what is caching</strong>? Caching is taking a chunk of data and temporarily sticking it in any easy to reach place. For caching to work for you (instead of against you), that temporary place (called the &#8220;cache&#8221;) has to be easier to reach than the place you originally got the data. The cache is always dynamic and temporary.</p>
<p>So <strong>why does caching improve performance</strong>? Because you&#8217;re taking a hard to reach piece of data and making it temporarily easier to reach. It&#8217;s like this &#8211; if you&#8217;re driving through the streets of Saint Paul, MN, <a href="http://query.nytimes.com/gst/fullpage.html?res=9B07E3DD173CF935A15751C0A96F958260&amp;n=Top/Reference/Times%20Topics/People/V/Ventura,%20Jesse">you&#8217;re going to need a map</a>. If that map is in your backseat, you can temporarily cache it in your front passenger seat so it&#8217;s handy when you need to reference it. When you&#8217;ve left Saint Paul, you can throw in the backseat for next time. If you have a list of movie genres stored in your database that you need to access often, you can store that list in a memory cache. Because reading data from memory is much faster than reading it from a database, each request will be that much quicker.</p>
<p>So <strong>when should you cache</strong>? Basically, if you have a database or an I/O (hard drive read access) operation that needs to happen often for a broad range of requests, there&#8217;s a good chance that you can greatly improve performance by caching that data in memory the first time you grab it.</p>
<p>So <strong>when should you NOT cache</strong>? You shouldn&#8217;t cache data that is not used often<em>.</em> You shouldn&#8217;t cache data that is huge (this is kind of a judgment call). You shouldn&#8217;t use cache if you can&#8217;t use it judiciously. Remember &#8211; when you use caching in ASP.NET, you&#8217;re putting the object into memory. If you put too much stuff in memory, it can cause a lot of IIS application pool recycles; it can slow things down not only for your site, but for the entire Web server. Be nice to the cache and it will be nice to you.</p>
<p><strong>Caching in ASP.NET C#<br />
</strong></p>
<pre>DataTable dtGenres = null;
try
{
    string cachekey = "Genres";
    dtGenres = (DataTable)HttpContext.Current.Cache[cachekey];
    if (dtGenres == null)
    {
        //the Genres weren't cached yet, so fetch the data, and add it to the cache for next time
        dtGenres = Genres.GetFromDB(); //put your own database code in here.
        HttpContext.Current.Cache.Insert(cachekey, dtGenres);
    }
}
catch (Exception ex)
{
    //handle exception
}</pre>
<p>That&#8217;s about as basic as we can get with a cache example. Remember &#8211; it doesn&#8217;t have to be just DataTables that go into the cache. You can cache any object.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terrapinstation.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terrapinstation.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terrapinstation.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terrapinstation.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terrapinstation.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terrapinstation.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terrapinstation.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terrapinstation.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terrapinstation.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terrapinstation.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=10&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terrapinstation.wordpress.com/2008/06/13/caching-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">AJ Rabe</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.NET Custom Configuration Settings in web.config</title>
		<link>http://terrapinstation.wordpress.com/2008/06/13/aspnet-custom-configuration-settings-in-webconfig/</link>
		<comments>http://terrapinstation.wordpress.com/2008/06/13/aspnet-custom-configuration-settings-in-webconfig/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 15:55:31 +0000</pubDate>
		<dc:creator>terrapinstation</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://terrapinstation.wordpress.com/?p=7</guid>
		<description><![CDATA[Custom configuration settings for ASP.NET in the Web.config file.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=7&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Your Web site may have a need for configuration settings like the email address your contact form should send to, your <a title="Google Maps API" href="http://code.google.com/apis/maps/">Google Maps API</a> key, or whatever. You could store those in a database, and even <a title="Caching in ASP.NET" href="http://terrapinstation.wordpress.com/2008/06/13/caching-in-aspnet/">cache them as a DataTable</a> using <a title="System.Web.Caching" href="http://msdn.microsoft.com/en-us/library/system.web.caching.aspx">System.Web.Caching</a>, but ASP.NET actually provides an easier, more efficient method &#8211; store your configuration settings in a config section in your web.config. This method not only saves you from a trip to the database, and from adding another object to the cache (web.config is already cached), it has the added benefit of providing an easy way to silo your development, testing and production environments with different settings.</p>
<p><strong>Add a configuration setionGroup to Web.config</strong></p>
<pre>&lt;?xml version="1.0"?&gt;
&lt;configuration&gt;
    &lt;system.web&gt;
        &lt;!-- ... --&gt;
    &lt;/system.web&gt;
    &lt;configSections&gt;
        &lt;sectionGroup name="MyCustomConfigSection"&gt;
            &lt;section name="MySettings" type="System.Configuration.NameValueSectionHandler"/&gt;
        &lt;/sectionGroup&gt;
    &lt;/configSections&gt;
    &lt;MyCustomConfigSection&gt;
        &lt;MySettings&gt;
            &lt;add key="SomeSetting" value="SettingValue" /&gt;
            &lt;add key="ContactFormEmail" value="some@guy.com" /&gt;
        &lt;/MySettings&gt;
    &lt;/MyCustomConfigSection&gt;
&lt;/configuration&gt;</pre>
<p><strong>Create a Settings class to read your settings<br />
</strong></p>
<pre>using System;
using System.Web;
using System.Web.Security;
using System.Configuration;
using System.Collections.Specialized;

public class Settings
{
    static Settings()
    {
        config = new NameValueCollection();
        config = (NameValueCollection)ConfigurationSettings.GetConfig("MyCustomConfigSection/MySettings");
    }

    private static NameValueCollection config;

    public static string Get(string key)
    {
        string rtn = "";
        try
        {
            rtn = config[key];
        }
        catch (Exception ex)
        {
            //handle the exception
        }
        return rtn;
    }
}</pre>
<p><strong>Implement It</strong></p>
<pre>string emailAddress = Settings.Get("ContactFormEmail");</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terrapinstation.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terrapinstation.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terrapinstation.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terrapinstation.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terrapinstation.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terrapinstation.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terrapinstation.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terrapinstation.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terrapinstation.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terrapinstation.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=7&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terrapinstation.wordpress.com/2008/06/13/aspnet-custom-configuration-settings-in-webconfig/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">AJ Rabe</media:title>
		</media:content>
	</item>
		<item>
		<title>Restart IIS application pool from ASP.NET page</title>
		<link>http://terrapinstation.wordpress.com/2008/06/12/restart-iis-application-pool-from-aspnet-page/</link>
		<comments>http://terrapinstation.wordpress.com/2008/06/12/restart-iis-application-pool-from-aspnet-page/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:45:16 +0000</pubDate>
		<dc:creator>terrapinstation</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://terrapinstation.wordpress.com/?p=6</guid>
		<description><![CDATA[Restarting IIS application pools via ASP.NET page.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=6&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been developing this .NET class library as a COM object consumable by Classic ASP. Every time I go to build my project, it would tell me:</p>
<blockquote>
<pre>Unable to copy file "..\Core\bin\Debug\Core.dll" to "bin\Debug\Core.dll". The process cannot access the file 'bin\Debug\Core.dll' because it is being used by another process.</pre>
</blockquote>
<p>It turns out that the process that had a lock on my dll file was the IIS application pool process. After about a day of using remote desktop to login to the development server, opening up IIS and stopping the application pool, pressing Alt-Tab to get back to Visual Studio and swearing because Remote Desktop hijacks my Alt-Tab, painstakingly lifting my hand to my mouse, minimizing Remote Desktop, maximizing Visual Studio and building my library, trying the new build in my browser and swearing when I see &#8220;Service Unavailable&#8221; because the application pool is stopped&#8230; you get the picture.</p>
<p>This code enables you to stop and start your application pool from the comfort of your own browser. It also gives you your application pool&#8217;s status by monitoring AppPoolState.</p>
<p><strong>IIS Application Pool restart .aspx page</strong></p>
<pre>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="iis.aspx.cs" Inherits="service.iis" %&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
&lt;head runat="server"&gt;
    &lt;title&gt;IIS App Restart&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
        Status: &lt;asp:Label ID="lblStatus" runat="server" /&gt;&lt;br/&gt;
        &lt;asp:Button ID="btnStop" Text="STOP App Pool" BackColor="IndianRed" ForeColor="White" runat="server" CommandArgument="dev.somesite.com" OnClick="stopAppPool" /&gt;&lt;br /&gt;
        &lt;asp:Button ID="btnStart" Text="START App Pool" BackColor="Lime" runat="server" CommandArgument="dev.somesite.com" OnClick="startAppPool" /&gt;&lt;br /&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Remember to replace &#8220;dev.somesite.com&#8221; in the CommandArgument attribute of the two buttons with the name of your application pool.</p>
<p><strong>Codebehind .aspx.cs file</strong></p>
<pre>using System;
using System.Web;
using System.Web.UI;
using System.Management;
using System.DirectoryServices;
using System.Web.UI.WebControls;

public partial class iis : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(System.Environment.MachineName);
        status();
    }

    protected void status()
    {
        string appPoolName = "dev.somesite.com";
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        int intStatus = 0;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            intStatus = (int)w3svc.InvokeGet("AppPoolState");
            switch (intStatus)
            {
                case 2:
                    lblStatus.Text = "Running";
                    break;
                case 4:
                    lblStatus.Text = "Stopped";
                    break;
                default:
                    lblStatus.Text = "Unknown";
                    break;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    protected void stopAppPool(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string appPoolName = btn.CommandArgument;
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            w3svc.Invoke("Stop", null);
            status();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    protected void startAppPool(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string appPoolName = btn.CommandArgument;
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            w3svc.Invoke("Start", null);
            status();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}</pre>
<p>You should probably stick this little page on a separate site using a different application pool <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terrapinstation.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terrapinstation.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terrapinstation.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terrapinstation.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terrapinstation.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terrapinstation.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terrapinstation.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terrapinstation.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terrapinstation.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terrapinstation.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=6&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terrapinstation.wordpress.com/2008/06/12/restart-iis-application-pool-from-aspnet-page/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">AJ Rabe</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.NET HTTP Comression and reducing response size</title>
		<link>http://terrapinstation.wordpress.com/2008/06/12/aspnet-http-comression-and-reducing-response-size/</link>
		<comments>http://terrapinstation.wordpress.com/2008/06/12/aspnet-http-comression-and-reducing-response-size/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:01:16 +0000</pubDate>
		<dc:creator>terrapinstation</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[HTTP Compression]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://terrapinstation.wordpress.com/?p=4</guid>
		<description><![CDATA[Compression is important on the Web. Pre-compression, my pages were sometimes 700 KB+! Granted, this was in a development environment, so more than half of the page was dedicated to debug and trace data, but still, under high traffic, a large page can put unnecessary strain on your Web server and bandwidth/throughput, thus slowing your [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=4&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Compression is important on the Web. Pre-compression, my pages were sometimes 700 KB+! Granted, this was in a development environment, so more than half of the page was dedicated to debug and trace data, but still, under high traffic, a large page can put unnecessary strain on your Web server and bandwidth/throughput, thus slowing your site for all of your visitors.</p>
<p>I did a couple of things to drastically reduce the sizes of my pages. First and foremost, I disabled that ASP.NET event validation crap. ASP.NET purists might tell me that I&#8217;ve committed a mortal sin, but really&#8230; I don&#8217;t need it and chances are you don&#8217;t need it either. If you&#8217;re really that worried, just hit Google and find a pros/cons lists.</p>
<p>You may even be able to turn off the <a href="http://msdn.microsoft.com/en-us/library/ms972976.aspx">View State</a>, but that broke my pages.</p>
<p><strong>Turning off ASP.NET Event Validation in web.config</strong></p>
<pre><strong></strong>&lt;system.web&gt;
    &lt;pages enableEventValidation="false" /&gt;
&lt;/system.web&gt;</pre>
<p>Okay, so now I&#8217;ve saved a few precious KB.  <a title="K. Scott Allen" href="http://odetocode.com/Blogs/scott/default.aspx">K. Scott Allen</a> has an article related to Event Validation that goes a bit more in-depth, specifically how to <a title="user controls register for event validation" href="http://odetocode.com/Blogs/scott/archive/2006/03/21/3153.aspx">register individual user controls for event validation</a>, and turning off event validation for just one page.</p>
<p>Now the meet and potatoes &#8211; here&#8217;s how I reduced my overall response size by 52% from an initial 787 KB to 376 KB.</p>
<p><strong>Implementing HTTP <a href="http://en.wikipedia.org/wiki/Gzip">gzip</a>/<a href="http://en.wikipedia.org/wiki/Deflate">deflate</a> compression in Global.asax (in C#)</strong></p>
<pre>void Application_BeginRequest(object sender, EventArgs e)
{
    HttpCompress((HttpApplication)sender);
}

private void HttpCompress(HttpApplication app)
{
    try
    {
        string accept = app.Request.Headers["Accept-Encoding"];
        if (accept != null &amp;&amp; accept.Length &gt; 0)
        {
            if (CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
            {
                Stream stream = app.Response.Filter;
                accept = accept.ToLower();
                if (accept.Contains("gzip"))
                {
                    app.Response.Filter = new GZipStream(stream, CompressionMode.Compress);
                    app.Response.AppendHeader("Content-Encoding", "gzip");
                }
                else if (accept.Contains("deflate"))
                {
                    app.Response.Filter = new DeflateStream(stream, CompressionMode.Compress);
                    app.Response.AppendHeader("Content-Encoding", "deflate");
                }
            }
        }
    }
    catch (Exception ex)
    {
        //handle the exception
    }
}

private bool CompressScript(string scriptName)
{
    if (scriptName.ToLower().Contains(".aspx")) return true;
    if (scriptName.ToLower().Contains(".axd")) return false;
    if (scriptName.ToLower().Contains(".js")) return false;
    return true;
}</pre>
<p>What we&#8217;ve done here is we&#8217;ve wrapped the response in a Response.Filter object, which lets us manipulate the response before sending it back to the browser. Careful &#8211; there are is a known issue when <a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;814206">using Server.Transfer with HTTP Filters</a>.</p>
<p>The bit where we prevent compression of a couple file extentions (see CompressScript(string scriptName)) is important, because ASP.NET doesn&#8217;t like it when you compress WebResource.axd, since that&#8217;s the file that contains the JavaScript postback code.</p>
<p>That&#8217;s pretty simple code to reduce the size of your response by over 50%. I usually implement this with a <a title="custom web.config configuration setting" href="http://terrapinstation.wordpress.com/2008/06/13/aspnet-custom-configuration-settings-in-webconfig/">config setting in web.config</a> that allows me to easily turn on/off the compression just in case breaks  something. You could even take that a step further and turn it off on a per page level by looking at <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcontext.request(VS.71).aspx">HttpContext.Current.Request</a>.Url.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terrapinstation.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terrapinstation.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terrapinstation.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terrapinstation.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terrapinstation.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terrapinstation.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terrapinstation.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terrapinstation.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terrapinstation.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terrapinstation.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=4&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terrapinstation.wordpress.com/2008/06/12/aspnet-http-comression-and-reducing-response-size/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">AJ Rabe</media:title>
		</media:content>
	</item>
		<item>
		<title>Custom .NET caching</title>
		<link>http://terrapinstation.wordpress.com/2008/06/12/custom-net-caching/</link>
		<comments>http://terrapinstation.wordpress.com/2008/06/12/custom-net-caching/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 20:09:02 +0000</pubDate>
		<dc:creator>terrapinstation</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Caching]]></category>

		<guid isPermaLink="false">http://terrapinstation.wordpress.com/?p=3</guid>
		<description><![CDATA[Implementing a simple custom caching class for .NET for use in stand alone class libraries.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=3&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It turns out that it&#8217;s hard to implement caching in class libraries in .NET. The enterprise libraries have a Caching Application Block, but this is nice and light-weight, and it works nicely as a standalone, which the Caching Application Block won&#8217;t do for you because it requires a config file. ASP.NET has great support for Web caching in System.Web.Caching, and a few examples on the net actually suggest referencing that assembly in your class libraries or Win Forms apps, but that seems kind of&#8230; breaky&#8230; so here&#8217;s a simple custom cache implementation in C# that handles expiration. It&#8217;s not quite as nice as System.Web.Caching, but if you don&#8217;t like it, don&#8217;t use it.</p>
<p>I use this in a COM visible class library that gets used by a Classic ASP site. Works great.</p>
<p><strong>CacheFactory class</strong></p>
<pre>public class CacheFactory
{
    private Hashtable _data;

    public CacheFactory()
    {
        _data = new Hashtable();
    }

    #region properties
    public object this[string key]
    {
        get
        {
            object rtn = null;
            RemoveExpiredItems();
            CacheItem item = (CacheItem)_data[key];
            if (item != null)
            {
                rtn = item.Data;
            }
            return rtn;
        }
    }

    public int Count
    {
        get
        {
            RemoveExpiredItems();
            return _data.Count;
        }
    }

    public ICollection Keys
    {
        get
        {
            RemoveExpiredItems();
            return _data.Keys;
        }
    }
    #endregion

    public void Add(string key, object data)
    {
        Add(key, data, 0);
    }
    public void Add(string key, object data, double minutes)
    {
        DateTime expiresAt;
        if (minutes &gt; 0)
            expiresAt = DateTime.Now.AddMinutes(minutes);
        else
            expiresAt = DateTime.MinValue;
        _data[key] = new CacheItem(key, data, expiresAt);
    }

    public void Remove(string key)
    {
        CacheItem item = (CacheItem)_data[key];
        if (item != null)
            _data.Remove(key);
    }

    public void Clear()
    {
        _data.Clear();
    }

    public DateTime ExpiresAt(string key)
    {
        DateTime rtn = DateTime.MinValue;
        CacheItem item = (CacheItem)_data[key];
        if (item != null)
        {
            rtn = item.ExpiresAt;
        }
        return rtn;
    }

    public string Name(string key)
    {
        RemoveExpiredItems();
        string rtn = null;
        CacheItem item = (CacheItem)_data[key];
        if (item != null)
        {
            rtn = item.Name;
        }
        return rtn;
    }

    public CacheItem GetCacheItem(string key)
    {
        RemoveExpiredItems();
        CacheItem item = (CacheItem)_data[key];
        return item;
    }

    public bool IsExpired(string key)
    {
        bool rtn = false;
        CacheItem item = (CacheItem)_data[key];
        if (item.Expires &amp;&amp; DateTime.Now &gt; item.ExpiresAt)
        {
            rtn = true;
        }
        return rtn;
    }

    protected void RemoveExpiredItems()
    {
        CacheItem item;
        DateTime expiresAt = DateTime.MaxValue;
        StringCollection keys = new StringCollection();
        foreach (string key in _data.Keys)
        {
            keys.Add(key);
        }

        foreach (string key in keys)
        {
            item = (CacheItem)_data[key];
            if (item == null || (item.Expires &amp;&amp; DateTime.Now &gt; item.ExpiresAt))
                _data.Remove(key);
        }
    }
}</pre>
<p><strong>CacheItem class</strong></p>
<pre>public class CacheItem
{
    private object _data;
    private DateTime _expiresAt;
    private string _name;

    public CacheItem(string name, object data, DateTime expiresAt)
    {
        _name = name;
        _data = data;
        _expiresAt = expiresAt;
    }

    public object Data
    {
        get
        {
            return _data;
        }
    }

    public DateTime ExpiresAt
    {
        get
        {
            return _expiresAt;
        }
    }

    public string Name
    {
        get
        {
            return _name;
        }
    }

    public bool Expires
    {
        get
        {
            return (ExpiresAt != DateTime.MinValue);
        }
    }
}</pre>
<p>References:</p>
<p>http://authors.aspalliance.com/aldotnet/</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/terrapinstation.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/terrapinstation.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terrapinstation.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terrapinstation.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terrapinstation.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terrapinstation.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terrapinstation.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terrapinstation.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terrapinstation.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terrapinstation.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terrapinstation.wordpress.com&amp;blog=3964055&amp;post=3&amp;subd=terrapinstation&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terrapinstation.wordpress.com/2008/06/12/custom-net-caching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">AJ Rabe</media:title>
		</media:content>
	</item>
	</channel>
</rss>
