ASP.NET Runtime Cheat Sheet: HttpRequest, HttpRuntime, AppDomain and friends

This article has cheat sheets for retrieving various bits of ASP.NET runtime information. For each bit of info, there is:

* A code snippet for retrieving it in a page, with a link to MSDN
* Description, sometimes explanation
* The value for the live ASP.NET app backing this site”

ASP.NET Runtime Cheat SheetHttpRequest, HttpRuntime, AppDomain and friends

Troubleshooting Expired ASP.NET Session State and Your Options

I have a love/hate relationship with the ASP.NET Session. It’s such a convenient place to put things, but when you start putting applications into production there are a number of less-than-obvious edge cases that can come up and bite you.

Most often the Session is used when managing state over a long process like a multi-step wizard or questionnaire. However, when people use the Session, they often lean on it a little. They’ll bake it into their design so deep that when it doesn’t work, they’re screwed. That’s not to say they shouldn’t be able to lean on it, I’m just saying that there’s a lot of things going on with Session (not just on ASP.NET, but other frameworks as well) in order to get it to look seamless.

Scott Hanselman’s Computer ZenTroubleshooting Expired ASP.NET Session State and Your Options

Rhino Mocks Quick Reference

Last week I gave a presentation on mock objects for Software Professionals of Alaska. Once the PowerPointy hand-waving was out of the way, I illustrated the concepts with code examples using Rhino Mocks. I thought I was well-versed in Rhino Mocks before I started, but in the process of preparing the presentation I realized there was a lot that I didn’t know or had forgotten. So I distilled all the main facts and features down into a 3-page quick reference of tables and example usage. You might argue that 3 pages makes it a ‘slow reference,’ but regardless I still think it’s useful to see all the basics stripped down to the bare minimum. Much of the content came from the Rhino Mocks Documentation Wiki, so look there first if you need more details on a particular feature.

Oran DennisonRhino Mocks Quick Reference

Copy project files to another location after build

For a (web-)project I am working on, I needed to copy the content of 1 website to a sub-folder of another website after the project was built. This enables the 2 projects being separately developed and deployed, but also allows the one project being deployed as a sub-site of the other one. Got it? 🙂

This is how I did this:
In the project file of the sub-project:

  1. In the project file I defined a few variables to make things easier
    <PropertyGroup>
    <MyOutputFolder>C:ProjectOutputFolder</MyOutputFolder>
    <OutputSubFolder>SubFolder</OutputSubFolder>
    </PropertyGroup>
  2. I defined what files to copy, with the binary output (dll) separately from the website content:
    <ItemGroup>
    <MyCopyItems Include="$(MSBuildProjectDirectory)***.aspx" />
    <MyCopyItems Include="$(MSBuildProjectDirectory)***.htm?" />
    <MyCopyItems Include="$(MSBuildProjectDirectory)***.js" />
    <MyCopyItems Include="$(MSBuildProjectDirectory)***.gif" />
    <MyCopyItems Include="$(MSBuildProjectDirectory)***.jp?g" />
    <MyCopyItems Include="$(MSBuildProjectDirectory)***.png" />
    <MyCopyBin Include="$(MSBuildProjectDirectory)bin***.dll" />
    </ItemGroup>
  3. I created a new target that performs 2 copy commands, one for the binaries and one for the content:
    <Target Name="MyPostBuildTarget">
    <Copy SourceFiles="@(MyCopyItems)"
    DestinationFiles="@(MyCopyItems->'$(MyOutputFolder)$(OutputSubFolder)%(RecursiveDir)%(Filename)%(Extension)')" />
    <Copy SourceFiles="@(MyCopyBin)"
    DestinationFolder="$(MyOutputFolder)bin" />
    </Target>
  4. I added this target to the BuildDependsOn property so that it is executed everytime the project is built:
    <BuildDependsOn>$(BuildDependsOn);MyPostBuildTarget</BuildDependsOn>

This gives something like:

<!-- Copy project output to defined folder -->
<PropertyGroup>
<BuildDependsOn>$(BuildDependsOn);MyPostBuildTarget</BuildDependsOn>
<MyOutputFolder>C:ProjectOutputFolder</MyOutputFolder>
<OutputSubFolder>SubFolder</OutputSubFolder>
</PropertyGroup>
<ItemGroup>
<MyCopyItems Include="$(MSBuildProjectDirectory)***.aspx" />
<MyCopyItems Include="$(MSBuildProjectDirectory)***.htm?" />
<MyCopyItems Include="$(MSBuildProjectDirectory)***.js" />
<MyCopyItems Include="$(MSBuildProjectDirectory)***.gif" />
<MyCopyItems Include="$(MSBuildProjectDirectory)***.jp?g" />
<MyCopyItems Include="$(MSBuildProjectDirectory)***.png" />
<mycopybin include="$(MSBuildProjectDirectory)bin***.dll" />
</ItemGroup>
<Target Name="MyPostBuildTarget">
<Copy SourceFiles="@(MyCopyItems)"
DestinationFiles="@(MyCopyItems->'$(MyOutputFolder)$(OutputSubFolder)%(RecursiveDir)%(Filename)%(Extension)')" />
<Copy SourceFiles="@(MyCopyBin)"
DestinationFolder="$(MyOutputFolder)bin" />
</Target>

Some references on copying project files with MSBuild:
MSDN MSBuild Reference – Copy Task
Channel 9 Wiki: Copy Built Output Of A Visual Studio Project
SharpDevelop Community – Copy config post build
How To: Insert Custom Process at Specific Points During Build
How To: Add Custom Process at Specific Points During Build (Method #2)

How To Create A Custom Control

  1. Create a new ASP.NET Web Application, Windows Forms Control Library or Class Library project
  2. Create a new class and inherit from System.Web.UI.WebControls.WebControl or from a specific control (like TextBox)
  3. Override the Render method and call writer.write() to write raw HTML to the page. ASP.Net server tags won’t work
  4. Use WriteBeginTag(), WriteEndTag(), RenderBeginTag(), RenderEndTag(), AddAttribute(), AddStyleAttribute() and WriteAttribute() to create HTML tags and attributes
  5. Add design-time attributes to properties, like:
    [Category(“Appearance”)]
    [Description(“The text to be shown in the control”)]
  6. Define a tagprefix attribute:
    [assembly: System.Web.UI.TagPrefix(“Anthoro.WebControls”, “awc”)]
    namespace Anthoro.WebControls
    { …
  7. Add a toolbox icon:
    1. Create a 16×16 pixel bitmap (BMP 16 colors) with the same name as the control.
    2. Add it to the root of your project
    3. Set its Build Action to “Embedded Resource”
  8. Add additional resources (stylesheet, images, text files, …) and mark them as “Embedded Resource” also
  9. Add an Assembly attribute for each resource to your AssemblyInfo.cs:
    [assembly: WebResource(“Anthoro.Controls.style.css”, “text/css”, PerformSubstitution = true)]
    When the PerformSubstitution parameter is set to true, the resource will be processed and other webresource URL’s inside it will be parsed and replaced (works only for text-based resources:
    background-image: url(‘<%= WebResource("Anthoro.Controls.back.png")%>‘);
  10. Pay attention to the full namespace and folder structure where your resources are stored. The resource name is build as: Namespace + Subfolders + filename
  11. At design-time, the control is displayed by running the rendering logic. To override this, use the GetDesignTimeHtml(), GetEmptyDesignTimeHtml() and GetErrorDesignTimeHtml() methods

Update: Unlike in C#, in VB.Net the subfolders are not taken into account with the namespace. So the example above in point 10 will be: Namespace + filename

Database Drivers not provided by Microsoft (Updated) – Joseph Guadagno

I wanted save some people some time looking for providers for their non-Microsoft databases to use with .NET. Please keep in mine I have not tried or used all of these providers / tools but have found them from searching and asking around. Based on some of the feed back and comments I added a few others.

Joseph Guadagno: Database Drivers not provided by Microsoft (Updated)

Directory Structure For Projects

Directory Structure For Projects

That last part in the title is to indicate that for me, this is
something that has changed several times over the past year with a
change happening even within the last month.
Let me stress the fact that I am a big automated build junkie, and
am not really even a fan of compiling from within studio. To that end I
do the majority of my work using studio + ReSharper as the editor, and
NAnt (currently) as the compile/test tool coupled with FinalBuilder as
my deployment tool.

From Jean-Paul S. BoodHoo’s Blog: Develop With Passion

Directory Structure For Projects

Directory Structure For Projects

That last part in the title is to indicate that for me, this is
something that has changed several times over the past year with a
change happening even within the last month.
Let me stress the fact that I am a big automated build junkie, and
am not really even a fan of compiling from within studio. To that end I
do the majority of my work using studio + ReSharper as the editor, and
NAnt (currently) as the compile/test tool coupled with FinalBuilder as
my deployment tool.

From Jean-Paul S. BoodHoo’s Blog: Develop With Passion