New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

Update: Note there is currently a bug in this rule that prevents it from firing on .NET 2.0 SP2 and .NET 3.0 SP2 added APIs when targeting .NET 3.5. See BUG: Multi-targeting rule not firing on usages of 2.0 SP2 and 3.0 SP2 for more information.

Back in October of last year, Krzysztof Cwalina talked about the multi-targeting changes in Visual Studio 2008 and .NET 3.5. He mentioned the limitations in its design that could cause an application that explicitly targets .NET Framework 2.0 and .NET Framework 3.0 to accidently take a dependency on a new member or type that did not exist in the RTM version of that framework. As a workaround, he posted the source code of a Code Analysis/FxCop rule that could detect and fire on a member that took one of these dependencies.

A little while before he wrote that post, my team at the time, the Code Analysis team, was approached to see if we could ship a similar rule out of the box. Unfortunately, we ran out of time to get it included in Visual Studio 2008 RTM and FxCop Beta 2, however, I’m pleased to announce that we’ve added the rule, Use only API from targeted framework, to Visual Studio 2008 SP1 and FxCop 1.36 RTM release.

Note: The Visual Studio integration for this rule is only available in editions that support Code Analysis; Visual Studio Team Development Edition and Visual Studio Team Suite.

 

How this rule works

In Visual Studio, this rule reads the current targeted framework stored in the C# or Visual Basic project and fires on any usage of API that was not included in the RTM version of that framework. Whereas, in FxCop, the rule reads the targeted framework stored within the FxCop project or passed via the command-line.

The following table attempts to explain this:

 

When Target Framework (in Project properties or in FxCop project options/command-line) is set to:

Code Analysis/FxCop fires on usage of:

.NET Framework 2.0

.NET Framework 3.0

.NET Framework  3.5

.NET Framework 2.0

 

 

 

.NET Framework 2.0 SP1

 

.NET Framework 2.0 SP2

 

.NET Framework 3.0

 

 

.NET Framework 3.0 SP1

 

.NET Framework 3.0 SP2

 

.NET Framework 3.5

 

.NET Framework 3.5 SP1

MSBuild prevents a user from mixing these combinations in Visual Studio, so only FxCop can fire on this.
Client-only Framework subset is not supported.

For example, when a user selects .NET Framework 3.0 as their project’s target framework, Code Analysis and FxCop will fire on any usage of members and types that were introduced in .NET Framework 2.0 SP1 and SP2, .NET Framework 3.0 SP1 and SP2, .NET Framework 3.5 and .NET Framework 3.5 SP1. This is because on a fresh install of .NET Framework 3.0 (such as on Vista RTM), none of these service packs or frameworks are installed.  

Enabling in Visual Studio

By default, Visual Studio projects with the default Code Analysis settings will already have the rule enabled. To check this, do the following:

  1. In Solution Explorer, right-click on your project and choose Properties
  2. Choose the Code Analysis tab
  3. Expand the Portability node and ensure that Use only API from targeted framework is checked

Use only API from targeted framework

Once this rule has been enabled, you need to make sure that you targeting the framework that you plan to release your application on. To verify this, do the following: 

For C# projects:
  1. In Solution Explorer, right-click on your project and choose Properties
  2. Choose the Application tab
  3. From the Targeted Framework drop down, choose the framework that your application is targeting

Target Framework in C# project properties 

For Visual Basic projects:
  1. In Solution Explorer, right-click on your project and choose Properties
  2. Choose the Build tab and click Advanced Compile Options…
  3. From the Target framework (all configurations) drop down, choose the framework that your application is targeting

Target Framework in Visual Basic project properties

Once you’ve done the above, running Code Analysis (Analyze -> Run Code Analysis) should now fire on any service pack dependencies your project has taken on. For example, using DateTimeOffset in a project that is set to target .NET Framework 2.0, will cause the following warning to output.

'Use only API from targeted Framework' warning 

Enabling in FxCop

In contrast to Visual Studio, Use only API from targeted framework does not fire by default in FxCop. This is because FxCop does not have access to the project (.csproj, .vbproj) of the assembly that it is analyzing, and hence cannot automatically determine the Framework your application is targeting. Because of this, you need explicitly specify it; either via the FxCop project or via the command-line.

Enabling using an FxCop project

By default, FxCop projects will already have the rule enabled. To check this, do the following:

  1. Open your FxCop project in FxCop
  2. Choose the Rules tab
  3. Expand the Portability node and ensure that Use only API from targeted framework is checked

Use only API from target framewor

Once this rule has been enabled, it will not fire until you tell FxCop the framework you are targeting. To do this, do the following:

  1. Open your FxCop project in FxCop
  2. Choose Project -> Options and then choose the Spelling & Analysis tab
  3. From the Target Framework drop down, choose the framework you are targeting. Typically this would match the value specified in your C#, Visual Basic or C++ project’s properties.
  4. Click OK

Target Framework in FxCop

Note: You can make this the default for all new FxCop projects and for the command-line by clicking Defaults… after you open the Project Options dialog and then following the rest of the steps above.

Once you’ve done the above, running analysis (Project -> Analyze) should now fire on any service pack dependencies your application has taken on. For example, using DateTimeOffset in a assembly that is set to target .NET Framework 2.0, will cause the following error to output:

'Use only API from targeted framework' error  

Enabling via the command-line

While the preferred method to specify the target framework is by using an FxCop project as described above, you can also specify it explicitly via the command-line. The new /targetframeworkversion switch was added for this purpose. Its valid values are described below:

Argument

Description

/targetframeworkversion:2.0

Specifies .NET Framework 2.0

/targetframeworkversion:3.0

Specifies .NET Framework 3.0

/targetframeworkversion:3.5

Specifies .NET Framework 3.5

/targetframeworkversion:none

Disables the rule.

This is the default. However, it can useful to specify this argument if you want to override the associated setting stored in an FxCop project that is passed on the same command-line.

Specifying any of these arguments on a command-line will override the target framework setting stored in the FxCop project.

An example command-line would be the following:

FxCopCmd.exe /file:MyAssembly.dll /console /targetframeworkversion:2.0

Disabling the rule or targeting specific service packs

Use only API from targeted framework does not have support for targeting specific service packs, such as .NET Framework 2.0 SP1. If you want to deliberately take a dependency on a service pack, simply disable the rule by unchecking instead of checking the rule in the Enabling in Visual Studio and Enabling in FxCop sections.

Published Monday, August 25, 2008 7:00 AM by David Kean
Filed under: ,

Comments

Sunday, August 24, 2008 11:07 PM by Code Analysis Team Blog

# New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

I've just posted a new post over my blog about a new feature we added for Visual Studio 2008 SP1 and

Monday, August 25, 2008 8:04 AM by Dave's Box

# FxCop 1.36 Released!

After what has to be the longest beta period for a product (after Gmail) - we've finally released

Monday, August 25, 2008 11:13 AM by Bryan Hinton

# re: New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

Any plans to add Service Pack checking support to this in the future?  I suppose you would need the project files in Visual Studio to enable a project to target a patched version of the FW instead of the base version for this to work?  It is just kind of ironic that this rule ships with .NET 3.5 SP1 and if you run it on any project that leverages SP1's API changes or enhancements (like ASP.NET Routing) it will fire.

There are no plans to enable support for targeting specific service packs in Visual Studio 2008. We wanted to do it, but it turned out to involve a a lot of work, was risky and it would impacted an extremely large number of teams across Visual Studio. However, in saying that, I can assure you that this is something Visual Studio as a whole is thinking about in future versions. It's very unlikely that it will be left up to a Code Analysis/FxCop rule to enforce this - in future versions you will likely never to be able to get into the situation of taking a dependency on a service pack or Framework that you explictly didn't specify.
Tuesday, August 26, 2008 10:08 AM by Omar Villarreal's WebLog

# Visual Studio 2008 SP1 links

**Be sure to read the readme and release notes!! If you had installed the Beta code.msdn.microsoft.com/RemoveKB944899 VS2008...

Sunday, August 31, 2008 10:10 AM by Visual Studio Hacks

# Visual Studio Links #69

My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. Busy week this week so this is a quick all at once version. Greg Duncan posted a link to release announcement for XamlPadX 4.0 . Sasha

Tuesday, September 02, 2008 7:16 PM by Brad Abrams

# VS2008 Multitargeting Gets Even Better with SP1

One of my favorite features of VS2008 is the ability to target older versions of the .NET Framework.

Wednesday, September 03, 2008 9:45 AM by Антон Палёк

# Multi-targeting rule для Visual Studio 2008 SP1 и FxCop

Все знают о такой замечательной возможности как "Multi-Targeting", которая появилась в Visual

Saturday, September 06, 2008 8:12 AM by Andrew Smith

# re: New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

This is great however it seems that it doesn't check xaml/baml. So if I reference ContentStringFormat in code I get a warning/error but if I do it in xaml then I don't get a warning or error. Also, it seems like it doesn't verify assemblies like the Microsoft.Windows.Design.Extensibilty because it didn't catch when I overrode a member added in SP1.

Unfortunately, BAML does not yet have reusable reader - so FxCop/Code Analysis cannot analyze it. With regards to Microsoft.Windows.Design.Extensibility.dll, this assembly does not ship in the Framework - it is part for the WPF extensibility model for Visual Studio and Blend, hence why the rule doesn't know about it.
Sunday, September 07, 2008 5:54 PM by Blake Niemyjski

# FxCop 1.36 Released

After a very long time, an FxCop update has been released . Here is a list of all that has changed: 200

Sunday, September 07, 2008 7:16 PM by Andrew Smith

# re: New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

I see that there are xml files in the "Team Tools\Static Analysis Tools\FxCop\Repository\Compatibility" directory which seem to correspond with the version changes for each service pack. Is there a utility so we can generate/update the file for the dlls you skipped/missed?

Friday, September 12, 2008 2:45 AM by Joycode@Ab110.com

# VS2008中的多定向支持(Multi-targeting)在SP1中改进了

【原文地址】 VS2008 Multitargeting Gets Even Better with SP1 【原文发表日期】 02 September 08 09:53 我在VS2008中最喜爱的特性之一

Tuesday, September 23, 2008 10:28 AM by Scott Hanselman's Computer Zen

# Update on .NET Framework 3.5 SP1 and Windows Update

Tuesday, September 23, 2008 11:07 AM by Readed By Wrocław NUG members

# Update on .NET Framework 3.5 SP1 and Windows Update

The .NET Framework 3.5 SP1 included a bunch of new features, but as a Service Pack it also included a

Tuesday, September 23, 2008 11:24 AM by ASPInsiders

# Update on .NET Framework 3.5 SP1 and Windows Update

The .NET Framework 3.5 SP1 included a bunch of new features, but as a Service Pack it also included a

Wednesday, September 24, 2008 1:10 AM by Aurélien Norie

# Le multi-targeting de Visual Studio et les Services Packs

Bonjour, Aujourd'*** je voudrais parler de Visual Studio 2008 , du multi-targeting et des Services Packs

Tuesday, September 30, 2008 7:20 AM by Programming

# VS2008 Multitargeting Gets Even Better with SP1

One of my favorite features of VS2008 is the ability to target older versions of the .NET Framework.&;

Saturday, October 11, 2008 2:16 AM by Aurélien Norie

# Le multi-targeting de Visual Studio et les Services Packs

Bonjour, Aujourd'*** je voudrais parler de Visual Studio 2008 , du multi-targeting et des Services Packs

Saturday, October 11, 2008 1:01 PM by John Saunders

# re: New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

My issue with Service Pack support is that 3.5SP1 had enough new features that it might well have been named 3.6. We are, in fact, targeting "3.6" because we are targeting Entity Framework. I would have liked to set this rule to complain about the use of any post-"3.6" features, but I can't do that.

Of course, this issue will not be an actualy _problem_ until there are some post-"3.6" features to use!

Tuesday, October 14, 2008 8:41 AM by Eric and the .NET Framework

# Q&A: Multi targeting and Visual Studio 2008, how do I ensure I only use the available APIs?

This question has come up a couple of times in the last few weeks as developers move to Visual Studio

Tuesday, October 28, 2008 10:32 AM by mir hassan(pakistan)

# re: New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

.net multitargeting is great features for microsoft fan.

Friday, November 14, 2008 1:14 PM by Andrew Smith

# re: New for Visual Studio 2008 SP1 and FxCop 1.36 – Multi-targeting rule

It seems like there are bugs in this because it didn't complain when I referenced IEditableCollectionView which was added in 3.5 SP1. As a matter of fact, it seems like "C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\Repository\Compatibility\Desktop3.5SP1.xml" doesn't list ANY of the new WPF assembly members.

Andrew, thanks for the feedback. You are correct, there is a bug. The data for WPF 3.5 SP1 is included, it's just listed under 3.0 SP2. However, the 'Priority' attribute for this file and 2.0 SP2 is incorrect. I've forwarded this information on to my old team, in meantime, a workaround is to manually change these files; for Desktop3.0SP2.xml change the priority from '3020' to '3520' and for Desktop2.0SP2.xml change the priority from '3015' to '3515'.

Leave a Comment

(required) 
(required) 
(optional)
(required)