July 2006 Entries

Package Pages, User Controls, and Master Pages for Reuse

Graham Dyson has an interesting project named WAPUL: "WAPUL is a part of a project I've been developing for creating libraries of user controls, pages and master pages (although currently no support for a master page using another master page). Its aim is to avoid bodging or extra leg work when distributing (i.e. just one .dll file)." Sharing controls and pages is a feature many people are looking for. Graham has a clever solution using code, a post-build step, and the aspnet_merge tool.

Dependency Property Notes

I've run into dependency properties in both Windows Presentation Foundation and Windows Workflow Foundation. In trying to understand what they do, I ran across Drew Marsh's excellent introduction - "Avalon: Understanding DependencyObject and DependencyProperty". Drew's article has a WPF slant and I've mostly been seeing dependency properties from the workflow side. In WF, dependency properties enable three features: Attached properties Activity binding Meta properties The attached property story in WF is similar to the attached property story in WPF - a parent object can add additional state to the children it manages. A WPF grid will attach Row and Column properties to its...

Review of the Creative Zen Vision:M

I'm the owner of a new 30GB Zen Vision:M. The Vision:M is a portable media center and plays both MP3s and videos. The unit currently retails between $269 and $299. The 320x240 2.5'' LCD display looks remarkably good, even with the brightness setting at 50%. The Vision:M is a bit thicker and heavier compared to its iCompetitor, but has more color (262k versus 65k), and supports more file formats (DivX, XviD, MPEG 1/2/4, and WMV all play). I'll take function over form. The rechargeable Li-Ion battery lasted over 10 hours on a mix of video and audio (mostly audio)....

Narrative JavaScript

I stumbled across Narrative JavaScript this weekend. NJS uses a compiler to generate asynchronous JavaScript from procedural JavaScript. You write simple code with local variables and blocking operations. NJS takes the simple code and spits out JavaScript with all the callbacks and asynchronous goo. It's like programming with continuations. I wonder if Nikhil could add something like this to ScriptSharp.

Pickup Lines In .NET

Imagine if everyone in the world knew .NET programming. The world might be a bit strange, but at least these pickup lines might work. If I were a generic collection, I'd be strongly typed for you. You must be the latest version, because I've been checking you out. I have a small problem. I put an object in a Dictionary, but I lost the key! Can you come back to my place and help me look for it? A TypeConverter just returned my heart, and it's ready to assign to you. Let's turn off option strict and do some late night binding. So,...

It's Getting Crowded in the ThreadPool

It seems everybody loves the thread pool. The default scheduler in Windows Workflow uses the thread pool, as does ASP.NET and the BeginInvoke method of every delegate. There is only one pool per process, which seems a bit limiting with such a big party inside. Joe Duffy says there is a lot of work going into improving the ThreadPool in future versions of the base class library, I wonder if future versions will allow for some partitioning of work. . A couple people have asked me about custom thread pools. I do see the need in some scenarios, but writing...

On The Internet, Nobody Knows You’re Guilty

Information week has been covering the trial of a sysadmin accused of sabotaging 1,000 UBS Paine Webber servers. There is an abundance of circumstantial evidence in this case. Investigators found a printout of malicious code in the defendant’s bedroom, and the defendant bought an outstanding amount of stock puts that would benefit from a drop in UBS's price. Direct evidence, however, is from the ephemeral world of 1s and 0s: VPN logs, tape backups, and server audit trails. The defense called all these technologies into question. Wolfe also used his closing arguments to attempt to rebut defense theories. Chris Adams, Duronio's attorney,...

Dynamically Loading Controls in ASP.NET Is A Pain

Q: I'm using LoadControl to dynamically place objects into my web form. I've struggled with missing events and a host of other strange problems. I think Microsoft did a poor job designing this feature - it's a pain in my butt. A: If you are just rotating advertisements on a page, using LoadControl is simple. If you have a complex control with ViewState and events firing during postback, then LoadControl is difficult. It's not just LoadControl either - newing up a server side control and adding it to a Controls collection is also tricky. ASP.NET provides a thick layer of abstraction...

Dogfood

Q: Has Microsoft built and shipped any products with .NET? A: A quick look around turns up the following frameworks, applications, and utilities with at least some managed code inside. I'm sure there are others I don't know about. BizTalk Server, Team Foundation Server, SQL Server Reporting Services, SQL Server Integration Services , Microsoft Small Business Accouting, SharePoint Services, Content Management Server, MSDN Online, Media Center, Visual Studio 2005 What else?

PowerShell and AppPool Names

Yesterday we looked at stopping ASP.NET worker processes using PowerShell. However, I don't always want to stop the process by exe name or process ID, but by the name of the IIS application pool that the process is hosting. One way to do the job is with the iisapp.vbs script that ships with Windows Server. PS>iisapp.vbs W3WP.exe PID: 4420 AppPoolId: aspNet20 W3WP.exe PID: 2572 AppPoolId: DefaultAppPool How does iisapp.vbs figure this out? The script uses WMI to look at the command line that started w3wp.exe. There is a parameter (-ap) that specifies the application pool by name. I beat...

50 Ways to Kill a Process

I've been learning PowerShell and alternating between frustration and fascination. PowerShell is potent but comes with a learning curve. ScottGu recently posted about using tasklist and taskkill to stop an ASP.NET worker process, and I thought it might be a good experience to give this a whirl at the PS prompt. Cmdlets are the built-in commands of PowerShell. The get-process cmdlet is equivalent to tasklist, and can return all running processes: PS> get-process | where { $_.Name -eq "w3wp" } Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) ...

Community News

I'm excited to be part of By The Community, For The Community on the ASP.NET site. Go vote for the sample you'd like to see built, and an MVP will build it. If you have any suggestions for future samples, drop me a comment here. Here is how some of my compadres are spending their time: Sahil Malik set up shop on a new site: Winsmarts.Com. He's got his blog on and is going to run an ADO.NET boot camp this month.[1] Bill Ryan has been writing a book for MS Learning and making covert trips to the mothership in...

Impersonation and Application_Start in ASP.NET

Q: I want to query my database and cache some static data during the Application_Start event. SQL Server requires a trusted connection, and I'm using impersonation in web.config. <identity impersonate="true" userName="jacque" password="crackme"/> This works everywhere except Application_Start. The code in that method can't connect to the database. What's the dilly? A: The short answer is that impersonation isn't "on" during Application_Start, so SQL Server is seeing the worker process identity (NETWORK SERVICE or ASPNET) instead of Jacque's identity. Application_Start is an odd event. It's not really a request related event, but of course it won't fire until the first event arrives....

Lightning Visio: Step 3

Informative Diagrams for the Artistically Challenged Step 1 gave us raw material to mold. Step 2 molded material into a readable diagram. The final step, step 3, is going to add some aesthetic touches. What? I thought this was a pragmatic process! Yes, that’s true. But we can always take 2 minutes to add finishing touches. We want to make the diagram easier to read, and we can use some additional formatting to draw attention to significant concepts in the drawing. Here are three possible responses a developer might give when looking at our diagram: A: Ahhhhh, I understand, now. B: This is the...

Lightning Visio: Step 2

Fast Diagrams for the Artistically Challenged Step 1 produced the raw materials we need. Step 2 is molding the raw materials into a comprehensible diagram. There is a fine line between making a diagram comprehensible and adding little touches for the sake of aesthetics. As time ticks on, remember the ultimate objective is to communicate ideas to a team of software developers, not sell the diagram at an art exhibition. We need something more permanent than a whiteboard sketch, but that only requires a minimum amount of time. My first action is adjusting the font sizes in the diagram. The default...

Lightning Visio: Step 1

Effective Diagrams for the Artistically Challenged Over the years, I’ve had to whip up quite a few Visio diagrams. I used to spend a long time fiddling with different shapes and formatting, but not getting anything done. These days I have a three-step process that helps me crank out diagrams to communicate ideas. The diagrams won’t be the type you’ll include in product literature, nor will they win awards for innovation, but they are perfect to hang on the wall for a few weeks while a team is turning ideas into working software. Step 1: A Shape for Every Entity By the...

.NET Performance

Q: I need my application to perform better. Do you know any good resources on .NET performance? A: I know one great resource. The resource is the "Improving .NET Application Performance and Scalability" book from Microsoft's Patterns and Practices group (or whatever their name is this quarter). You can read the book online, download the PDF, or buy the 1152 page paperback. The book includes chapters dedicated to manage code performance, ASP.NET performance, and ADO.NET performance, among others. There guide also include How To articles and checklists you can use for reviews. It's an oldie but a goodie.

Build Failed With No Errors

MSBuild does a good job of reporting build errors, but MSBuild can only report errors that the underlying Tasks report. Custom tasks add a lot of variation to quality of the error messages. Case in point is the following message, which popped up as a deadline loomed. Build FAILED. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:05.85 The build failed, but there are no errors to report. Yup. It's strange messages like this that make me want to curse in German, but I don't know German, so I make the noises an angry Klingon might make after...

Getting Rid Of Inline Code in Global.asax (ASP.NET 2.0)

Q: I’m using the default web site project model in Visual Studio 2005. When I add a global.asax file to my web site, it wants me to use inline code. What do I do to get a code-behind file? A: First, create a class that derives from HttpApplication. You'll want to override the Init event in this class. This class can live in a separate class library project, or in the App_Code directory. The class might look something like this: using System;using System.Web;namespace OTC{    public class Global : HttpApplication    {        public override void Init()        {            BeginRequest += new EventHandler(Global_BeginRequest);            base.Init();        }        void Global_BeginRequest(object sender, EventArgs e)        {            throw new Exception("The...

Base Classes and Extending the @ Page Directive

Q: I'm having problems setting meta tags in my @ Page directive like in your article. My graphics designers don't understand code, but they can deal with page directives. It would be great to have this approach working. Do you have an example I can download? A: Download the example web project. The project contains four web forms, one class file, and one master page. The class file defines a base class for all web forms, and looks like the following. using System;using System.Web.UI;using System.Web.UI.HtmlControls;public class BasePage : Page{    protected override void OnInit(EventArgs e)    {                base.OnInit(e);        if (!String.IsNullOrEmpty(MetaKeywords) && Header != null)        {            HtmlMeta metaTag =...

Life Of A Static

Q: In my application, I am using some static fields to hold information. How do I make sure the garbage collector doesn't collect the objects referenced by the static fields? How long will the objects stay in memory? A: A static (C#) or shared (VB) field establishes a root reference. The garbage collector will only collect objects that can't be reached directly or indirectly from a root reference. The objects you reference from static fields are safe from garbage collection. Assuming a static field always references the same object, the object will be around for the duration of the application...

Master Page Performance

Q: I'm having a problem with performance in my ASP.NET master page. In the debugger I see the master page runs during each postback. How do we stop the master page from running its Load event each time? A: The MasterPage class derives from UserControl, which gives a hint as to why the master behaves this way. The master page becomes a control inside the page, and like every control in ASP.NET the master recreates itself on every postback (see Master Pages: Tips, Tricks, and Traps). You might be able to improve the situation using some common ASP.NET performance tips: Take...