September 2005 - Posts

Language Quiz

What language is the following code snippet using?

var person = new

  {

    Name = "Steve",

    JobTitle = "National Assurance Orchestrator"

  };

A) JavaScript
B) C# 3.0
C) COBOL.NET with WinFS extensions.
D) None of the above

For hints, see Steve Maine’s excellent post “On JSON” (JavaScript Object Notation), and Abhinaba’s informative rant “C# 3.0: I do not like Anonymous types”.

posted by scott with 2 Comments

The Weeks Ahead

This week is Microsoft's MVP Summit in Seattle. I’m looking forward to seeing the city of Seattle, home of the National UFO Reporting Center. I hear Microsoft has a branch office or something nearby, too. I'm also looking forward to meeting many people I only know electronically, and cornering members of the MSBuild team. I want information, and not about aliens.

In the middle of October, I’ll be speaking at VSLive! 2005, in Orlando, Florida. The topic will be debugging with Visual Studio 2005, and I’m in the midst of preparing a pretty cool demo, complete with bugs. It’s not often that you want to start off a presentation with an exception.

The .NET Rocks! Road Show is making a couple stops in my neighborhood (Baltimore on October 18th, and D.C. on October 19th).

At the end of October is the Mid-Atlantic Security Code Camp. If you are anywhere near Reston, Virginia during the last weekend in October, you should stop in. The line-up of talks should be available soon.

This concludes post #300. Past performance is no guarantee of future results.

posted by scott with 2 Comments

Ted’s C# 3.0 Overview

Ted Neward has a great post analyzing new C# 3.0 features.

Ted refers to the language enhancements as mostly “syntactic sugar”. In other words - the syntax additions don’t change the expressiveness of C#, but merely add conveniences and shortcuts. We write less code while the C# compiler generates more IL behind the scenes.

Over the years I’ve been puzzled by the term ‘syntactic sugar’. People generally use the term in a derogatory sense, as in: “C++? Pffft. That’s just C with some syntactic sugar”.

Isn’t every language just syntactic sugar when compared to, say, CPU opcodes?

posted by scott with 3 Comments

Notes From the Atlas Hands On Lab

Rough and random thoughts while playing with the new Atlas bits.

Lab 1

Lab 1 creates a master page with <atlas:script> tags to ensure links to the Atlas framework .js files are sent to the client. I’d like to see this functionality in a base class so it isn’t copy and pasted everywhere, or simply injected auto-magically, perhaps when an attribute like <% @ Page AtlasEnabled=”true” %> appears.

Nifty master page tricks: the master page uses a ContentPlaceHolder inside the %lt;head%gt; tag, which makes it easy for content pages to override the contents of head. The master page does not define a %lt;form> tag, but leaves the content pages to define the tag (typically with action=”” since there are no postbacks involved).

Glitch: the setup puts a default.master and a default.aspx in the same directory, which can produce duplicate definitions for _default in the codegen directory. The errors appear in temporary files with scary names, and the errors come and go depending on how many files compile. It’s scenarios like this that make the system appear broken and one reason why people hate the project-less web project.

Cool: you can generate JavaScript proxies for a web service by browsing to the asmx file and appending /js (why not ?js to be consistent with ?wsdl?). Assuming you wire up a button’s onclick event, you can pull back and process results with relatively simple code. Lab 1 passes the contents of a textbox to a HelloWorld type service, which essentially echoes the input back with a timestamp.

function DoSearch()

{

  var SrchElem = document.getElementById("SearchKey");

  Samples.AspNet.HelloWorldService.HelloWorld(

      SrchElem.value, OnRequestComplete

    );

}

 

function OnRequestComplete(result)

{

  var RsltElem = document.getElementById("Results");

  RsltElem.innerHTML = result;

}

Lab 2

Lab 2 performs the same work as Lab 1, but with a declarative syntax. Instead of writing JavaScript you write a mess of declarative XML that only a machine can love. Excerpt:

<serviceMethod

    id="helloService"

    url="HelloWorldService.asmx"

    methodName="HelloWorld">

  <bindings>

    <binding dataContext="SearchKey" dataPath="text"

            property="parameters" propertyKey="query" />

  </bindings>

    <completed>

      <invokeMethod target="resultsBinding" method="evaluateIn" />

    </completed>

</serviceMethod>

Weep: Lab 2 didn’t work for me immediately and I had to stare at the XML and compare the above to what was in the sample. Not knowing what else to do I started stepping through the Atlas JavaScript. I was deep inside a JSON.serialize method when I realized it was building a payload like “query:foo”, where “foo” was the value I typed into the textbox and “query” was the name of the web service parameter – except I had named my parameter on the C# method differently.

I hope that none of the XML cruft will have to be hand written. I hope there will be some way to keep the XML and web method in sync, or decoupled. Lab 1 worked because the JavaScript proxy was custom generated from the code– I hope something along those lines will be available for the declarative service method bindings.

Lab 3

Lab 3 added auto-completion to the textbox control, which I found rather appealing in a scary way. When typing into the textbox the JavaScript will invoke a web service method and bring back a list of possible matches to display in a DHTML control.

Unfortunately, this lab started throwing a JavaScript exception, so once more I tried to step through the optimized-for-transport-no-whitespace JavaScript of the framework. I thought my head was going to explode. I gave up and posted on the asp.net forums, and  found that changing:

<div id="completionList" />

to

<div id="completionList"></div>

made everything work.

Sigh.

Lab 4

Lab 4 implements the same auto-completion textbox as Lab 3, but with a server side Atlas control instead of client side gunk.

<atlas:TextBox

  id="searchBox"

  runat="server"

  AutoCompletionServiceUrl="AutoCompleteService.asmx"  

  AutoCompletionServiceMethod="GetWordList"

/>

Sweet.

Lab 5

Lab 5 uses Atlas server-side controls to call a web method and populate a templated listview control. The word “bindings” appears a lot. Excerpt:

<atlas:Button runat="server" ID="fillButton" Text="Get URL List">

  <click>

    <actions>

      <atlas:invokeMethodAction

        target="dataSource1"

        method="select" />

    </actions>

  </click>

</atlas:Button>

The declarative syntax feels passive and the terminology is vague, but I suppose that is the nature of the beast and something I have to get over.

This concludes my notes from the Atlas hands on exercises. I’m certain you have not enjoyed them half as much as I have enjoyed typing them in.

posted by scott with 7 Comments

Concurrency

Concurrency is terribly difficult. Example: a primary cause of the massive North American blackout in 2003 was a software race condition. The application logged over three million production hours before the bug manifested itself.

Nevertheless, concurrency, here we come. Herb Sutter points out why with his article: “The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software”.

Mr. Sutter explains how future CPU improvements will no longer give applications a free performance boost - clock speed and execution optimizations are topping out. We need to increase the concurrency of our applications to take advantage of the next wave of CPU improvements (multiple cores).

Like Jeff Atwood, I don’t think concurrency is an area the typical application developer is ever going to deal with. Instead, the revolution will happen in the runtimes, the platforms, and the languages we use.

ASP.NET developers already use a runtime to write applications that process requests in parallel. Even a simple console mode program in the CLR has work happening concurrently. On the platform side, T-SQL developers have parallelism readily available with SQL Server’s Service Broker on the horizon.

While the platforms and runtimes are moving ahead, I’m not sure what is happening with languages. When Comega first appeared I thought the next version of C# would be taking a turn towards new concurrency abstractions, but I see no mention of these in the C# 3.0 specification.

As far as languages go, perhaps the LINQ / DLinq / XLinq features are enough to chew on and iron out first.

posted by scott with 2 Comments

Developer Toolbar For IE

The beta version of the IE Developer Toolbar is now available. The toolbar allows you to inspect the DOM, view class names and IDs, clear the browser cache, validate the markup, and more.

Other than a minor annoyance (the installation required a reboot), the toolbar looks great and works well. If you’ve ever used FireFox’s web developer extension you’ll know just how invaluable this tool can be.

The IEDevToolbar.dll the setup package deploys doesn’t appear to use managed code, though. Bummer.

posted by scott with 4 Comments

SiteMapProvider using SQL Server

If you are looking to build a custom ASP.NET 2.0 SiteMapProvider to read site navigation information from SQL Server, then look to Jeff Prosise’s “Wicked Code” column in the June 2005 issue of MSDN Magazine.
posted by scott with 0 Comments

Who Needs Press Releases?

Mike Gunderloy’s Daily Grind is a great source of information. In today’s grind Mike points to the unveiling of Microsoft Expression, both the official press release and the “readable” version from Somasegar’s blog entry.

Here is a one paragraph excerpt from the press release.

Rudder stressed that the new tools portfolio, paired with the Windows Presentation Foundation, a powerful platform-level presentation and display engine, will help drive down the cost and effort associated with building richer, more compelling and exciting applications with unique differentiated capabilities.

Yes, that’s one paragraph – one period, four commas, and the mind numbing phrase (“unique differentiated capabilities”). I have no idea what this paragraph is supposed to mean to me, but I get the feeling Eric Rudder is stressed, and I’ll never get a job writing press releases.

posted by scott with 2 Comments

Fun With the Exception Assistant

If you’ve spent time with VS 2005, you’ve probably met the Exception Assistant.

Reading the tips for a common exception is a bit like calling the cable company when your broadband connection is down.

“Sir, is the cable connected to the modem? Are there any green lights blinky-blinking?
Ok, sir.
Sir, I’m going to need you to power down your computer, your modem, your cell phone, and your microwave oven…. "

The “View detail” link is useful, as it lets you drill into all the properties of an exception.

The tips appear to be extensible. You can drop an XML file into the correct ExceptionAssistantContent directory (under Visual Studio 8\Common7\IDE) to provide helpful troubleshooting tips for your own exception types…

… or you can repurpose the existing DefaultContent.xml file to torture your co-workers…

posted by scott with 3 Comments

When It Rains It Pours

I’ve been catching up on all the news from the PDC today (I’m not there). It’s a bit of a shame all of this news has to stay bottled up and released all on one day, but I suppose it does make for an exciting event.

I’m stuck with real work for awhile, and won’t be able to jump into the excitement just yet. The following graph illustrates my backlog for technology research.

Here are some highlights of stuff to revisit some day.

Language integrated query, set, and transform operations (LINQ) are the kind of features you expect to see in a “fringe” language before reaching widespread acceptance, yet they appear to be headed for mainstream C# and VB.NET. Matt Warren has a nifty code sample posted, and Frans Brouma has an early reality check.

ASP.NET fans get their first look at Atlas today. ScottGu has a post “Atlas Unleashed” which describes some details and the development model for Atlas. It looks as if Microsoft will provide a JavaScript library to make the client side a bit easier, although I see a great deal of “getElementByID” calls in the samples, which isn’t the type of easy I wish for. Hopefully the team will keep blogging and getting information out, as some people seemed to feel they were mugged in a dark alley with the beta 2 release of ASP.NET 2.0.

Office 12 (screenshots here) will release with Vista. These two need to dance together better than the pairing of SQL Server and the CLR in terms of scheduling and binary dependencies. I’m a minimalist when it comes to the tools I use everyday, so toolbars with big icons have me immediately looking for the options menu. I’m more interested to hear about improvements in the areas I use everyday. Will Word let me post to a blog? Will Word do code formatting?

Microsoft gadgets bring Konfabulator type widgets to Vista. Some of the widgets are scary cool, like the ones that can run on the lid of a laptop.

Now back to the mundane…

posted by scott with 2 Comments

Page Inheritance in ASP.NET 2.0

Rick Strahl has a fantastic post: “Understanding Page Inheritance In ASP.NET 2.0”. Go read the post now.

Here are some ideas I thought of while reading.

The @ Reference, @ MasterType and @ PreviousPage directives are the only reliable mechanisms for referencing a type defined in another page (or user control, or master page) from inside a web form.

In Rick’s case he wanted to create a new web form (UploadItemPictureEx) derived from UploadItemPicture, where UploadItemPicture is defined as a Page derived class in the CodeFile for a second web form. In the new compilation model UploadItemPicture isn’t visible from the code inside the UploadItemPictureEx web form. This behavior is dramatically different from 1.1, as Rick clearly explains.

One solution is to define a base class for both web forms and stick the class in App_Code or a class library. Another solution is to use @ Reference, e.g:

If you place the above in UploadItemPictureEx.aspx, you’ll be able to use the UploadItemPicture type inside the CodeFile without any hassle.

The @ MasterType directive is useful when you want a strongly typed Master property for your page. I described @ MasterType in a previous post.

Likewise, the @ PreviousPage directive is useful during Server.Transfer and cross page postbacks. The runtime will give you a strongly typed PreviousPage property. I have some example of using @ PreviousPage in this article.

If you are dynamically loading user controls with LoadControl and don’t know which control you need to @ Register or @ Reference, I don’t see any choice but to define a base class for the control and keep the class in App_Code or in a class library referenced by the web project.

posted by scott with 6 Comments

Finding Web Services

Every so often I go looking for a public web service to do a job and end up at a UDDI Registry, like the one run by Microsoft. UDDI (Universal Description, Discovery, and Integration) came with the promise of providing searchable metadata to make web services easy to find and use from client software. A UDDI registry is a place where business and individuals can publish their metadata and become “discoverable”.

All the registries I find are terrible.

The registries contain out of date information, have confusing interfaces and categorizations, but worst of all they are not comprehensive. I have not found a registry yet that lists any of the latest and greatest web services for maps, photos, or blogs. Perhaps no one with a public web service cares about UDDI anymore, as UDDI always seemed destined for large, heterogeneous corporations to use inside the firewall.

I have, however, found two nice repositories for open web service APIs, thanks to Alex Barn’s blog:

Web 2.0 APIs
WSFinder Wiki

posted by scott with 0 Comments

Master Page Anti-pattern

Following a software design pattern is good. Falling into a software anti-pattern is bad.

The “master” in “master page” can be misleading. The term suggests the master page is somehow in control of everything happening during the lifecycle of a request. What actually happens is that the master page injects itself into the Controls collection of a web form and becomes a child control, (see figure 7 of Fritz Onion’s article on the topic). The master becomes the child.

Because the master page sounds like it commands all that happens under it’s domain, there is a tendency to lean towards a God object anti-pattern design. In other words, the master page class knows too much, and does too much. If you see a web form calling into a master page method to open a database connection or authenticate a user, then you are witnessing the master page anti-pattern in full glory.

Master pages are for UI layout, nothing more.

posted by scott with 6 Comments

Power Phases

This morning I was holding on to a precious idea and typing furiously into a remote desktop, when bleeeeeeep – the UPS units kick in and the network goes down. Power outage.

Mood: displeased.

After 15 minutes of rummaging, I found the phone book hiding between a book of salsa recipes and the classic “Joy Of Cooking” hardcover edition. The power was still off, so I started a manual search for the phone number of the power company.

Mood: irritated.

I found the number, but realized my landline didn’t work either. I picked up my cell phone and prepared to voice displeasure at the people who manage the local electrons.

Mood: aggravated.

Just before someone came on the line, I started to feel that perhaps I should be happy to have a roof over my head.

Mood: pensive.

I quietly reported the loss of power at my residence, and was told power would be back in an hour. I went for a stroll in the yard. Nice weather this time of year.

Mood: calm.

posted by scott with 1 Comments

Themes for Custom Controls in ASP.NET

Q: How do you skin custom controls and user controls in ASP.NET 2.0?

A: You can use an @ Register directive in the .skin file. Once the control is registered, skinning works just as you'd expect. For instance, the following control:

namespace OdeToCode.WebControls

{

    public class MyCalendar :

          System.Web.UI.WebControls.Calendar

    {

      // ...

    }

}

Can be skinned like so:

<%@ Register TagPrefix="OTC" namespace="OdeToCode.WebControls" %>

 

<OTC:MyCalendar runat="server" BackColor="White" BorderColor="#EFE6F7">

  <SelectedDayStyle BackColor="#41519A" ForeColor="White" />

  <SelectorStyle BackColor="#41519A" />

</OTC:MyCalendar>

The above Register directive assumes your control is defined in App_Code, otherwise you'll need to add the Assembly attribute to specify the assembly. It’s also important to note that a skin defined using <asp:Calendar> will not apply to the MyCalendar control, even though they have an inheritance relationship. The theme algorithm does an exact match on the control’s type.

posted by scott with 10 Comments

Colorful ASP.NET Themes

The Colorful Web Site Starter Kit on .NET Treats and Tricks is a nice piece of work (via Cindy). On the site you’ll find a find 8 ASP.NET 2.0 Themes you can download and use. The themes not only look nice but have a great design.

For instance, Erika used styles from a CSS file whenever possible. I’ve seen some grumbling that themes and skins in ASP.NET 2.0 are an evil plot to subvert web standards, when actually themes provide a great infrastructure to manage multiple sets of CSS files. Select a theme for a page and the runtime will automatically inject link tags for all of the stylesheets in the chosen theme directory.

There are some server controls that are just not easily styled: WebPartZone, Calendar, GridView, for instance. The colors and other visual properties of these controls are set with a skin file in each theme (see my ASP.NET 2.0 Themes article for more details on themes and skins).

There is also an Images subdirectory for each theme. Images can be tricky with themes, and tricky with master pages, and even trickier with themed pages using a master page.

You never want to use an absolute path to an image in a theme directory. Absolute paths break easily, and you’d have to change them with your own code if you want to switch themes. Always use relative paths. For instance, here is a snippet from the default.css in one of the themes.

td.headerbar

{

    background-image: url(Images/bar.jpg);

    text-align: right;

    height: 24px;

}

When the above reaches the client, the browser will be smart enough to request bar.jpg from the App_Themes/MSN_Blue/Images directory (when MSN_Blue is the selected theme).

What about images you want to vary by theme on a page, or on a master page? We know the runtime will do some URL rebasing for paths in a master page (read near the end of ASP.NET 2.0 Master Pages for more details), but that doesn’t help in rebasing an image’s src attribute from one theme to the next if you have hard coded the full path to an image file in a theme.

The easy solution is the SkinID attribute for server side controls. You can only have one default skin for a control, but multiple skins are available with a skin id. For instance, the colorful web site starter kit uses two image skins, identified by SkinID.

<asp:image runat="server" Imageurl="Images/logo.jpg" skinid="logo" />

<asp:image runat="server" Imageurl="Images/bullet.jpg" skinid="bullet" />

Notice the src uses relative paths. The logo on the master page uses the following:

<asp:Image ID="Image1" runat="server" skinid="logo"/>

With this approach you can switch the logo for a web site just by using a different theme. Sweet.

posted by scott with 8 Comments Rated Excellent [5 out of 5].