Back when I used to have an MSDN license, or work for a small enough company where I administered the license, I always enjoyed receiving the various Microsoft posters. I had posters of all of the .Net System namespaces, ASCII posters, etc. A really useful one was the keyboard shortcut poster. Scott Guthrie just posted the new Visual Studio 2010 keyboard shortcuts posters. Using even a few of the most productive keys will make you day coding much more enjoyable. I encourage all programmers to pick up a copy of these posters and pick up a few new key combos.
Archive for the ‘Programming’ category
Visual Studio 2010 Keyboard Shortcuts
July 29th, 2010The new System.Collections.Concurrent Namespace
July 20th, 2010I just stumbled across the new System.Collections.Concurrent Namespace in .Net 4.0. I needed a thread safe dictionary and a coworker suggested I check out ConcurrentDictionary. Of course if you are attempting multiple operations against the dictionary you still need to lock or use some other type of mutex. But if all you are doing is reading a writing from multiple threads in single, atomic statements then this is the critter for you.
COM: Everytime I think I am out, it pulls me back in. (Silverlight + DLR)
May 24th, 2010Silverlight 4 introduces some nice functionality on top of the Out Of Browser (OOB) capabilities introduced in Silverlight 3. The application can now be granted a higher level of trust which allows it to operate outside the Silverlight sandbox file system, and to interact with COM. To test the capabilities of this new functionality I immediately tried to take it file system access far beyond the sandbox; I wanted to enumerate all of the drives on the client machine. I quickly ran into a wall. Silverlight does not expose access to drives through its IO libraries. Why? I read several answers to this question, none of which satisfied me, especially given the fact that the app can simply access these drives through COM!
Luckily .Net 4.0 / Silverlight 4.0 / C# 4.0 comes with the Dynamic Language Runtime (DLR). I had previous used the new dynamic pseudo-type that ties into the DLR to call Iron Python. I figured I would see if I could use this same technique to call into COM. Back in ancient times (circa 1990s) I called into COM all the time from VB and ASP. I knew the libraries were all there to do what I needed to do.
I was not surprised to find I wasn’t the first person down this path, and quickly encountered multiple articles and postings about working with the file system via COM this exact way. With the knowledge that it was possible I plowed forward.
To access COM from Silverlight one uses the AutomationFactory to instantiate a COM library. I wanted to use the good ol’ Scripting.FileSystemObject. In my test case I had a Silverlight app running locally OOB with elevated privileges. This Silverlight app has a single list box which will display the drive letters once I can get access. Via COM, the code looks like this:
dynamic fs = AutomationFactory.CreateObject("Scripting.FileSystemObject");
foreach (dynamic drive in fs.Drives)
{
filesListBox.Items.Add(drive.DriveLetter);
}
Its really that simple. The dynamic keyword and COM do all the heavy lifting. The most work I had to do was lookup the object model to use in the FSO, as no intellisense is available. The runtime does late binding onto these properties via the DLR. This did a nice end-route around the missing Silverlight IO libraries and provided me with a nice list of drives.
Excellent C# Threading Memory Model Article
April 28th, 2010
I just read an excellent article on the memory model in .Net and C# as it relates to threading, written my Microsoft’s Igor Ostrovsky. I think I had gathered most of these details over the years from various MSDN articles and CLR Via C# (1 & 2) by Jeffrey Richter. However, Ostrovsky provides a solid narration of how the model works. I was unaware that the local thread cache (conceptually) is all or nothing on reads and writes. There are a lot of misconceptions of how threads, locks, and volatile variables work and this article does a pretty good job of nailing it all down. There are some pieces of information that I had previously found slightly contradictory that really make sense when reading the article.
.Net 4.0 and VS 2010 GA
April 12th, 2010Microsoft has released the 4.0 version of the .Net Framework and Visual Studio 2010. The release includes several framework enhancements. The Dynamic Language Runtime has been added, which will act as a useful bridge to such languages as IronPython and IronRuby. The DLR optimizes dynamic languages at runtime for better performance with .Net. Also added is the dynamic pseudo-type, which allows run-time lookup of properties and methods on dynamic and compiled types. This will assist in the usage of dynamic code from compiled code, and also fits into some polymorphism scenarios. There are also a number of Base Core Library changes.
C# will be gaining some new language functionality with the .Net 4.0 release. C# now supports option parameters, named parameters and default parameter values to assist with those situations where overloads are cumbersome. C# 4.0 also supports a type of covariance and contravariance for collections.
The .Net 4.0 framework was delayed because of performance issues. I am glad they waited to get it right.
Silverlight 4.0 is scheduled to be released later this week. ASP.Net MVC 2.0 was released a couple of weeks ago, which takes advantages of a couple of new .Net 4.0 scenarios.
A Real Turing Machine (other than the one you are using to read this)
March 28th, 2010In 1937 Alan Turing published his famous paper on computability. In the paper he expanded on Kurt Gödel’s work on the Entscheidungsproblem (decision problem). The Entscheidungsproblem is the question, “Can any mathematical problem be solved?” Gödel proved that there are problems that cannot be solved, which was a big deal because most mathematicians assumed the opposite was true. Turing and separately another mathematician named Alonzo Church, proved that not only are there problems that cannot be solved, but that it is not possible to build a universal algorithm to determine which problems can or cannot be solved.
A side effect of Turing’s famous paper was The Turing Machine. In the paper Turing described a machine that ran on a tape, entering zeros and ones to compute problems. Generalized his description of this early computer is any computer in use today with a processor and memory (what you are using to read this).
A Turing Machine enthusiast name Mike Davey has built a real, working Turing machine. Here is the video:
Silverlight, REST, and Unhandled Exception
March 19th, 2010As part of the final push to finish up our brand-new, Silverlight 3, MVVM application I have been writing a lot of logging and instrumentation code. Any new platform is a risk, and while we believe we have taken a solid approach using MVVM and the unit testing it allows, we still don’t really know how this thing will perform in the field. With this in mind, I am trying to be rather liberal in the logging.
In Silverlight, one can write code to handle any unhandled exception in the application. This type of functionality exists in Winforms, WPF and ASP.Net, so I wasn’t terrible surprised to find it available. This is incredibly convenient. I can attack areas likely to cause exceptions such as web service calls with specific logging and exception handling code, and catch everything else with a few lines of general-purpose logging.
As soon as I implemented this catch all I immediately started getting log messages stating:
No XAML was found at the location ”.
at System.Windows.Navigation.PageResourceContentLoader.EndLoad(IAsyncResult asyncResult)
at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
Well, that was really helpful. And after some Google searches I soon realized that I wasn’t going to find a solution on the web. Well, I guess this is why I actually get paid to program. After some testing I discovered that the only time I saw this error (and it was just logging, it never bubbled up to the UI) it was when leaving the Silverlight app for another page in our web application.
Our app takes advantage of Silverlight’s Frame + Page navigation handling, which can map one URI to another. In practice this is basically REST. When the frame “hears” a new URI entered it converts the URI to an internal, component-based URI with the location of the Silverlight page to display. These mappings are configured during our bootstrap process which rides on top of Prism.
As it turns out, when the user (me) clicks on a link to leave the Silverlight app, the frame “hears” a new URI. The browser is navigating, and the frame tries to handle it. But it doesn’t have a mapping for the URI, which is curiously an empty string no matter where outside the Silverlight app one is navigating. The frame barf, and produces the above exception. But no one sees it because the app is closed within a split second of all this happening.
After poking and prodding with various ways of squashing this, I finally figured out that in the Navigating event of the Frame I can just set e.Cancel = true, which tells the frame to not navigate. My conditional logic simply checks to see if the URI is empty. The browser continues on with its business, and the Silverlight app ignores the navigation from that point on.
Hopefully this entry helps someone else doing REST in Silverlight.
ASP.Net MVC 2 Is GA
March 12th, 2010Scott Guthrie just posted on his blog that ASP.Net MVC 2 has been released. Looks like I am going to have to buy a new ASP.Net MVC book, and I didn’t even finish the last one!
Unit Testing and Code Change
January 29th, 2010This morning I made a small but important change to a chunk of code that our entire Silverlight application uses. This code, lets call it the Manager, facilitates every work flow through the application. The code change to the Manager amounts to deleting some unused code, changing some types, and some small tweaks to how new screens are called. On the surface the actual lines of code changed appeared to be very simple, but I was nervous I would break something since this is a fundamental piece of code. Everything uses the Manager.
Code can have subtle and dangerous dependencies. Even when one is the author (as I am in this case), one cannot be completely sure of the consequences of a code change in a sufficiently complex system. This is where unit tests come to the rescue.
I was able to execute my suite of unit tests, and after a successful report I ran through a few regressions with live data. Everything works. And I commited the code to our repository with a high level of confidence.
If something had broken I would have fixed it, and then written a unit test to check for the condition in the future.
Unit tests are a pain to write, but they sure do have a big payoff.
Code Snippet Editor
December 30th, 2009I stumbled across this great Visual Studio code snippet editor and thought I should share the link. I have already created a few snippets with it, and it my development time is benefiting from it.
The editor allows one to highlight existing code and export it into a snippet file. This is a great launch pad for snippet development. The editor runs in Visual Studio, and once a snippet is saved with a shortcut it is immediately available in the IDE. The editor also allows the editing of existing snippet files.
