05 July 2011

Assembly Not Available in the Currently Targeted Framework

Oh, dreaded .NET 4.0 target framework issue... how I loathe thee.

Symptoms

When attempting to compile a .NET application, using Visual Studio 2010, the build fails, and the following warnings, or similar warnings, are issued:
The primary reference "System.Configuration.Install", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "System.Configuration.Install" or retarget your application to a framework version which contains "System.Configuration.Install". 
The primary reference "System.ServiceProcess", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "System.ServiceProcess" or retarget your application to a framework version which contains "System.ServiceProcess".
I found that even though the .NET Client profiles are supposed to be a subset of the full profile, many assemblies, including the two noted above, are not included in the "Add Reference" dialog.  These assemblies properly appear in the Client Profile.  It's as if the list has somehow been modified.  Now, to identify the culprit...


Cause

I was building a Windows service project, when the problem first occurred.  I ran PEX, to generate additional, parameterized tests, and the gates of programmer hell were flung open.  It appears that, PEX "fixed" something in Visual Studio 2010.

This issue is now plaguing other projects, as well.  All of these projects include one or both of the Install and ServiceProcess assemblies.  I already built unit tests via Test-Driven Design (TDD), including Moles detours, and everything worked fine.  The service project was set to .NET 4.0 Client Profile, and the test project is required to be set to the full 4.0 profile.

How to Fix the Problem*

In the Properties window pictured here, warning icons appear next to the System.Configuration.Install and System.ServiceProcess assemblies.  Also note the version number shows 0.0.0.0.  Ah-hah, a clue!  The Visual Studio project is unable to determine the framework version for the assembly.

To correct the problem, the project file must be edited:
  1. Unload the broken project
    1. Right-click the project item, in the Solution Explorer window
    2. Select Unload Project in the context menu
  2. Edit the project file
    1. Right-click the project item, in the Solution Explorer window
    2. Select Edit .csproj in the context menu
  3. Locate the broken reference(s):
    <Reference Include="System.Configuration.Install" />
    
  4. Add a SpecificVersion child node:
    <Reference Include="System.Configuration.Install">
        <SpecificVersion>True</SpecificVersion>
    </Reference>
  5. Save the edited project file
  6. Close the edited project file
  7. Reload the project
    1. Right-click the project file
    2. Select Reload Project in the context menu
Now, the references not longer display warnings, no warnings are issued upon build, and the project successfully builds.

*IMPORTANT NOTE:
This will not correct the problem with dynamically generated project files (*.csproj, *vbproj), such as those generated by the Microsoft Pex and Moles frameworks.

2 comments:

  1. Just for the record, i have exactly the same problem. I'm going to reinstall VS.

    ReplyDelete
  2. I tried re-installing VS 2010, to no avail. Please let me know if this corrects the issue!

    ReplyDelete

Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.