'AzurePipeline failing due to: The reference assemblies for .NETFramework,Version=v4.6.1 were not found

I have an Azure pipeline setup for my builds. I have been running into this issue recently and cannot figure out a way to fix this:

##[error]C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1220,5): Error MSB3644: The reference assemblies for .NETFramework,Version=v4.6.1 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks

About a week or so ago lots of our builds were failing, MS had changed something and we were getting this sort of thing:

[error]C:\Users\VssAdministrator\.nuget\packages\codegeneration.roslyn.buildtime\0.6.1\build\CodeGeneration.Roslyn.BuildTime.targets(73,5): Error CGR1001: CodeGeneration.Roslyn.Tool (dotnet-codegen) is not available, code generation won't run. Please check https://github.com/AArnott/CodeGeneration.Roslyn for usage instructions. 

However was able to solve this by explicitily adding a task to include the netcore2.1 sdk

    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '2.x'

Now this issues is fixed we are now posed with the error complaining it cannot find .NET Framework 4.6.1.

Anyways any idea whats going on, this is driving me banannas - any advice or insight greatly appreciated.



Solution 1:[1]

The .Net framework version 4.6.1 has been deprecated by Azure DevOps Microsoft-hosted agent. For now, there are two kinds of Microsoft-hosted agents:

windows-2019 OR windows-latest: .Net framework version 4.7.2 and 4.8 preinstalled. This is documented here.

windows-2022: .Net framework version 4.8 preinstalled. This is documented here.

That is, you need to use self-hosted agent to use .Net framework 4.6.1 in the pipeline.

Solution 2:[2]

From Agent pool - Change Agent Specification from Window-Latest to Window-2019 ,It seems MS has done some changes in default agent

Pipeline setting

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jane Ma-MSFT
Solution 2 JeremyW