How to use MSBuild with WPF project that references net standard projects - c#

We're creating a new Windows application with a WPF interface. For various reasons, we are attempting to make all of the supporting libraries run on net standard 2.0.
So the WPF project, Foo, references .NET Standard 2.0 projects from the same solution.
My problem is with getting the solution to build on the TeamCity build server.
msbuild gives an error when building the solution (works fine in Visual Studio 2017).
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(268,9): error MC1000: Unknown build error, 'Cannot resolve dependency to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' [Src\Foo\Foo.csproj]
If I use nuget to add NetStandard.Library to the Foo project, then I get a different error from msbuild (still works fine in visual studio).
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets(178,5): error : Your project.json doesn't have a runtimes section. You should add '"runtimes": { "win": { } }' to your project.json and then re-run NuGet restore. [Src\Foo\Foo.csproj]
There isn't a projects.json file.
It's running with 15.0 version of msbuild
External Program Failed: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MsBuild.exe (return code was 1)

Starting with a fresh build server solved the problem. I guess there were too many versions of msbuild and visual studio or SDKs or something on the old ones.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(268,9): error MC1000: Unknown build error, 'Cannot resolve dependency to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' [Src\Foo\Foo.csproj]
The error message indicates you are using MSBuild for .NET Framework 4.0 to attempt to compile your assembly.
You should use dotnet msbuild to compile your .NET Standard 2.0 library and do note you need to have the .NET Core 2.0 SDK installed on the build machine.
dotnet msbuild /p:Configuration=Release
A simple way to install the .NET Core 2.0 SDK remotely on a CI server is to execute the dotnet-install script.
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets(178,5): error : Your project.json doesn't have a runtimes section. You should add '"runtimes": { "win": { } }' to your project.json and then re-run NuGet restore. [Src\Foo\Foo.csproj]
This error indicates your build server has a preview version of the .NET Core SDK on your server that uses project.json format. You will need the .NET Core 2.0 SDK in order to build.

Related

How to run microsoft BotBuilderLocation C# sample using VisualStudioCode?

I downloaded project from https://github.com/microsoft/BotBuilder-Location , extracted and opened the CSHARP folder using VisualStudioCode. I tried to run the project from terminal using command
dotnet run --project BotBuilderLocation.sln
But I'm getting multiple errors as follows:
D:\InsuranceClaimSolution\BotBuilder-Location-master\CSharp\BotBuilderLocation.Sample\BotBuilderLocation.Sample.csproj(158,3):
error MSB4019: The imported project "C:\Program
Files\dotnet\sdk\3.1.416\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets"
was not found. Confirm that the expression in the Import declaration
"C:\Program
Files\dotnet\sdk\3.1.416\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets"
is correct, and that the file exists on disk.
C:\Program Files\dotnet\sdk\3.1.416\Microsoft.Common.CurrentVersion.targets(1177,5):
error MSB3644: The reference assemblies for .NETFramework,Version=v4.6
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
[D:\InsuranceClaimSolution\BotBuilder-Location-master\CSharp\BotBuilderLocation.Tests\BotBuilderLocation.Tests.csproj]
C:\Program Files\dotnet\sdk\3.1.416\Microsoft.Common.CurrentVersion.targets(1177,5):
error MSB3644: The reference assemblies for .NETFramework,Version=v4.6
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
[D:\InsuranceClaimSolution\BotBuilder-Location-master\CSharp\BotBuilderLocation\BotBuilderLocation.csproj]
The build failed. Fix the build errors and run again.
I'm new to dotnet and VisualStudioCode. I tried to search and fix these errors, but I'm not understanding how to fix this using VisualStudioCode. I've dotnet version 3.1.416. There are too many files and too many lines in this project, which are referring to a different dotnet version. Some target files are also not found. Now I'm confused from where to get these missing target files, what to do inorder to retarget this project to my current dotnet version.
Can someone please help?

C# MSBuild: Microsoft.CodeAnalysis.BuildTasks.Csc task could not be loaded

I wrote a small C#/WPF application that builds a list of solutions with MSBuild.
These solutions can easily be built with Visual Studio 2017.
However, if I build the same solutions with MSBuild, I get the following error:
The "Microsoft.CodeAnalysis.BuildTasks.Csc" task could not be loaded
from the assembly C:\Program Files (x86)\Microsoft Visual
Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll.
Could not load file or assembly 'Microsoft.Build.Utilities.Core,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or
one of its dependencies. The system cannot find the file specified.
Confirm that the 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.
What is striking here is that version 14.0.0.0 is being searched for, although Visual Studio 2017 and version 15.0 are being used.
To narrow down the problem, I build a simple C# console application that only outputs a Hello World string. But I get the same error message.
I have the following constellation:
Visual Studio 2017
NuGet packages: Microsoft.Build, Microsoft.Build.Framework, Microsoft.Build.Tasks.Core and Microsoft.Build.Utilities.Core version 15.9.20 and Microsoft.Net.Compiler version 2.10.0.
I read here that
The Microsoft.Net.Compilers package overrides (by setting properties
in your project file) the default version of Roslyn included with
MSBuild / Visual Studio.
A downgrade of the Microsoft.Net.Compiler to version 2.8.2 as suggested here does not change the bug.
How can I make sure that I build with the right version?

.NET 4.6.1 project referencing a .NET Standard 2.0 library - VSTS wont build

I am referencing a .NET Standard 2.0.0 project in a .NET 4.6.1 project.
Building the project locally works fine. Building it with a hosted agent on VSTS gives the following error:
The type 'Object' is defined in an assembly that is not referenced.
You must add a reference to assembly 'netstandard, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Adding the netstandard assembly in Web.config as described here, didnt work, same error:
<system.web> <compilation debug="true" targetFramework="4.6.1" > <assemblies> <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/> </assemblies> </compilation> <httpRuntime targetFramework="4.6.1" />
Also tried adding the NETStandard.Library.NETFramework Nuget package. Got alot of errors like this:
CSC : error CS1703: Multiple assemblies with equivalent identity have
been imported:
'D:\a\1\s\packages\NETStandard.Library.NETFramework.2.0.0-preview2-25405-01\build\net461\ref\System.Xml.XmlSerializer.dll' and 'C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETFramework\v4.6.1\Facades\System.Xml.XmlSerializer.dll'.
Remove one of the duplicate references.
Used the latest NETStandard.Library.NETFramework package on Nuget.org - 2.0.0-preview2-25405-01. The description of the package says:
This package is deprecated. To consume .NET Standard libraries from
.NET Framework in VS2017 15.3, you only need the .NET Core 2.0 SDK
installed. For VS 2015, you'll need NuGet client 3.6 or higher.
Also tried adding the ".NET Core Tool Installer" for v. 2.0.0 as the first build step. No luck.
Here is my current build configuration. How do I make it work for a .NET 4.6.1 project referencing a .NET Standard 2.0 library?
I found lots of information online about installing NuGet packages, adding .NET Core tooling, restore, and build steps - but actually in the end I solved the issue by changing one item in a few places.
You need to ensure your build consistently asks for the right Visual Studio / VSTest version.
I had a build with one phase that contained a build and a test step (as well as things like NuGet restore, publishing etc - but they don't rely on any version of Visual Studio).
At the phase level, you need to select the "Hosted VS2017" agent queue.
For the build step, you need to set the Visual Studio Version to "Visual Studio 2017".
For the test step, you need to open Advanced Execution Options and set the VSTest version to "Latest".

ASP .NET CORE could not find file or assembly with custom assembly

I've ran into a weird problem.
I basically have my own web-stack for .NET-core which I've built into a few .dlls, and I want to reference these from another ASP CORE-solution.
VS seems to find the assemblies, where I can navigate types etc.
I can also build the project without any issues, but when IIS then runs the server I get an internal server error stating:
FileNotFoundException: Could not load file or assembly 'myDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
The referenced DLLs are in the debug-folder, and the really weird thing is that if I create a new ASP Core project in the same solution as the web-stack, I can reference and use it without any problems.
Why is this happening only when running on a project outside the web-stack's solution, and what can I do to make it runnable everywhere?
The .NET Core Tooling in VS 2017 (< 15.3 preview) / .NET CLI < 2.0 doesn't fully support referencing assemblies on disk. You need to package the library up as a NuGet package or use a project reference ("same solution"). The technical reason is that all the required assemblies and versions are resolved during compilation and written to the .deps.json file. When loading arbitrary assemblies, this might fail because either the assembly or its dependencies cannot be found (or a conflict with each other).
for load the external dll in core project, needed to:
vs 2017 with version 15.3 or upper.
Microsoft.Extensions.DependencyModel NuGet package installed in your project.

What install files in location - Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5

Please bear with me on this, I will try to explain as clearly as I can.
I started a new project (class library) which targets 4.5 and not the client profile 4.5.
I added a reference "System.Runtime.Serialization" and the properties indicates it's in location "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Runtime.Serialization.dll"
On my pc it compiles fine with a build script using msbuild.
On the buildserver which uses team city - it complains
error CS0012: The type 'System.Object' is defined in an assembly that
is not referenced. You must add a reference to assembly
'System.Runtime, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'
The buildserver has the full .net framework installed - "dotnetfx45_full_x86_x64.exe"
The only time it will build is if I copy my version of "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5" to the target machine.
I have checked using this link and .net 4.5 is installed - http://myspworld.wordpress.com/2012/10/18/how-to-check-if-net-4-5-is-installed-on-a-server/
I am asking two question:
1. How does this get installed?
2. If it does find it in this location surely it should look in the GAC?
Will appreciate any help on this.
It's the Windows SDK:
The Windows SDK includes the reference assemblies, tools, headers, and libraries needed to build applications that target the .NET Framework 4.5.
(For 1.1 and 2.0, there was a separate .NET Framework SDK, but I can't find a link for any more recent versions - I don't think it's separately available)
For part 2, no. The GAC is not searched at compile time. It's only used at runtime.

Categories

Resources