Archive for the 'Technical How-To's' Category

.NET Framework Debugging 1, Reflector 0

Reflector has and will continue to be one of the coolest, most useful development tools around. However, when it comes to demystifying .NET framework classes, there is a new game in town.

Here’s a glimpse into what a method in the System.URI .NET framework class looks like using Reflector.

Not bad…but here is what it looks from inside the Visual Studio debugger after configuring it to use the newly available Microsoft symbol server.

Besides actually allowing you to see the control flow in action, manipulate input values, and replay edge case scenarios, this new approach lets you see all of the original developer comments. The ones I’ve looked at so far have tended to add real value to the code reading experience rather than just pointing out the obvious.

Also, since it is derived from the actual source code files rather than a reverse-engineered approximation, the code can serve as a valuable learning tool by showing real-world examples of best practices rather than the 2-3 line fluff usually found in documentation.

Well, ok…the code fragments above are screaming for some “Extract Method” refactorings, but in general I’m guessing the Framework has some instructive examples of how to program given the vast amount of effort and review that goes into the framework classes.

If you haven’t given it a try yet, the setup process to get this working is fairly easy. It simply involves installing a Visual Studio 2008 QFE and changing a couple of debugging configuration options (see Shawn Burke’s post). Within minutes, I was able to F11 into WebClient.DownloadString(). For some reason this just magically worked the first time, but the next day I had to right click on System.dll in the modules window and select “Load Symbols” in order to get it to work.

Below you can see in the Modules window how it shows that the symbols are loaded for System.dll and you can see in the call stack window that I am inside a framework method. If you look in the directory on your local machine that you configure as the symbols location, you’ll also be able to see the downloaded PDB file after you do this step.

Reflector will still hold an esteemed place in my developer toolbox when I need to peek inside all non-framework dll’s or verify the contents of a deployed dll, but I doubt I will use it anymore to spelunk framework dll’s (at least the ones whose symbols have been released).

Update: Although the setup was seamless on my Vista box at home, I haven’t been able to get the QFE to install on my Windows XP laptop yet. I don’t know if this is a common problem or if my machine has some unusual quirk due to the hundreds of utilities I install on it.

Puzzling Issue Involving Reflection and MbUnit

I just sent the following email to the BCL (Base Class Library) Team Blog email address in hopes of getting some insight into a weird problem we’ve been having.

I was hoping you could give me some general guidance on how Type.GetProperties() determines the sequence of the PropertyInfo objects it returns.

I just ran across this remark on MSDN about the order that Type.GetProperties() returns PropertyInfo objects

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

We have parsing code that uses the 1.1 framework which is dependent on the order in which properties are returned. It fails intermittently when run as part of a suite of MbUnit tests. After seeing this msdn remark, I changed the code to remove the sequence dependency and the MbUnit intermittent failure problem disappeared. This led me to believe that properties were sometimes being returned in an order other than what we expected (the order they were declared in the code files).

Normally we are not allowed to make production changes this time of the year unless the issue is critical, so we are trying to determine the likelihood of this causing production issues. The problem is that we get contradictory results based on how we test this. If I wrap the method in a hundred thousand loop iteration, then I can’t reproduce the error in MbUnit. The properties always return in the order in which they are declared in the class file. However, if I run the whole suite of tests several times in succession, then I receive random errors that seem to derive from the sequence being returned in a different order.

Is there some internal algorithm that might trigger a different sequential order to be generated based on some unique condition that running the whole MbUnit suite of tests as opposed to running them in a large loop would cause (i.e. a certain number of threads or memory/cpu pressure)?

Any insight you could offer in this problem would be greatly appreciated.

Thank you,

Russell Ball

Does anyone else have any ideas?

** Update **

We no longer get these failures in MbUnit if we revert it to the 1.1 framework by changing the supported frameworks node in the config file as described here. I’m guessing they changed the algorithm in the 2.0 framework that is used to determine the sequence.

That still doesn’t explain why there is a difference between calling our code in a massive loop versus calling it as part of a suite of tests, but at least it puts our minds at ease with respect to the risk of intermittent failures occurring in our current production code.

Don’t Forget to Verigoogle

I knew that SQL Server 2005 had structured exception handling, but for some reason I assumed that you could only use it within CLR sprocs. Since I still haven’t actually heard of anyone using CLR sprocs for anything other than demos or sample projects (at least not without being pummeled by hoards of angry DBA’s), I mostly ignored this new feature until now.

Today I worked on a sproc that involved over a dozen DML statements (it was a utility sproc to handle security and setup steps for developers as part of our build process, so there is no need to chastise me about it being bad design to have so much logic in the database). The thought of copy-pasting dozens of tedious procedural error handling code snippets was so distasteful to me that I decided to do a quick verigoogle. Verigoogling is my freshly coined word for challenging one of my assumptions through google. I was delighted to discover that this new Try/Catch syntax is available to the masses to be used in plain vanilla sprocs. Instead of checking the @@error variable after each statement, we can now be lazy..er..I mean concise and consolidate all the error handling code into the catch block like we’re used to doing in good ole’ OOP code.

For those of you like me who haven’t been taking advantage of this new feature yet, here is some sample code that demonstrates the how easy it is to use. As for the rest of you, please restrain yourself for making fun of me and go verigoogle one of your own assumptions.

BEGIN TRY

      BEGIN TRANSACTION

          DELETE FROM table1

          DELETE FROM table2

      COMMIT

END TRY

BEGIN CATCH

    IF @@TRANCOUNT > 0
           ROLLBACK

     DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
     SELECT @ErrMsg = ERROR_MESSAGE(),
     @ErrSeverity = ERROR_SEVERITY()

     RAISERROR(@ErrMsg, @ErrSeverity, 1)

END CATCH

 

Resharper Jedi Envy

I’ve finally been bitten by the Resharper bug and now I want to be a Resharper Jedi when I grow up like Ilya Ryzhenkov on this video. According to the JetBrains .NET Tool Blog, a Resharper Jedi is someone who “can code hella fast with ReSharper and blow productivity levels off the chart”. I haven’t heard any claims about Resharper being able to help me Jedi-mind-trick my way out of traffic tickets, but there sure is a lot of reverent blogosphere speak about people like Ayende who can code at the speed of light during demos thanks to his mastery of this tool’s navigation, code generation, and refactoring keyboard short-cuts.

I just finished reading Joe White’s 31 days of R# and trying out almost every item in the Resharper menu that appears in Visual Studio after installing it. So far the force does not appear to be as strong in my family as I had hoped, but with luck I will make steady progress in the near future with the help of the Resharper Cheat Sheet, which I plan to post up on the wall of my new cube.

Below is a screen shot of one of my favorite features, the Usage option (Shift + Alt + F12 under VS Bindings). It shows all the references to the selected method or property, which is great when you are trying to determine potential side affects of changing legacy code (assuming you don’t already have a ton of unit tests to assuage your fears).

Confessions of an Aero Addict

When given a choice between style and substance in software, I usually opt for what I consider to be substantive qualities such as speed, functionality, usability, and cost. Since I never saw a sentence about Vista without the phrase ”eye candy”, I casually dismissed Microsoft’s long-awaited OS release as fluff when it first came out and was in no hurry to jump aboard the upgrade band-wagon.

Well despite my valiant effort at indifference, I am ashamed to admit that I have finally succumbed to the narcotic affects of the Aero and was compelled to upgrade both my work and home PCs to Vista last week. After less than a week, I now experience acute withdrawal symptoms every time I sign back onto an XP machine and am deprived of my glass translucency goodness. I could talk enthusiastically about some my favorite non-visual features in Vista, such as the pervasive search box (ctrl + esc) that I now use in lieu of Google Desktop to quickly find content and launch programs, but who am I kidding? My name is Russell Ball and I am an eye-candy junkie.

For those of you who are weak like me and are considering an upgrade in the near future, here are a few things to keep in mind.

At the Office

  • If you’re going to take advantage of an MSDN license and upgrade your OS well ahead of the crowd, make sure you’re nice to the IT Pro group. I got their blessing ahead of time by offering to give them a scouting report of any issues they will likely encounter when trying to upgrade the entire department. 
  • When encountering an application error in Vista, try restarting the app using the right-click ‘Run as Admin’ option. Several rather cryptic errors have miraculously disappeared after re-opening the app under elevated priviledges.
  • Go paperless. The IT Pros in my shop are understandably reluctant to risk upgrading drivers on the print server just for me, so I am currently remoting in to the dev box to print documents until I can get someone to punch a whole in the dev domain firewall and let me connect directly to the printer. 
  • Expect to have to spend a little time applying a new service packs and installing new versions of applications. PowerShell requires a new version and Visual Studio and SQL Management Studio both require service pack upgrades before they will properly work on Vista. 
  • Expect a few apps to not work. I had to turn off a visual affect in order to get the UltraMon title bar butttons to show up and shut down the Track-It agents on my machine to stop getting bombarded with error messages. Transcender won’t work at all, so I’m having to remote into the development server to take practice tests for my upcoming certification exam. Considering that I probably have a hundred apps installed on my machine, that’s really not that bad.

At Home

  • If you have a machine that is more than a few years, just let it go. I had a P4 that worked fine for remoting into the office and web browsing, but it was easier to just spend the $800 at my local computer store for a basic machine than fuss with tracking down incompatible parts as part of the upgrade process. After a quick $150 upgrade for a new video card that supported dual monitors, I found myself with a Windows Experience Score of 4.8 (not bad) and enough RAM, disk space, and processing cycles to power a small third world country. 
  • When your wife sees the bill for the new PC, cast an “Aero Translucent Glass” spell on her. If she isn’t fully distracted, try the alt-tab spinning windows trick. If that doesn’t work, then try defending yourself with one of the countless left-over non-Vista compliant parts that will soon litter the tech landscape.

See you at the Vista half-way house. 

The Open Source Side of Powershell: PSCX

One of my goals for the next six months was to contribute to an open source project and I think I’ve chosen one. I downloaded and installed the PowerShell Community Extensions (a.k.a. pscx) from codeplex a while ago, but I just now got around to really tinkering with the cmdLets and reviewing the source code and scripts. I have to say I’m impressed. Keith Hill and his team of volunteer developers did a first rate job of supplementing version one of Powershell with some really useful cmdlets. I can’t wait to dig into the source code some more and figure out how the snap-in model works.

Here are a few of my favorite features:

Provider:

  • GAC Provider - It’s a little slow when it first loads up, but it is very helpful to be able to easily navigate around in the GAC and do wildcard searches on version numbers.

CmdLets

  • Get/Stop-TerminalSession - It seems like every couple of weeks I have to kill a remote terminal services connection on one of our development servers because someone forgot to log off and pushed us over the two simultaneous users limit. This cmdlet is a lot quicker than loading up the administrative GUI and will help make the experience much less aggravating.
  • Write-Zip - This will be very useful addition to backup/archiving scripts. At home, I just did the following: dir “G:\Music\Radiohead\Pablo Honey” | write-zip
  • Get-FileVersionInfo - We just started auto-versioning all of our dll’s during our build process and set it up so we can match the version of a dll in production with a build directory on our build server. I’m sure we’ll be asked to create some automated way to help auditors figure out the version numbers and this will definitely help. 
  • Set-Writeable - This just removes the Read-Only flag from files, but it would have come in handy last year when we had to upgrade several projects to the 2.0 framework and move them from VSS to TFS. Removing a project from VSS is painful and one of the more tedious steps is clearing the read-only flag on all the source files. I remember wasting an hour of my life writing and debugging a VBScript because the right-click/recursive method never seemed to fully work.
  • Out-Clipboard - One of the things that still sucks about working in the shell environment is the editing capabilities, especially when it comes to copy and pasting. When I want to transfer the results of a command to an email or word doc, I simply pipe it to this cmdlet.
  • Send-Smtp - This will be great for the error-handling notification part of scripts. It would be even better if I could figure out how to populate the body parameter from the pipeline.

You can get a list of the 59 new cmdLets added when installing this snap-in by calling the function gcmpscx.

Using Powershell with SQL Server

Anything that can be done with ADO.NET is possible in PowerShell through a call to Assembly::Load and the New-Object cmdLet, but that doesn’t mean that the resulting code will seem very shell-like or practical. Two approaches to database access that definitely do seem compelling to me, however, are the sample SQL Provider recently demoed by the PowerShell team and Dan Sullivan’s PowerSMO script, which is really just a thin wrapper around the SQL Management Objects found in the Microsoft.SqlServer.Management.Smo namespace. The SQL Provider allows traditional command line navigation through the database using dir and cd and the SMO objects provide access to nearly any administrative function in SQL Server.

In order to use the PowerShell team’s SQL Provider, you’ll need to download SQLProvider.dll, install it in the GAC, and call the Add-PSSnapin cmdLet. Then you can treat the database just like the filesystem and cd or dir your way around with statements such as the following:


PS>cd SQL:

SQL:>cd MySQLServerName

SQL:>dir

master
tempdb
model
msdb
pubs
Northwind

SQL:>cd Northwind

SQL:>dir

name
—-
Categories
CustomerCustomerDemo
CustomerDemographics
.. etc…

SQL:>cd Employees

SQL:>Dir -filter {where EmployeeId=8}

EmployeeID      : 8
LastName        : Callahan
…etc…

Unfortunately, this provider is only a partially implemented sample and I quickly lost interest when I figured out that I couldn’t easily or naturally pipe the results to the where, select, and sort cmdLets. I did some quick searching to see if someone else had taken the next step and fully implemented a sql provider but I didn’t come across anything. However, I’m sure it won’t be long before somebody creates a production-ready version. The PS team did include the code for their sample provider, so perhaps I’ll take a look at it myself to see how hard it is.

To run the PowerSMO examples found on Dan Sullivan’s blog, you simply need to run a powershell script or place it in your profile so that it is run automatically when you start the shell. The script preloads some required dll’s and provides a factory function for retrieving some of the key objects, so retrieving a server object feels more like a cmdLet experience.

$server = get-SMO_Server mySQLServerName

However, since he warns that his script is for instructional purposes only, you might also just want to load the server object directly: 

[System.Reflection.Assembly]::Load(”Microsoft.SqlServer.Smo,Culture=Neutral,Version=9.0.242.0,PublicKeyToken=89845dcd8080cc91″) | out-null

$server = new-object “Microsoft.SqlServer.Management.Smo.Server” myServer

Then you can grab a database object and do interesting things like finding which tables and indexes in your database are taking up the most space.

$pubs = $s.databases | ?{$_.name -eq “pubs”}

$pubs.tables | ?{$_.name -notlike “sys*”} | select name, dataspaceused,indexspaceused,@{name=”Total Space”;expression={$_.dataspaceused + $_.indexspaceused}} | sort dataspaceused -desc

…or finding out the firve widest tables in the database as potential candidates for normalization and save it to an html file.

 $pubs.tables | ?{$_.name -notlike “sys*”} |select name, @{name=”Columns”;expression={$_.columns.count}} -first 5 | sort columns -desc | convertto-html > C:\wideTables.html

Again, you can do almost any of these functions through some other way (system sproc, sql function, utility), but what is significant is the capabilities that powerShell gives you to be able to manipulate the resulting data once you have it (sort, group, filter, format, etc.).

Disk Space Management with Powershell

The more I work with powershell, the more I start seeing everything that lives on the computer as part of a giant database that I can query. When used in conjunction with the pipeline and get-member -type property to discover the available properties on an object, the where-object, sort-object, select-object, and group-object cmdlets can be strung together to answer some very sophisticated questions. We often seem to run out of space on our development sql servers, so one tedious question that I am often forced to answer manually or with a utility such as windirstat is which sql backups are taking the most space so we can delete unused ones and free up space. This question can be answered easily and quickly in one line from powershell and then exported to text, csv, or html so that it can be sent to anyone else that needs to assist in the decision making process. In fact, with an extra line of code you could easily load up the .NET SMTPClient class in system.net.mail and email the output without leaving the command line. Here is the powershell one-liner that I used to find the largest 10 backups on a development server and save the output to a text file:

Dir \\devsql\sqlbackups *.bak -rec | sort length -desc | select name,directory, @{name=”Size in GB”;Expression={[System.Math]::Round($_.length/1073741824,1)}},CreationTime -first 10 | ft -auto > C:\LargeBackups.txt

This produces output that looks something like this:

Name                                      Directory                   Size-GB   CreationTime 
—-                                           ———                          ——–       ———— 
LendingHistory.bak              \\devsql\sqlbackups\Dev03       7.5     0/27/2006
Lending0702.bak                 \\devsql\sqlbackups\Dev01       6.2     06/29/2007
LendingPostEOD0628.bak   \\devsql\sqlbackups\Dev01       6.2     06/29/2007
LendingSOD0628.bak          \\devsql\sqlbackups\Dev01      6.1      06/12/2007
LendingSOD0627.bak          \\devsql\sqlbackups\Dev01      6.1      06/29/2007
Lending.bak                        \\devsql\sqlbackups\Dev02      6.1      11/27/2006
Investments.bak                  \\devsql\sqlbackups\Dev02      6.1      05/23/2007
InvestmentsHistory.bak        \\devsql\sqlbackups\Dev02     6 1       1/27/2006
Warehouse.bak                   \\devsql\sqlbackups\Dev01     5.9        04/25/2007
Lending.bak                        \\devsql\sqlbackups\Dev01     5.9        10/27/2006

How To Log On To A Web App With One Keystroke Using Powershell

I’m doing some work now on an internal web application where I had to logon with an external test account before I could do anything. That got old fast, so I created a powershell script file to automatically launch IE, input the username and password, and click the submit button. I originally tried to do this with WatiN, which provides a much easier interface for controlling the browser than the COM object InternetExplorer.Application, but I couldn’t get by the security exceptions explained in this Scott Hanselman post. I then added a keystroke shortcut in WinKey to execute this ps1 file via the hstart utility so that no command window would flash on the screen before the browser window could load. It’s probably not the cleanest solution, but it has been a huge time and annoyance saver.

Here’s the winkey info

command: <>\hstart.exe
parameters: /nowindow “C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -command “”&”  ‘C:\myScriptsDirectory\launchWebApp.ps1′”"

…And the powershell script

$psie = new-object -com InternetExplorer.Application
$psie.Navigate2(”http://www.someWebApp/”)
$psie.visible=1
($psie.document.getElementsByTagName(”input”) | where { $_.Name -eq “UserID”}).value = “userID”
($psie.document.getElementsByTagName(”input”) | where { $_.Name -eq “Password”}).value = “password”
($psie.Document.GetElementsByTagname(”input”) | where { $_.Value -eq “Login”}).click()

 

A Powershell Inspired Prank

Here is a good joke to play on one of your developer buddies if they have powershell and SQL Server installed on their local machines and you have sa rights on the SQL Server for whatever reason (former debugging help, open environment between developers, blank sa password, or mad hacker skills on your part). Wait until they are busy typing and execute the following query in Query Analyzer (or using sqlcmd.exe if you want to be a purist) against their SQL Server. You may want to lock down your own machine before trying this in order to avoid certain retribution…:-)

master..xp_cmdshell “powershell.exe (new-object -com ‘SAPI.spvoice’).speak(’Ouch. You are typing too hard’)”

This is a little more difficult with SQL 2005 because remote connections and xp_cmdshell are both disabled by default for obvious security reasons. You can enable remote connections by adding exceptions to the windows firewall for SQLServer and SQL Browser as described in the following KB article. You can enable xp_cmdshell in SQL 2005 with the following script:

EXECUTE sp_configure ’show advanced options’, 1
RECONFIGURE WITH OVERRIDE
GO
EXECUTE sp_configure ‘xp_cmdshell’,
‘1′
RECONFIGURE WITH OVERRIDE
GO
EXECUTE sp_configure ’show advanced options’, 0
RECONFIGURE WITH OVERRIDE
GO

If you’re really itching to do this prank but they don’t have Powershell or SQL Server installed, there are still lots of options. All you really need is a way to shell out a command against someone else’s computer since the underlying voice capabilities come from a COM component, which you can call from vbs script. Have fun…

Next Page »