Visual Studio – Target Framework Not Supported



If you’ve ever authored SCOM management packs with Visual Studio Authoring Extensions then you’ve likely run into this issue. There’s no option in the prompt that that will fix this. To my knowledge, the product group hasn’t fixed this yet. There are two solutions that I know of:

  1. Add specific text to your .mpproj file.
  2. Download some .NET reference assemblies.


Option 1. Modify .mpproj

The first solution is to add the following text to your .mpproj file, then reload your solution:

  <PropertyGroup Label="Globals">
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
  </PropertyGroup>

Here’s an even better workaround for #1; create your own external tool in Visual Studio to fix the .mpproj file for you.

  • Command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Arguments: -NoProfile -ExecutionPolicy Bypass -File “C:\YOURPATH\Set-TargetFrameworkVersion.ps1” -SolutionDir $(SolutionDir)
  • Initial Directory: $(SolutionDir

Set-TargetFrameworkVersion.ps1

<#
.SYNOPSIS
    This PowerShell script is designed to add a TargetFrameworkVersion node to the Globals PropertyGroup of a Microsoft Project file (.mpproj) if it does not already exist. 
    If the PropertyGroup does not exist, it creates one and adds the TargetFrameworkVersion node with the value "v4.8".

.DESCRIPTION
    This PowerShell script is designed to add a TargetFrameworkVersion node to the Globals PropertyGroup of a Microsoft Project file (.mpproj) if it does not already exist. 
    If the PropertyGroup does not exist, it creates one and adds the TargetFrameworkVersion node with the value "v4.8".
.PARAMETER SolutionDir
    The path to the solution directory containing the Microsoft Project file (.mpproj). This parameter is mandatory and should be provided when running the script.

.EXAMPLE

.NOTES
    Designed to be launched from Visual Studio MP Project as an External Tool, passing the $(SolutionDir) as the argument.
    Set-TargetFrameworkVersion.ps1
#>

param (
    [Parameter(Mandatory=$true)]
    [string]$SolutionDir
)

$ScriptName = 'Set-TargetFrameworkVersion.ps1'
 <# TESTING
    $SolutionDir = "D:\SOURCE\Workspaces\WellsFargo\DataCollector"
#>
#region Functions
#####################################################
function LogIt($message,$Line) {
    "$($Line): $message" | Add-content -Path $logfile -Force
}
Function _LINE_ { 
    $MyInvocation.ScriptLineNumber 
 }

####################################################
function Add-TargetFrameworkIfMissing {
    param (
        [Parameter(Mandatory = $true)]
        [string]$ProjFile
    )

    [xml]$xml = Get-Content $ProjFile
    $nsMgr = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
    $ns = $xml.DocumentElement.NamespaceURI
    $nsMgr.AddNamespace("msb", $ns)

    # Look for the Globals PropertyGroup
    $globalsGroup = $xml.SelectSingleNode("//msb:PropertyGroup[@Label='Globals']", $nsMgr)

    if ($globalsGroup) {
        # Check for existing TargetFrameworkVersion = v4.8
        $tfv = $globalsGroup.SelectSingleNode("msb:TargetFrameworkVersion[text()='v4.8']", $nsMgr)

        if (-not $tfv) {
            $newNode = $xml.CreateElement("TargetFrameworkVersion", $ns)
            $newNode.InnerText = "v4.8"
            $globalsGroup.AppendChild($newNode) | Out-Null
            $xml.Save($ProjFile)
            Logit "TargetFrameworkVersion node added to existing Globals group in $ProjFile" $(_LINE_)
        } else {
            Logit "TargetFrameworkVersion already exists in Globals group. No changes made." $(_LINE_)
        }
    }
    else {
        # Create the entire PropertyGroup and add the node
        $propertyGroup = $xml.CreateElement("PropertyGroup", $ns)
        $propertyGroup.SetAttribute("Label", "Globals")

        $tfvElement = $xml.CreateElement("TargetFrameworkVersion", $ns)
        $tfvElement.InnerText = "v4.8"
        $propertyGroup.AppendChild($tfvElement) | Out-Null

        # Insert after the last PropertyGroup element
        $propertyGroups = $xml.SelectNodes("//msb:PropertyGroup", $nsMgr)
        if ($propertyGroups.Count -gt 0) {
            $lastPG = $propertyGroups[$propertyGroups.Count - 1]
            $lastPG.ParentNode.InsertAfter($propertyGroup, $lastPG) | Out-Null
        } else {
            # Fall back to appending to the root
            $xml.Project.AppendChild($propertyGroup) | Out-Null
        }
        $xml.Save($ProjFile)
        LogIt "Globals group with TargetFrameworkVersion added to $ProjFile" $($(_LINE_))
    }
}

#####################################################

$ProjFile = (Get-ChildItem -Path $SolutionDir -Recurse -Filter *.mpproj | Where-Object { $_.Name -notmatch '\s' } | Select-Object -First 1).FullName
$logfile = Join-Path $($SolutionDir) "$($ScriptName)_log.txt"

$msg = @"

'Set-TargetFrameworkVersion.ps1': Log file created at: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
`$SolutionDir: [$SolutionDir]"
"@

LogIt $msg $(_LINE_)

Set-Location $SolutionDir
Add-TargetFrameworkIfMissing -ProjFile $ProjFile
#Get-Content $logfile | Out-Default

If you project is currently loaded somehow, “unload” it. (Right click the project, not the solution, select Unload Project)

Run your new custom tool:


Option 2. Download .NET Resources

Here’s another solution (thanks to Daniel Creanga , Mohammed Farooq, and the VS PG):
Step 1: Go to this link:
https://www.nuget.org/packages/microsoft.netframework.referenceassemblies.net40

Step 2: Click on “Download package”

Step 3: Go to Downloads folder and rename file with .nupkg to .zip extension.
Now you can browse the .zip archive like a directory

Step 4: Follow the below path and copy all 233 files

This is the typical path from the Download folder location:

*The path where the individual archive files are located. Windows will let you browse the .zip. Otherwise you can also extract the .zip contents to the final destination folder shown in Step 5.

Step 5: Paste all copied files into the below path then close VS.

“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0”

Note: this must be done for each machine that has VSAE installed.

Step 6: Open solution again.

Success.

Leave a Reply

Your email address will not be published. Required fields are marked *