How to build an unorthodox project structure with dotnet cli - .net-core

I have been trying to build the following project structure with dotnet cli on netcoreapp3.1:
project_root:
project.sln
projecct1
--------project1.csproj
project2
--------project2.csproj
third_party
--------third_party1
------------third_party1.csproj
The third party project is referenced in one of the projects and it is in its self a netsandard2.0 assembly
running dotnet build produces the following error:
error NETSDK1005: Assets file '...\third-party\third_party1\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v3.1'. Ensure that restore has run and that you have included 'netcoreapp3.1' in the TargetFrameworks for your project.
Building the third party on its own works fine.
I realize that the error stems from the structure of the project. Any ideas how I can build the project with getting this error?

Ok so answering my own question. The problem was not the structure. When I built the project in command line I use dotnet build -c Release -f netcoreapp3.1
Since the project is a mix of netcoreapp3.1 and standard2.0 assemblies the standard2.0 assemblies failed to compile. If your framework is set in the csproj file not need for -f apparently.

Related

AssemblyInfo ingrored when building from GitLab-Runner

On a Windows machine there is a GitLab-Runner run from a domain user with admin rights. When I log as this user and call dotnet build -c release to build an ASP.NET Core app, the dll has all the information from the AssemblyInfo.cs file. When I do the same as part of a CI job, the produced dll is missing all this information (for example the version number).
The AssemblyInfo.cs file is not part of the repository, instead, it is produced by a prebuild event (using gitWCRev.exe tool). However after running some tests I can see that the AssemblyInfo.cs is actually generated when the job is run by the runner.
Any help as to why the file is ignored and how to overcome this issue would be appreciated.
At first I thought that this might be related to Pre-build task of Visual Studio project fails in GitLab Runner issue, but I don't get any build errors.
On the same machine, I build a .Net Framework app which has the same AssemblyInfo setup, but is compiled using msbuild /property:Configuration=Release by the runner and the produced dll file has all the expected information.
It turns out the problem was partially related to the AssemblyInfo.cs file not being part of the repository.
SDK-style csproj normally don't list the files in the project, but figure them out based on the folder contents. When dotnet build was run, AssemblyInfo.cs wasn't present in the project directory (GitLab-Runner usually clears out files not present in the repository before starting a job/pipeline), so build tools had no idea they needed to load it. It made no difference that the file was being created by the build tools.
The solution proved to be creating an empty AssemblyInfo.cs file before running dotnet build. This way build tools knew they needed to compile it. Actual compilation still happened after prebuild events, so all the needed information was there.
I created the empty AssemblyInfo.cs file using PowerShell:
New-Item -Path "Properties/AssemblyInfo.cs" -ItemType File
Also, checking the build logs helped me finally figure it out. To get the build logs I've called build tools like this:
dotnet build -c release /flp:v=diag
The .Net Framework app didn't have this problem because it wasn't an SDK-style project. All the files needed for compilation were listed in the project file and build tools knew to load them, even if they were created during the prebuild event.

Nothing to do. None of the projects specified contain packages to restore dotnet restore

I am working on an ASP.NET Web API 2 project with .NET target framework 4.6.1. I am trying to setup github workflow for my repo. When the dotnet restore command is run, it throws an error like below.
I am getting the same error if I run the same command in from command prompt inside my project. Also if I run dotnet build, it shows below error.
The project builds fine from Visual Studio but not working from command line or github workflow yml. Can anyone please point me on what am I missing?
The project builds fine from Visual Studio but not working from command line
Check which sln file Visual Studio is using to build your project.
Since I don't see any sln/csproj in your GitHub repository, it is also possible that you have a .gitignore which would prevent adding those in the first place.
DOTNET Restore does not support pacakges.config https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore
So you have to move the nuget package references to csproj file itself
Here is a great comment on how to do that https://stackoverflow.com/a/65701746/8318698
Note: check that if multiple projectGuid is there on csproj at the end of the steps
After that you will be able to use dotnet restore without a hitch.

dotnet publish sln having projects with multiple target frameworks fails

I have a solution with many projects. Some target frameworknetcoreapp2.1, some other target framework netstandard2.0 and one project has a double target framework
<TargetFrameworks>netstandard2.0;net471</TargetFrameworks>
I'd want to have a artifact for win10 with a single command:
dotnet publish MySolution.sln -c Release -o "targetFolder" -r win10-x64
With this command I have this error while building the project with double target framework. Here's the errors:
C:\Program Files\dotnet\sdk\2.1.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.CrossTargeting.targets(31,5) error : The 'Publish' target is not supported without specifying a target framework. The current project targets multiple frameworks, please specify the framework for the published application.
The error is clear. At the end I find that dll compiled in the output directory and it seems like it is a netstandard2.0 dll because my application still works.
I don't like dirty things so, how can I solve my problem?
I would avoid to call N times the "dotnet publish" command if possible.
Don't use dotnet publish with the same output directory on a solution. Especially not with the "-r" argument.
It is dangerous because:
libraries don't have the right trimming behaviour for netstandard facade packages
libraries may have odd behaviour when publishing with "-r", especially for netstandard<2.0 dependencies. (they'd end up copying the .NET Core 1.0/1.1 implementation(!) assemblies)
you may end up with different NuGet dependencies in the output (transitive dependencies)
Copy-to-output/publish-directory items may end up overwriting each other, it may even lead to build failures
Call it individually for all application (console app, web app) projects or create an MSBuild file that publishes these applications.

TeamCity - Building dotnet core web api project using cake script

Running Cake Build script on my local machine works fine but when same script is run on TeamCity, the following error appears: Nuspec file does not exist in package.
Any idea what could have caused this error?
I spent two days due to this error
On dotnet build --configuration Release in TeamCity PowerShell, always got this error
error NU5000: Nuspec file does not exist in package
After deleting this folder, everything worked C:/Users/teamcity_user/.nuget
I add an additional step on top of my build steps which is a command line custom script
dotnet nuget locals --clear all

runtime folder with dotnet core application

If I create a net core 2 console app and get it to reference another project e.g. MyLibrary.csproj
This (MyLibrary.csproj) is a net core class library
If I run dotnet publish -c release --output test1
then in the output folder their is a runtime folder present
I have not found anywhere that describes this folders purpose.
Any one have a link?
Also do I need to copy this as part of my deployment?
The example I have has a reference to System.Data.SqlClient.dll which is present in the root publish folder(test1) so why does it need to get it from the runtime folder when I try to run via dotnet my.dll?
From the docs (https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish?tabs=netcore2x)
dotnet publish - Packs the application and its dependencies into a folder for deployment to a hosting system.

Resources