Category Archives: .net

Finally arrived: “Concurrent Programming on Windows”

Last June Joe Duffy announced that he had submitted the final manuscript for his (then upcoming) book Concurrent Programming on Windows to his publisher. Since then I had been anxiously waiting for it to arrive. Well, yesterday was the big day. The mailman delivered a big package and to my surprise it was Joe’s book. I had it on pre-order for some time and hadn’t expected it to be such a big book. OK, I knew it was going to have around 1000 pages, but still…

Ofcourse I haven’t been able to read the whole book yet, but I’ve scanned through it and I must say that I’m really impressed by it. It really covers many, if not all, aspects of concurrent programming on Windows. Joe has divided his book into four parts. In the first part he explains what concurrency is at a high level, which is great to get you started. In the second part he then thoroughly discusses fundamental platforms features, inner workings and API details. Here you’ll read everything about threads, thread pools, synchronization, asynchronous programming models and the lesser-known subject of fibers.

Actually, one of the chapters that immediately caught my eye was the one in which he discusses why fibers aren’t supported in the .NET Framework. As it turns out Microsoft planned to add fiber support to the CLR 2.0 so SQL Server 2005 could continue running in its "lightweight pooling" mode (a.k.a. fiber mode) when the CLR was hosted in-process. However they decided to completely remove it due to difficult bugs and schedule pressure. Joe gives some nice background info on this and tells you why it isn’t a good idea to try to use fibers from managed code yourself (i.e. by using P/Invoke).

The third part of the book then covers common patterns, best practices, algorithms and data structures that emerge while writing concurrent software. Here you’ll read about concurrency hazards like deadlocks and race conditions, data and task parallelism and performance and scalability. The final part of the book then discusses overlapped I/O, I/O cancellation and GUI threading models. All in all a very complete and thorough treatment of the subject of concurrency.

As a bonus Joe added two interesting appendices. The first one focusses on designing reusable libraries for concurrent .NET programs and the second one discusses the Parallel Extensions to .NET. Although this last appendix is very interesting and extensive I’m still a bit disappointed by it. I had expected the development of Parallel Extensions to go a bit faster and had hoped they would be released by now, so Joe’s book would include full coverage of this subject. Because of the current state of the Parallel Extensions (currently only some CTP’s have been released, although some of it is scheduled to appear in .NET Framework 4.0) the appendix might (well… most probably will) be outdated very soon. Considering the popularity and importance of the .NET Framework and the multi-core future I expect these Parallel Extensions to become very popular. So coverage of them has to become an essential part of this book. So expect a second edition of Concurrent Programming on Windows to arrive in the near future 😉

Well, to conclude things: Concurrent Programming on Windows is a definite must-have for anyone having an interest in concurrent programming on Windows and can be considered an instant classic. No other book out there has such an extensive coverage of the matter. Just get it!

Building a SPQuery ViewFields string

If you’re querying SharePoint content using a CAML query from code it’s a good habit to always populate the SPQuery instance’s ViewFields property. Otherwise the returned SPListItem instances might not contain any data for certain fields and throw an exception when you try to access those fields.

Specifying ViewFields involves creating a string of CAML FieldRef elements. For instance if we want our query to return items that contain data for the Title, Created and ID field we use this:

   1: <FieldRef Name='Title'/><FieldRef Name='Created'/><FieldRef Name='ID'/>

As you can see there’s some overhead of boilerplate markup involved. I’ve written a small piece of code that I always use to make my life a little easier. Today I happened to post this code in a reply I wrote on the MSDN forums and also decided to submit it as Community Content to the official SPQuery docs on MSDN. Then I thought I might as well share it with you here. So here it is:

   1: public static string BuildViewFieldsXml(params string[] fieldNames)
   2: {
   3:     const string TEMPLATE = @"<FieldRef Name='{0:S}'/>";
   4:     StringBuilder sb = new StringBuilder();
   5:     foreach (string fieldName in fieldNames)
   6:     {
   7:         sb.AppendFormat(TEMPLATE, fieldName);
   8:     }
   9:     return sb.ToString();
  10: }
  12: // Use it like this:
  13: SPQuery query = new SPQuery();
  14: query.ViewFields = BuildViewFieldsXml("Title", "Created", "ID");
  16: // Note that you can specify a variable amount of string parameters, i.e.
  17: query.ViewFields = BuildViewFieldsXml("Title", "Created", "ID", "Author", "Gender");

Yeah, you’re right. This piece of code isn’t exactly rocket science. But you might appreciate it anyway 🙂

.NET 3.5 Enhancements Training Kit

The Visual Studio & .NET Framework Evangelism team did it again. After their cool .NET 3.5 Training Kit they are now working on the follow up: the .NET 3.5 Enhancements Training Kit. It’s not final yet, but still very interesting!

Currently, the training kit contains six hands-on labs, made up of the following technologies:

  1. ADO.NET Data Services
  2. ADO.NET Entity Framework
  3. ASP.NET AJAX History
  4. ASP.NET Dynamic Data
  6. ASP.NET Silverlight controls

Read more about it on Jonathan Carter’s blog.

Beware of the SharePoint Memory Leaks!

Beware of the SharePoint Memory Leaks!

You might not know it, but when developing for SharePoint (either WSS 3.0 or MOSS 2007) it is very easy to cause memory leaks, which can ultimately thrash your SharePoint server’s performance. The reason for this is that even though your own code might be written using only managed code, the SharePoint API still uses unmanaged code in some places. These unmanaged resources have to be explicitly disposed of, especially considering the fact they are living inside an ASP.Net web application (SharePoint) and potentially can have a long lifetime.

The problem is that the SharePoint API doesn’t always make clear when exactly you have to dispose of objects explicitly. Sometimes it may look like you’re just inspecting a value of a property, while in the background a whole new object is instantiated, which holds valuable unmanaged resources that should be freed by the caller (you!). Other times you might find yourself disposing an object, only to find out you weren’t supposed to…

Way back in June 2006 Microsoft employees Scott Harris and Mike Ammerlaan released their article Best Practices: Using Disposable Windows SharePoint Services Objects on MSDN, which can be regarded as the mother of all articles on this subject. I think this article is often overlooked and should be considered essential reading material for every serious SharePoint developer out there. Also read their more recent article called Best Practices: Common Coding Issues When Using the SharePoint Object Model, which also handles topics like data and object caching and writing scalable code.

Recently two other Microsoft employees also wrote interesting pieces on this subject. Stefan Goßner did a piece called Dealing with Memory Pressure problems in MOSS/WSS, in which he explains what a "Memory Pressure Situation" is and he lists some common causes and solutions for it. The other interesting read came from Roger Lamb. His entry is called SharePoint 2007 and WSS 3.0 Dispose Patterns by Example and in it he shows some very clear code samples and patterns on how to properly handle SharePoint objects.

Very cool material, guys!

Return of SmartPart V1.3 released

Yesterday Jan Tielens released a new version of his famous SmartPart to the community. It is now at version 1.3 and here’s the changelog:

  • Added a setup wizard to install the Return of SmartPart.
  • Added sample user controls (including connectable user controls and AJAX user controls).
  • Added localization support for ASP.NET AJAX user controls.
  • Various minor bug fixes.
  • Nice 2-minute screencast on how to deploy and test the SmartPart.
  • 64 bit version available.

Great work, Jan! I also like the fact you’ve used Lars Fastrup‘s SharePoint Solution Installer for deploying SmartPart.

Visual Studio 2008 and SQL Server 2005 Business Intelligence Development Studio

As soon as Visual Studio 2008 went RTM I installed it and removed Visual Studio 2005 from my system. As you might know VS2008’s new multi-targeting feature can target .NET Framework 2.0, 3.0 and 3.5 and should be fully backwards compatible with VS2005, so in theory there shouldn’t be any need for keeping VS2005 around*.

However, today I encountered an unexpected situation where I did need VS2005. I tried to launch the SQL Server 2005 Business Intelligence Studio, which failed miserably… As you may know Business Intelligence Studio makes use of the VS2005 IDE (it uses either an existing “normal” Visual Studio installation or it installs the Visual Studio Premier Partner Edition). I knew this, but I had expected it to somehow automatically (or magically :-)) switch to the VS2008 IDE, which ofcourse it did not.

Unwilling to re-install SQL Server 2005 I searched the internet for a solution to this problem and stumbled on a thread on the MSDN forums discussing this exact same issue. It contains an interesting reply by Microsoft employee Dan Jones. There he explains a way to install the VS2005 files need without the need for re-installing SQL Server 2005. Here’s the trick:

  1. Go to the location for SQL Server setup and run .ToolsSetupvs_setup.exe. This will install the VS Shell.
  2. Repair the Business Intelligence Studio installation by running the following command from the command line from the .Tools directory (note: this should be typed on one line):
    start /wait setup.exe /qb REINSTALL=SQL_WarehouseDevWorkbench 

I hope this helps some of you facing this same issue.

* In practice I recommend you still keep VS2005 installed, especially when doing team development. If you open a VS2005 solution in VS2008, it will ask you to convert existing solution files to the new VS2008 format. Once converted, these updated solution files cannot be used in VS2005 anymore.I actually asked Scott Guthrie a question about this on his blog and even got a reply from him. Here’s what he had to say (for the original reply search the comments on this blog posting):

Hi Leon,

> Hey Scott, I noticed VS2008 wants to
> convert my existing VS2005 solutions.
> Why? The problem is that the converted
> solutions cannot be opened in VS2005 anymore,
> which some of my team members still use. I
> thought VS2008’s multi-targeting support didn’t
> require me to have VS2005 installed anymore,
> but now I still have to install them side-by-side.

VS 2008 allows you to target .NET 2.0 projects, but does make some changes to your solution file (that prevents it from being opened with VS 2005). What you can do, though, is maintain two solution files that point to the same projects and have them work with both VS 2005 and VS 2008.

Here are some blog posts that cover this more:…/122975.aspx…/multi-targeting-vs2005-and-vs2008-web-application-projects-a-gotcha.aspx…/enabling-team-development-with-vs2008-2005-mix.aspx

Hope this helps,


Yeah Scott, it did!

Cool e-book: The Web Part Infrastructure Uncovered

The Web Part Infrastructure Uncovered coverA while ago I did some proofreading for Teun Duynstee‘s e-book The Web Part Infrastructure Uncovered. I found it a very good read. In fact I think it is one of the most thorough books available on the subject, even though it only has 135 pages. This e-book has been available for some months now, but since I just recently started blogging I thought I might spend a blog entry to it. With SharePoint 2007 now using the .NET Framework 2.0 Web Part technology as one of its core technologies, knowledge about this framework has become more and more important.

Teun sells his book online through The price is €14.00 (ca. $17) for the printed book (+ shipping & handling) or €9.00 for the downloaded PDF (ca. $11) which is peanuts considering the book’s useful content.

I recommend this e-book to every .NET developer that’s working with or interested in Web Parts. You can check out some sample chapters here and here.