Dylan Marks on Web Application Development http://www.dioramadesign.net/blog Notes on web application design using .NET and XHTML Tue, 01 May 2007 15:40:36 +0000 http://wordpress.org/?v=2.3.2 en optimizing XSL in .NET 2.0 http://www.dioramadesign.net/blog/optimizing-xsl-in-net-20/ http://www.dioramadesign.net/blog/optimizing-xsl-in-net-20/#comments Tue, 01 May 2007 15:39:27 +0000 dmarks http://www.dioramadesign.net/blog/optimizing-xsl-in-net-20/ This week I spent some time with the excellent Jet Brains dotTrace Profiler, which allows you to see exactly how many milliseconds every method of your code is taking. I found the performance profiling quite easy to use, and within a few minutes I had already isolated a few problem areas in our application. 

One of the main trouble spots I came across was with the XslCompiledTransform class, which replaces the new obsolete XsltTransform class.  This class, new to .net 2.0, performs way faster because it actually loads and compiles the xslt into MSIL.  We rely heavily on xslts in our applications, with almost all html being defined in xslts instead of embedded inside app code. I’ll go into detail on this in a future post.

Using the profiler, I was noticing that loading xslt into XslCompiledTransform was taking a good 30-35% of the processing time for a page, since it is actually compiling the xslt on the fly. The solution? Either use caching, or make the compiled transform static, so that it reuses the same one instead of constantly recompiling it.

You may find my notes on httpcontext.cache helpful.

]]>
http://www.dioramadesign.net/blog/optimizing-xsl-in-net-20/feed/
Rejoice Midi Software http://www.dioramadesign.net/blog/rejoice-midi-software/ http://www.dioramadesign.net/blog/rejoice-midi-software/#comments Mon, 12 Mar 2007 17:02:06 +0000 dmarks http://www.dioramadesign.net/blog/rejoice-midi-software/ In an earlier post, I mentioned how I use a piece of software called Rejoice to map events from a usb joystick/gamepad to midi notes.  This is so I can use a cheap usb video game joystick as a controller for audio software, for example changing pitch or tone by pushing the joystick a certain direction, or firing off loops with buttons.  Anyhow, someone pointed out to me that the site for Rejoice is currently down, so I thought I’d upload the installer for it here, in case anyone is looking for it.

Rejoice will convert your Joystick or P5 Glove into a MIDI controller. Up to 1000 Button/Axis/POV actions on your joystick can be mapped to MIDI controllers. Download the installer here.  Please note that I’m only hosting this freeware software as a mirror. Although I’ve personally used it, I don’t know much at all about its internal workings. Unfortunately I don’t have access to the documentation that was also on the now defunct original site.

]]>
http://www.dioramadesign.net/blog/rejoice-midi-software/feed/
Some Notes on HttpContext.Cache http://www.dioramadesign.net/blog/some-notes-on-httpcontextcache/ http://www.dioramadesign.net/blog/some-notes-on-httpcontextcache/#comments Thu, 14 Dec 2006 19:40:55 +0000 dmarks http://www.dioramadesign.net/blog/some-notes-on-httpcontextcache/ In order to really push the efficiency of your ASP.NET application, I recommend using the HttpContext.Cache object for data that rarely changes. One thing I was experiencing with a high volume site was seemingly random errors to do with accessing cached objects. Consider the following simple example:

if (HttpContext.Current.Cache[“myObject”] != null)
    Control ctrl = (Control) HttpContext.Current.Cache[“myObject”];

This seems foolproof: first you check if the object is in the cache, and only retrieve it if it is. The problem is that the Cache is accessed in a multi-threaded environment, and so there could be potentially thousands of separate web requests happening at once for this object, with some of them perhaps clearing the cache and re-adding a new copy. It’s entirely possible for the cache to become null in the microsecond between checking for existence and actually retrieving from it.  To make this matter worse, it’s a bug that will likely not be caught until on production with heavy traffic, and is very hard to duplicate effectively.  So the simple way to handle this (without messing with locks) would be to first retrieve into an object, check that object for null, and then cast it:

object cachedCtrl = HttpContext.Current.Cache[“myObject”];
if (cachedCtrl != null)
    Control ctrl = (Control) cachedCtrl;

Always Set an Expiration on a Cache

In order to avoid the dreaded OutofMemoryException, you should make sure you are not loading too much into the cache. One way of doing this is to place a rolling timeout on your cached object. That way if it’s not being used frequently it will automatically be discarded to free up memory for other activities:

HttpContext.Current.Cache.Insert(“MyObject”, myObject, null,
                            System.Web.Caching.Cache.NoAbsoluteExpiration,
                            TimeSpan.FromHours(1));

If you are experiencing OutOfMemoryExceptions because of this sort of situation, a quick fix would be to clear the Application Pool for that application in IIS. You can then configure the app pool to automatically recycle the working process after it has consumed too much memory.  Open up IIS Manager and right click the associated Application Pool:

change the maximum virtual memory in the recycling tab

Obviously this should be considered a temporary fix until you are able to test and push out a new version of code with more long term fixes.

On HttpContext and NUnit

Working directly with HttpContext makes writing Unit Tests difficult. When NUnit runs, it doesn’t have the same ASP.NET HttpContext to work with, and thus will throw an error when you try to access it in your code.  Instead of accessing HttpContext directly, lately I’ve used a facade-type object that mimics the interface of HttpContext.  If the context is available, it returns that, and if not, it returns simple collections for storing cache etc. I just found an excellent implementation by Phillip J Haack that does this better than my code, so I will direct you there instead of posting my version:  Simulating Http Context For Unit Tests Without Using Cassinni nor IIS.

]]>
http://www.dioramadesign.net/blog/some-notes-on-httpcontextcache/feed/
The WiiMote as a Percussion Instrument http://www.dioramadesign.net/blog/using-the-wiimote-as-a-percussion-instrument/ http://www.dioramadesign.net/blog/using-the-wiimote-as-a-percussion-instrument/#comments Wed, 13 Dec 2006 18:36:36 +0000 dmarks http://www.dioramadesign.net/blog/using-the-wiimote-as-a-percussion-instrument/ This is somewhat off my regular topic of .net programming, but still damn geeky: using the Nintendo Wii’s remote as a virtual percussion instrument.  I’m not talking about banging it around as a drum stick! Read on and I’ll explain..

I was one of the lucky ones to score a new Nintendo Wii a week after it came out, and have been currently wasting too many evenings on Need For Speed Carbon of late (wii-mote as steering wheel rocks). The wii-mote is truly a lovely piece of hardware that demands to be assimilated to other uses. Hacker-bees over at wiili.org are working away on this as we speak. Since it uses standard bluetooth to communicate, it’s entirely possible to use it on windows as an infrared mouse pointer, as a system to transfer your miis, or as a midi drum machine.

I’m particularly interested in the idea of the wiimote as a controller for audio software, much like the link for the midi drum machine above. In my ’spare’ time (hah), I’ve been playing around with Ableton Live, audio software that allows you to do live sampling and looping. My end goal here is to supplement my band’s live performances with some tasteful instrument looping and ambient samples. I run my bass and a microphone through this software and can build up whole compositions in real time using loops (or at least that’s what I’m working towards mastering).

At some point I’d like to be able to use my wiimotes in this sort of live music performance venue. One problem with looping percussion instruments on stage is that the mic picks up background noise and degrades the quality. Using the wiimote as a shaker or virtual drumstick would allow you to avoid that ambient noise buildup, and still have the performance quality that crouching over your laptop on stage doesn’t convey.

The link for the wii drum machine above isn’t quite there: he’s just mapping buttons to certain drum samples, but he isn’t taking into account the velocity of the movement of the wiimote. Midi is not only an on/off signal, but also allows for ‘velocity’. That’s how an expensive electric piano will respond with different volume depending on how hard you strike the keys. Without taking into account the velocity of movement, you aren’t going to be able to duplicate realistic percussion.

Check out this video of someone actually using his wiimote as a controller for Ableton live.  This is really close to what I’m talking about, although he doesn’t provide any information on his approach. Note how by twisting and tilting the wiimote, he is shifting around the settings on an audio effect filter to make that modulation sound.

I actually have a usb gamepad hooked up to my laptop and have been using it as a controller for Ableton Live already. It works great, and running at $35 it is both way cheaper than dedicated midi controllers and has bonus novelty effect. I’m using a combination of Rejoice and MIDI Yoke for this (let me know if you would like more information on how that is done). 

Unfortunately, it’s going to take some more time for me to get my Wiimote working: first I need to get a bluetooth adapter for my laptop! I’m sure by the time I do that, those wiili hacker-bees will have figured it all out.

On a related topic, someone has also done a great job making the Nintendo DS into a wireless midi controller, although it requires some hard-to-find hardware to hack homebrew on to it. Check out the video on that page where he uses it as a virtual keyboard, and how he can ‘bend’ notes like a guitar.

]]>
http://www.dioramadesign.net/blog/using-the-wiimote-as-a-percussion-instrument/feed/
MSBuild and Solution Files http://www.dioramadesign.net/blog/msbuild-and-solution-files/ http://www.dioramadesign.net/blog/msbuild-and-solution-files/#comments Thu, 30 Nov 2006 17:42:54 +0000 dmarks http://www.dioramadesign.net/blog/msbuild-and-solution-files/ I find the vast majority of documentation on using MSBuild to compile Visual Studio solutions is way too complicated.  MSBuild is very powerful, in that you can customize a lot of what is going on in the build process.  However, sometimes you just want to do a simple compile of a visual studio solution in your msbuild script, without having to worry about reconstructing references and other such nonsense.  It’s a one-liner task:

    <MSBuild Projects=MySolution.sln           Properties=Configuration=Debug ContinueOnError=false />
 

I did a lot of research on MSBuild in the last month, and it was amazing just how long it took me to realise that MSBuild will parse .sln files for you.  I guess it was just too obvious (in other words, it says it right there if you go msbuild /? ).

You could do this from the command line as well instead of using devenv.exe: 
msbuild mysolution.sln /p:Configuration=Release

Solution files are not proper MSBuild scripts (unlike vbproj and csproj files), but MSBuild can still work with them. I recommend this excellent article by Sayed Ibrahim Hashimi if you want to go a bit deeper into using the solution file in MSBuild.  He talks about generating a proper .proj file from a solution file with a custom task, and then working with that for any other customization you require in your build process.

]]>
http://www.dioramadesign.net/blog/msbuild-and-solution-files/feed/
fun with the Firebug Extension http://www.dioramadesign.net/blog/fun-with-the-firebug-extension/ http://www.dioramadesign.net/blog/fun-with-the-firebug-extension/#comments Thu, 09 Nov 2006 21:05:04 +0000 dmarks http://www.dioramadesign.net/blog/fun-with-the-firebug-extension/ The other day I came across an article called Hacking Web 2.0 Applications with Firefox. This is a fantastic article for two reasons:

  1. It emphasizes just how little you should be trusting any client-side validation for ensuring the security of your web application. In other words, don’t be trusting anything that is submitted to your server-side just because you are doing it in a hidden ajax method or something.
  2. It highlights what I consider to be pretty much the most important tool in my web application toolbox: the Firebug Extension for Firefox.

This tool is not only a debugger for javascript; it also allows you to completely dissect a page’s html structure, css and javascript in seconds.  No more combing through View Source in order to figure out how a page is laid out, or trying to find what css is acting on what. Just hit F12 in firefox to open the Firebug panel and click the Inspect button. 

There’s a tonne of things you can do with this extension, but go install it and come back here and I’ll show you a fun little trick.

Are you back?  Ok, try this: click F12 to open the firebug panel and hit the inspect button.  With this you can see the shape of all the DOM elements on this page. Actually click on an element and you can do things like browse through the html tree, view any css styles acting on it etc etc.

using firebug inspector

Now you know what the html structure of the page looks like. Note the id for the top header of this page is called ‘header’.  Now we’ll take advantage of the fact that this blog uses the scriptaculous library in order to make the header on this page disappear right in front of you.  Click the Console tab in Firebug.  At the bottom, you can enter in arbitrary javascript and it will run in the context of your page. This is very useful for testing out javascript you are working on.  Enter new Effect.BlindUp(”header”); here.  You’ll see the whole header on the page slide up into nonexistence. 

using firebug console

 In order to get it back, you can enter new Effect.BlindDown(”header”);. 

This command wouldn’t work for other pages, because it relies on the scriptaculous library being linked on the page you are viewing.  However you can easily do something like document.getElementById(”dom-id-of-irritating-flashing-gif”).style.display= “none” on any page as a way to nuke any irritating ads.

]]>
http://www.dioramadesign.net/blog/fun-with-the-firebug-extension/feed/
Formatted Input Masks for Textboxes http://www.dioramadesign.net/blog/formatted-input-masks-for-textboxes/ http://www.dioramadesign.net/blog/formatted-input-masks-for-textboxes/#comments Wed, 08 Nov 2006 23:14:17 +0000 dmarks http://www.dioramadesign.net/blog/formatted-input-masks-for-textboxes/ One fancy way to provide a bit of extra usability sugar on a form is to have formatting automatically applied to a textfield as the user types. I’ve been using a fantastic Mask Formatter javascript library created by PengoWorks for some time now on a number of production sites. This javascript can be configured different ways to constrain the user’s input to only certain characters, validate date format, etc. Check out the documentation on the PengoWorks site for ways it can be configured.

I’m uploading a slightly tweaked copy here since the version on that site hasn’t seen any changes in quite a while. I’ve made some minor bug fixes to it, but unfortunately I didn’t document exactly what I’ve changed. Primarily, I’ve focused on cleaning up the date format implementation.

  1. First, view the examples at Pengoworks.
  2. then download my modified js source file.

One day I’d like to rewrite the logic here and make use of all of the enhancements in the prototype.js library. This one’s nice though, in that it doesn’t have any dependancies.

It goes without saying that this should not be used as a replacement for server-side validation of user input; it’s simple to bypass javascript validation of this sort by disabling javascript in your browser.

Flash 6 Actionscript Version:

I also wrote an implementation of textbox input masks for Flash 6. I haven’t tested or worked with this in newer versions of Flash, so I don’t know if it is still applicable. It was basically an actionscript 1.0 port/rewrite of the input mask class in the Java Swing framework. Hopefully this can be a starting point for using in your own Flash-based application:

Preview Flash 6 Input Mask | Download Actionscript Source

]]>
http://www.dioramadesign.net/blog/formatted-input-masks-for-textboxes/feed/
OSQL: Calling SQL From Command Line http://www.dioramadesign.net/blog/osql-calling-sql-from-command-line/ http://www.dioramadesign.net/blog/osql-calling-sql-from-command-line/#comments Thu, 02 Nov 2006 20:46:47 +0000 dmarks http://www.dioramadesign.net/blog/osql-calling-sql-from-command-line/ Here’s a quick little trick with the command line: consider a situation where you have a folder of .sql scripts that are used to update or create a new database. One way you can easily run all of these scripts against the database is by using the osql command line tool and a ‘for’ loop. I found this tip on some other site a while back, and unfortunately can’t remember where.

for %z in (”c:\DBInstallFolder\*.sql”) do osql -E -n -S MyDatabaseServerName -d MyDataBaseName -i “%z”

Pow. WIth one click of a bat file, you can run a whole folder of .sql scripts.

Oh, one other bonus dos command: when you generate sql install scripts from a database in Enterprise Manager (or Management Studio), it outputs them as ‘.prc’ files. I prefer the more generic ‘.sql’ extension. Change the file extension for all files in a folder like so:

ren *.prc *.sql

It’s worth it to get to know all the things you can do at the command line. It’s a lot of power at a keyboard jump away (windows-r, cmd, enter).

]]>
http://www.dioramadesign.net/blog/osql-calling-sql-from-command-line/feed/
TFS & MSBuild Part 3 (custom msbuild task) http://www.dioramadesign.net/blog/tfs-msbuild-part-3-custom-msbuild-task/ http://www.dioramadesign.net/blog/tfs-msbuild-part-3-custom-msbuild-task/#comments Thu, 02 Nov 2006 20:32:23 +0000 dmarks http://www.dioramadesign.net/blog/tfs-msbuild-part-3-custom-msbuild-task/ In my first post in this series I showed an msbuild script that would create a Team Foundation Server source control workspace, get latest to a particular folder, and build the Visual Studio solution in one click. However, because of the fact that TFS v.1 does not allow shared workspaces, the script wouldfail if another developer is already has a workspace mapped to that folder. I needed a way to delete all workspaces of a certain name.

There are two tf.exe (command line tool for TFS) commands that are useful here:

Lists all workspaces by any user of a certain name:
tf workspaces [workspacename]  /owner:*

Deletes a workspace for a specific user:
tf workspace /delete [workspacename];username /noprompt

There’s no way to automatically pipe information from the first command into the second, to automatically delete another user’s workspace. The way I worked around this was to create a custom MSBuild task that used the TFS C# API…

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace EmergeMD.MSBuild.Tasks
{
    /// <summary>
    /// Will delete all workspaces with a particular name. This way a workspace can be recreated and mapped to a different user in a build script. Use this with caution, since it will delete any shelved changes in this workspace. I’ve found this useful for a build server type configuration, where multiple developers can log in and update the code by hand by simply clicking an msbuild script. This workaround is tied to the fact that you can’t have shared workspaces.
     /// </summary>
    /// <example>
    ///  <UsingTask TaskName=”EmergeMD.MSBuild.Tasks.DeleteWorkspaceByNameTask”
    ///   AssemblyFile=”EmergeMD.MSBuild.Tasks.dll”/>
    /// <DeleteWorkspaceByNameTask WorkspaceName=”$(WorkspaceName)” TeamFoundationServerUrl=”$(ServerName)” ContinueOnError=”false” />
    /// </example>
    public class DeleteWorkspaceByNameTask : Task
    {
       
        private string _workspaceName;
        [Required]
        public string WorkspaceName
        {
            get { return _workspaceName; }
            set { _workspaceName = value; }
        }

        private string _teamFoundationServerUrl;
        [Required]
        public string TeamFoundationServerUrl
        {
            get { return _teamFoundationServerUrl; }
            set { _teamFoundationServerUrl = value; }
        }
      
        public override bool Execute()
        {
            Log.LogMessage(“Preparing to delete workspaces named “ + WorkspaceName);
            TeamFoundationServer server = TeamFoundationServerFactory.GetServer(TeamFoundationServerUrl);
            VersionControlServer serverVc = (VersionControlServer) server.GetService(typeof (VersionControlServer));
            try
            {
                Workspace[] workspaces = serverVc.QueryWorkspaces(WorkspaceName, null, null);
                foreach(Workspace workspace in workspaces)
                {
                    serverVc.DeleteWorkspace(workspace.Name, workspace.OwnerName);
                    Log.LogMessage(
                     string.Format(“Deleted workspace {0} mapped to {1}”, workspace.Name, workspace.OwnerName));
                }           
                return true;
            }
            catch (Exception e)
            {
                Log.LogError(“This task failed to delete workspace by filepath: “ + e.Message);
                return false;
            }
        }
    }
}

—-

Some useful references here:

]]>
http://www.dioramadesign.net/blog/tfs-msbuild-part-3-custom-msbuild-task/feed/
TFS & MSBuild part 2 (batch scripts) http://www.dioramadesign.net/blog/tfs-and-msbuild-part-2-batch-scripts/ http://www.dioramadesign.net/blog/tfs-and-msbuild-part-2-batch-scripts/#comments Tue, 31 Oct 2006 22:16:19 +0000 dmarks http://www.dioramadesign.net/blog/tfs-and-msbuild-part-2-batch-scripts/ This is an extension of my previous post on TFS and MSBuild. I have an msbuild script now that will do all of the grunt work of updating and compiling the code on a particular copy of our application. This can be easily called from the command line by overriding the properties defined inside the msbuild script. This way the same script can be reused for multiple applications.

Create a new text file called UpdateSite1.bat in the same folder as the msbuild script and enter something like the following:
@echo off
“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\msbuild.exe”
getlatestsource.msbuild /p:solutionroot=”c:\inetpub\Site1″
/p:Workspacename=Site1
@Pause

Now all you have to do to update your site is double clicking the .bat script. You could create different bat scripts for each site that needs to be updated.

]]>
http://www.dioramadesign.net/blog/tfs-and-msbuild-part-2-batch-scripts/feed/