Blog Moving

I’m moving this blog over to http://mikefourie.wordpress.com. The www.freetodev.com url should redirect there soon. I’m busy copying over some posts and formatting the new site. I’ll be leaving this site up for now but all new posts will be to the new blog.

Hoping to do some interesting blogs soon…

Mike

Posted in Uncategorized | Leave a comment

PowerCommands for Visual Studio 2010

The RTM for PowerCommands for Visual Studio 2010 has just shipped! I found this an invaluable tool in VS 2008 and it’s good to have it back.

Download

If you’ve not used it before, below is a summary of what is on offer in version 10.0:

Enable/Disable PowerCommands in Options dialog
This feature allows you to select which commands to enable in the Visual Studio IDE. Point to the Tools menu, then click Options. Expand the PowerCommands options, then click Commands. Check the commands you would like to enable.
Note: All power commands are initially defaulted Enabled.
Format document on save / Remove and Sort Usings on save
The Format document on save option formats the tabs, spaces, and so on of the document being saved. It is equivalent to pointing to the Edit menu, clicking Advanced, and then clicking Format Document. The Remove and sort usings option removes unused using statements and sorts the remaining using statements in the document being saved.
Note: The Remove and sort usings option is only available for C# documents.
Note: Format document on save and Remove and sort usings both are initially defaulted OFF.
Clear All Panes
This command clears all output panes. It can be executed from the button on the toolbar of the Output window.
Copy Path
This command copies the full path of the currently selected item to the clipboard. It can be executed by right-clicking one of these nodes in the Solution Explorer:
The solution node; A project node; Any project item node; Any folder.
Email CodeSnippet
To email the lines of text you select in the code editor, right-click anywhere in the editor and then click Email CodeSnippet.
Insert Guid Attribute
This command adds a Guid attribute to a selected class. From the code editor, right-click anywhere within the class definition, then click Insert Guid Attribute.
Show All Files
This command shows the hidden files in all projects displayed in the Solution Explorer when the solution node is selected. It enhances the Show All Files button, which normally shows only the hidden files in the selected project node.
Undo Close
This command reopens a closed document, returning the cursor to its last position. To reopen the most recently closed document, point to the Edit menu, then click Undo Close. Alternately, you can use the CtrlShiftZ shortcut.
To reopen any other recently closed document, point to the View menu, click Other Windows, and then click Undo Close Window. The Undo Close window appears, typically next to the Output window. Double-click any document in the list to reopen it.
Collapse Projects
This command collapses a project or projects in the Solution Explorer starting from the root selected node. Collapsing a project can increase the readability of the solution. This command can be executed from three different places: solution, solution folders and project nodes respectively.
Copy Class
This command copies a selected class entire content to the clipboard, renaming the class. This command is normally followed by a Paste Class command, which renames the class to avoid a compilation error. It can be executed from a single project item or a project item with dependent sub items.
Paste Class
This command pastes a class entire content from the clipboard, renaming the class to avoid a compilation error. This command is normally preceded by a Copy Class command. It can be executed from a project or folder node.
Copy References
This command copies a reference or set of references to the clipboard. It can be executed from the references node, a single reference node or set of reference nodes.
Paste References
This command pastes a reference or set of references from the clipboard. It can be executed from different places depending on the type of project. For CSharp projects it can be executed from the references node. For Visual Basic and Website projects it can be executed from the project node.
Copy As Project Reference
This command copies a project as a project reference to the clipboard. It can be executed from a project node.
Edit Project File
This command opens the MSBuild project file for a selected project inside Visual Studio. It combines the existing Unload Project and Edit Project commands.
Open Containing Folder
This command opens a Windows Explorer window pointing to the physical path of a selected item. It can be executed from a project item node
Open Command Prompt
This command opens a Visual Studio command prompt pointing to the physical path of a selected item. It can be executed from four different places: solution, project, folder and project item nodes respectively.
Unload Projects
This command unloads all projects in a solution. This can be useful in MSBuild scenarios when multiple projects are being edited. This command can be executed from the solution node.
Reload Projects
This command reloads all unloaded projects in a solution. It can be executed from the solution node.
Remove and Sort Usings
This command removes and sort using statements for all classes given a project. It is useful, for example, in removing or organizing the using statements generated by a wizard. This command can be executed from a solution node or a single project node.
Extract Constant
This command creates a constant definition statement for a selected text. Extracting a constant effectively names a literal value, which can improve readability. This command can be executed from the code editor by right-clicking selected text.
Clear Recent File List
This command clears the Visual Studio recent file list. The Clear Recent File List command brings up a Clear File dialog which allows any or all recent files to be selected.
Clear Recent Project List
This command clears the Visual Studio recent project list. The Clear Recent Project List command brings up a Clear File dialog which allows any or all recent projects to be selected.
Transform Templates
This command executes a custom tool with associated text templates items. It can be executed from a DSL project node or a DSL folder node.
Close All
This command closes all documents. It can be executed from a document tab”

Posted in Visual Studio | Leave a comment

MVP 2010 Award!

I received great news from Microsoft last week saying that I have been awarded a MVP 2010 Award!

I’d like to thank all those involved in my nomination and look forward to another successful year working with the MVP family.

Mike

Posted in Uncategorized | Leave a comment

WMWifiRouter

I recently went on a trip where another delegate showed off his flashy HTC Touch HD2 acting as a WIFI router. It’s a really neat feature and I was able to use it with my iPod Touch. I added that feature to my ‘must haves’ for my next phone, however I’m pleased to say that I can now do the same with my existing HTC Touch HD. All it takes is WMWifiRouter.

There is a free trial to use and if it works for you it’s just a handful of euro to buy.

Mike

Disclosure: I was provided with a free licence of WMWifiRouter. This does not affect my right to not recommend the product. I find it useful and hopefully you do to.

Posted in Stuff I Use | Leave a comment

Debugging Outlook 2010 Add-ins with Visual Studio 2010

I’m using the Office 2010 RC and VS 2010 RC1.

There is an odd change in behaviour when trying to step into an Outlook 2010 Add-in from VS2010. Using VS2008, I set Outlook as the external program to start, set breakpoints and hit F5. The breakpoints were hit as expected. I upgraded the project to VS2010 but when trying to debug, Outlook started without hitting the breakpoints. I could see the Add-in loading and it was visible after Outlook started.

I was lucky enough to get a tip from a colleague that if I set the Start Action to Start project rather then Start external program, then it worked fine. We’re still trying to determine if this is an intended change in behaviour or a bug. I’d call it a bug, but this is my first attempt at writing an Office Add-in, so I’ve not been on any aliases etc to see if this was ever mentioned.

This may affect other Office applications, but I’ve not had a chance to investigate it further.

[UPDATE: Answers from Misha here: http://blogs.msdn.com/mshneer/archive/2010/03/19/com-shim-wizards-for-vs-2010.aspx]

Mike

Posted in Visual Studio | 2 Comments

The MSBuild Team are Blogging

I met recently with the MSBuild team and was happy to hear that they now have a blogging schedule. From a team that has been fairly quiet on the blogging front for a long time, we now have a steady flow of great posts to read,

 

Keep an eye on the Visual Studio blog for more…

Mike

Posted in MSBuild | Leave a comment

Upgrading Code — Tackling Code Analysis in Visual Studio 2010 (Part 2)

Read Part 1 first. Please note that this is all base on a non-RTM build of Visual Studio 2010, so you may get different mileage out of the final product.

 

As mentioned in Part 1, the ‘CA1062: Validate arguments of public methods’ and ‘CA1305: Specify IFormatProvider’ errors are easy enough, let’s not spend any words on them, so we are left with 79 errors falling into the following three new rules:

  • CA2000: Dispose objects before losing scope (a new rule 62)
  • CA2100: Review SQL queries for security vulnerabilities (a new rule 2)
  • CA2202: Do not dispose objects multiple times (a new rule 15)

CA2000, which although a new rule, illustrates a basic best coding practice. We’ve been caught red handed, guilty, but it’s fantastic that we can now rely directly on Visual Studio to keep a helpful eye on this for us.

If you are new to .net read this, it basically means that you should write something like this

PerformanceCounter pc = new PerformanceCounter(this.CategoryName, this.CounterName, null, this.MachineName);
this.Value = pc.NextValue().ToString(CultureInfo.CurrentCulture);

like this instead

using (PerformanceCounter pc = new PerformanceCounter(this.CategoryName, this.CounterName, null, this.MachineName))
{
    this.Value = pc.NextValue().ToString(CultureInfo.CurrentCulture);
}

That’s an easy fix we just need to do around 60 times, but looking more closely at the code, there are violations where using statements are being used and writing this

DataTable dt = new DataTable { Locale = CultureInfo.CurrentCulture };
this.total = DecimalToSglDbl(Convert.ToDecimal(dt.Compute(this.Expression, string.Empty).ToString(), CultureInfo.CurrentCulture));

like this

using (DataTable dt = new DataTable { Locale = CultureInfo.CurrentCulture })
{
    this.total = DecimalToSglDbl(Convert.ToDecimal(dt.Compute(this.Expression, string.Empty).ToString(), CultureInfo.CurrentCulture));
}

does not resolve the violation. All occurrences of this seemed to have one thing in common. We are using object initializers. I managed to find a Connect issue in which the Code Analysis team advise us to set the properties within the using statement. So the above would become

using (DataTable dt = new DataTable())
{
    dt.Locale = CultureInfo.CurrentCulture;
    this.total = DecimalToSglDbl(Convert.ToDecimal(dt.Compute(this.Expression, string.Empty).ToString(), CultureInfo.CurrentCulture));
}

This is a bit irritating as using object initialization usually makes code easier to read.

We have two violations for CA2100: Review SQL queries for security vulnerabilities. For suppression, this rule says “It is safe to suppress a warning from this rule if the command text does not contain any user input.”. This task is open by design though to take absolutely anything and execute it. It’s in the hands of the user to control how it is used. There may be a case for a version of the task that can handle strict usage of parameters, so that will be added to the backlog.

CA2202 will typically fire on two occasions. Firstly, in code were you have multiple using statements where the outer using objects are used in the inner using constructors, instead of writing this

using (Stream requestStream = request.GetRequestStream())
using (StreamWriter writer = new StreamWriter(requestStream))
{
    writer.Write(post);
}

we should write

Stream requestStream = request.GetRequestStream();
using (StreamWriter writer = new StreamWriter(requestStream))
{
    writer.Write(post);
}

Secondly if you are calling object.Close() within a using statement, instead of writing this

using (FileStream fs = this.versionFile.Create())
{
    fs.Close();
}

we should write

using (FileStream fs = this.versionFile.Create())
{
}

It’s arguable whether you will or have ever seen Dispose throw on ‘ObjectDisposedExeption’, but I don’t see any harm in following the rule.

I wasn’t expecting such on long journey after upgrading, but after going through all the exceptions I think its been a very useful exercise and my confidence in Visual Studio as a whole has been increased. Good luck on your upgrades.

Mike

Posted in Visual Studio | Leave a comment

Team Foundation Server goes Mainstream — VS 2010 Pricing

To use TFS with Visual Studio 2008 / 2005, you had to get hold of a second installer (Team Explorer) which if I remember correctly was not free at first, then was made freely available. You install it and then you have to try and find the menu that has a new menu item which lets you connect to a Team Foundation Server… If you were dedicated, you would find your way!

After starting up a fresh install of VS 2010 Beta 2 what do you see?

 

That’s right, easy access to connect to your Team Foundation Server. So that’s nice, but you’re a small company and SourceSafe works for you and Team Foundation Server costs thousands of $$$$. Right?

No. First, if you are using SourceSafe, product support will reach end of life in mid 2011. After that you will get extended support if you pay (handsomely). Second, TFS 2010 has been priced to appeal to the smaller development teams too. Below is the suggested retail pricing in USD.

With 1-Year MSDN Subscription*
Product Buy Upgrade Buy Renew
Visual Studio 2010 Ultimate $11,899 $3,799
Visual Studio 2010 Premium $5,469 $2,299
Visual Studio 2010 Professional $799 $549 $1,199 $799
Visual Studio Test Professional 2010 $2,169 $899
Visual Studio Team Foundation Server 2010 $499 $399
Visual Studio Team Foundation Server 2010 CAL $499
Visual Studio Load Test Virtual User Pack 2010 (1000 Virtual Users) $4,499
* Subscription contents vary by purchased product.
Posted in Visual Studio | Leave a comment

Upgrading Code — Tackling Code Analysis in Visual Studio 2010 (Part 1)

Please note that this is all base on a non-RTM build of Visual Studio 2010, so you may get different mileage out of the final product.

Work has started on creating a version of the MSBuild Extension Pack targeted at .net 4.0 which comes with Visual Studio 2010. There are two big changes when it comes to VS2010, Rule Sets & new rules.

Version 3.5.5.0 of the Extension Pack compiles with zero Code Analysis violations in Visual Studio 2008. There are suppressions in the code, but a lot of effort has gone into understanding what has been flagged and writing compliant code. The 4.0.0.0 branch is based off the 3.5.5.0 code base, so I was eager to see what the Code Analysis built into VS2010 would report.

When the projects were upgraded to VS2010 projects, they each received a “Migrated rules for [ProjectName].ruleset” file. This makes sense if you have crafted a subset of rules to use, but we use them all on the Extension Pack, and want to take advantage of the new rules; so all these files were deleted and each project configured to use the ‘Microsoft All Rules’ rule set.

That’s the easy part. Now lets build and see how bad it is….

1367 errors. Ok, so I was expecting a few errors, but not that many! Either we’ve found a way better way to write code, or there are some over zealous new rules…

A quick scan of the results shows that there are a lot of

  • CA2204: Literals should be spelled correctly (a new rule)
  • CA1303: Do not pass literals as localized parameters (a new rule)

For CA2204, it’s picking up just about every parameter used in messages. We could add a CA dictionary, but I don’t think there will be much benefit. Let’s suppress the messages initially and look into disabling the rule or implementing the dictionary at a later stage.

For CA1303, it’s a Globalization issue. Given more time and resource Globalization might make it up the priority list, but right now its pretty near the bottom. This rule will be disabled, so let’s edit the ‘Microsoft All Rules’ rule set and create a shared MSBuild.ExtensionPack.ruleset file. This file has the CA1303 rule removed and all rules set to error rather than warn. All projects are updated to point to the new rule set file.

Rescanning the code brings us to 87 errors. So we have cleared the ‘noise’! We are a fair way from zero still with the 87 falling into the following rules:

  • CA1062: Validate arguments of public methods (a new rule, 4)
  • CA1305: Specify IFormatProvider (3)
  • CA2000: Dispose objects before losing scope (a new rule 63)
  • CA2100: Review SQL queries for security vulnerabilities (a new rule 2)
  • CA2202: Do not dispose objects multiple times (a new rule 15)

So if VS 2008 had CA1305, why didn’t it pick up the errors? Well the error detected is not that IFormatProvider has not been specified, but that the wrong provider has been specified. So that’s good news. Not only do we have new rules, but the old rules have been improved too.

The ‘CA1062: Validate arguments of public methods’ and ‘CA1305: Specify IFormatProvider’ errors are easy enough, but the other three are worth looking at in more detail in Part 2.

Posted in Visual Studio | Leave a comment

MSBuild Extension Pack — Next up…

Those who keep a close eye on the Source Code tab in CodePlex will notice that in addition to shipping version 3.5.5.0, versions 3.5.6.0 and 4.0.0.0 have also been branched.

At the time of writing this, all branches contain the same code. Version 3.5.5.0 forms the basis of future versions. There were a few tasks and fixes that didn’t make it into 3.5.5.0 and we’ll try get those included in the 3.5.6.0 release due within the next quarter.

The 4.0.0.0 branch should be treated as alpha code and is likely to undergo many changes, some of which may break compatibility, but full details will be published when the code starts to stabilise. The only timeline currently available for a stable 4.0.0.0 release is Q1 2010.

As always your feedback is appreciated.

Mike

Posted in MSBuild | Leave a comment