MSBuild error when specifying PublishProfile for ASP.NET.Core project - asp.net-core

I have an ASP.NET Core 1.1.2 project targeting .NET Framework 4.6.2. I recently installed the latest version of Visual Studio (15.2 26430.16), and now I can't build the project using MSBuild version 15.1.1012.6693. The error occurs when using a PublishProfile.
The MSBuild command is:
msbuild D:\project\project.csproj /p:DeployOnBuild=true /p:PublishProfile=dist
And the error is:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(54,5):
error MSB4062: The "TransformWebConfig" task could not be loaded from the assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\..\..\tools\net46\\Microsoft.NET.Sdk.Publish.Tasks.dll.
Could not load file or assembly 'file:///C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk.Publish\tools\net46\Microsoft.NET.Sdk.Publish.Tasks.dll' or one of its dependencies.
An attempt was made to load a program with an incorrect format.
Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
[D:\project\project.csproj]
It works when not specifying a publish profile. However when publishing the project from Visual Studio, everything works as expected.

This is known bug of the tooling when building using the 64 bit MSBuild version (build server, command line). See this GitHub issue for details.
Since this issue has been fixed, you need to upgrade to the recently released 15.3.* versions of Visual Studio / MSBuild and probably also 2.0.0 version of the .NET Core SDK.

Related

Cannot add .NET 5 source code as a project reference

I've downloaded .NET 5 according to these instructions.
I can build the Antiforgery.slnf in Visual Studio with no errors.
I've added it as a project reference in a different solution with an ASP.NET Core 5 app.
But I'm getting these errors:
'..\..\..\..\..\..\aspnetcore\src\Antiforgery\src\Microsoft.AspNetCore.Antiforgery.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0'. AntiForgeryTest C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 1662
NU1201 Project Microsoft.AspNetCore.Antiforgery is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Project Microsoft.AspNetCore.Antiforgery supports: net6.0 (.NETCoreApp,Version=v6.0) AntiForgeryTest C:\Users\Davidk\source\repos\AntiForgeryTest\WebApplication2\AntiforgeryTest.csproj 1
It seems as though the current version of ASP.NET Core source code in Github is version net6.0, and the current version available in Visual Studio when creating a new project is only net5.0.
Do I need to get version net5.0 of the source code? I'd prefer to not have to do that since it's taken me hours just to get the .NET Core source code to build.
Any recommendations would be greatly appreciated.
I'm using Visual Studio Enterprise 2019 version 16.8.2 and here is the list of the installed SDKs:

how to solve Jenkins Build Failed Error :MSB4041

I have build this project as .Net standard project and i am facing this error issue in jenkins when I build this project:
C:\Users\tahab.jenkins\workspace\Mondaytest\Calculator\Calculator.csproj(1,1):
error MSB4041: The default XML namespace of the pr.oject must be the
MSBuild XML namespace. If the project is authored in the MSBuild 2003
format, please add
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the
element. If the project has been authored in the old 1.0 or
1.2 format, please convert it to MSBuild 2003 format
Make sure you use recent versions of the MSBuild task you use to build your project.
You need too use the Visual Studio 2017 (Build Tools, Full, ..) version of MSBuild - which is local to the Visual Studio installation - to build your project.
You may need to confiure the path to MSBuild, which would be - depending on the version installed on the build gent - similar to
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe
This error happens if you try to build new projects with older versions of Visual Studio / MSBuild or even with the old version of MSBuild that is part of .NET Framework.

VS 2017 .NetCore MSBuild custom task fails to load Newtonsoft.Json or other dependents

Environment
Visual Studio 2017
.NET Standard 2.0
.NET Framework 4.6.1
MyCustomTask.csproj contains custom MS build tasks and has multi targets to generate a separate dll for .NET Full and .NEt Core runtime.
There is a 3 level dependency on the Nuget Package "Microsoft.Extensions.Configuration.Json" which in turn has a dependency to Newtonsoft.Json package. as shown below.
The build is all fine and the Nuget package for MyCustomTask is generated successfully.
When I install my MyCustomTask Nuget package on target project then the custom build task is getting successfully executed on .NET Full runtime from Visual Studio 2017, but for .NET Core dotnet build command fail to execute the custom build task...
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral.
Even if I package all the dependent DLL and place it in same directory of the .netstandard custom task dll I keep getting same error.
Any thoughts how this can be resolved ?
Posting a solution here for those who are experiencing the same issue.
Credit to #MartinUllrich for posting the solution in a comment above. This saved me a lot of trouble.
https://github.com/AArnott/Nerdbank.MSBuildExtension
Ensure your custom task inherits from ContextIsolatedTask and copy the dependent assemblies to same directory as your task assembly.

Microsoft.Windows.UI.Xaml.CSharp.targets missing on CI server

Problem
I just committed the addition of a UWP application to source control. However, my CI server (Jenkins) didn't seem to like that:
error MSB4019: The imported project "C:\Program Files
(x86)\MSBuild\Microsoft\WindowsXaml\v14.0\Microsoft.Windows.UI.Xaml.CSharp.targets"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
This is stemming from the following UWP project file import:
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
On my local machine, I can see this file located at:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\WindowsXaml\v15.0\Microsoft.Windows.UI.Xaml.CSharp.Targets
Obviously, this is due to my local installation of Visual Studio 2017. However, the CI server does not have Visual Studio installed. I imagine this is why MSBuildExtensionsPath is resolving to the path mentioned in the error instead.
Dependencies
The following items are already installed on the CI server:
Visual Studio Build Tools 2017 (download)
Windows 10 SDK 10.0.15063.468 (download)
Xamarin.VisualStudio 4.5.0.476 (download)
.NET Framework 4.5.1-4.7 SDKs
I assumed these would include the necessary dependencies, but I cannot find Microsoft.Windows.UI.Xaml.CSharp.targets anywhere.
Workaround
Copying the directory on my local machine
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\WindowsXaml\v15.0
to the CI server at
C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v14.0
seems to resolve the error, but it feels very hacky.
Question
Is there a way to resolve this without installing Visual Studio 2017 on the CI server or copying/renaming files manually?
UPDATE 2018-01-12:
This is still an issue with the latest version of VSBT. The only thing to report is the error changed from v14 to v15. After copying the same directory over to v15, the error is gone again. However, another seemingly unrelated error shows up:
C:\Program Files (x86)\Microsoft Visual
Studio\2017\BuildTools\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1124,5):
error MSB3644: The reference assemblies for framework
".NETCore,Version=v5.0" were not found. To resolve this, install the
SDK or Targeting Pack for this framework version or retarget your
application to a version of the framework for which you have the SDK
or Targeting Pack installed. Note that assemblies will be resolved
from the Global Assembly Cache (GAC) and will be used in place of
reference assemblies. Therefore your assembly may not be correctly
targeted for the framework you intend.
UPDATE 2018-01-15
.NETCore,Version=v5.0 error resolved by copying the following directory from my local machine to the build server:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v5.0
While the UWP build is successful, no appxbundle is generated. This seems to indicate another dependency is missing from VSBT.

Cannot build .net core 1.1 with msbuild tools

I have a .net core class library I am attempting to build into a nugget package. however, on my build server (running team city), I am getting the following error:
C:\TeamCity\buildAgent\work\3c6531587491c25e\MySolution\MyProject.csproj
error MSB4019: The imported project "C:\Program Files (x86)\Microsoft
Visual
Studio\2017\BuildTools\MSBuild\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
When I look in this directory, I ma missing the "Microsoft.NET.Sdk" folder from my build server. where do I get these tools without having to install visual studio?
Thanks!
See the tracking issue on GitHub: https://github.com/Microsoft/msbuild/issues/1697
If you don't want to touch your build tools installation (by copying SDK files), you can also set the MSBuildSDKsPath environment variable to the location of the SDKs carried in the .net CLI (usually C:\Program Files\dotnet\sdk\1.0.0\Sdks\)