Azure Devops Pipeline Azure Repos Git .Net Error in Tests Failed to run as a self-contained app - asp.net-core

I have a .Net solution with multiple projects, including a test project.
When I create a pipeline for my solution using Azure Devops templates, with the connect Azure Repos Git and configure ASP.Net Core (.Net Framework), I get the following yml file:
# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- main
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
When I run the pipline and reaches the VSTest Task, after running the tests, it gives me the following error:
##[error]Testhost process for source(s) 'D:\a\1\s\Fazer.UITests\bin\Release\net6.0\Microsoft.TestPlatform.PlatformAbstractions.dll' exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
##[error]Failed to run as a self-contained app.
##[error] - The application was run as a self-contained app because 'D:\a\1\s\Fazer.UITests\bin\Release\net6.0\testhost.runtimeconfig.json' was not found.
##[error] - If this should be a framework-dependent app, add the 'D:\a\1\s\Fazer.UITests\bin\Release\net6.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
##[error]. Please check the diagnostic logs for more information.
How can I fix this?

After a while searching for the solution, I found out that all I had to do was to change the line **\*test*.dll to **\*Tests.dll for the key testAssemblyVer2 in the task VSTest#2.
Before:
- task: VSTest#2
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
--> **\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
After:
- task: VSTest#2
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
--> **\*Tests.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
Then the pipeline runned sucesssfully!

Related

Chromedriver.exe UI Testing Error with AzureDevOps

EasyRepo Selenium Tests running on Visual Studio 2019, but i get an error stating that the "Chrome Driver" does not exist, however the chromedriver.exe does indeed exist.
I want to conduct UI Tests via ADO Pipelines for Selenium EasyRepo whilst using Chrome on the Hosted Agent running on Windows 2019, but it keeps failing.
# YAML Azure DevOps Pipeline for Visual Studio UI Testing with NuGet and Chrome
trigger:
- master
pool:
vmImage: 'windows-2019'
variables:
solution: 'Automation.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: VisualStudioTestPlatformInstaller#1
inputs:
packageFeedSelector: 'nugetOrg'
versionSelector: 'latestPreRelease'
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: 'Automation.dll'
searchFolder: '$(System.DefaultWorkingDirectory)'
vsTestVersion: '16.0'
runSettingsFile: 'TestSettings.runsettings'
In the app settings i have also hardcoded the web driver for chrome to see if it would make a difference.
<add key="DriversPath" value="C:\SeleniumWebDrivers\ChromeDriver" />
The error;
##[error] [ERROR] The file D:\a\_temp\\Automation\bin\Debug\chromedriver.exe does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html

DevOps pipeline CI error when I updated .net core 3.1 to .net core 6

So I updated all the projects of my solution to .net core 6, before every single one was .net core 3.1.
Then I started to get the following error in build step:
##[error]PjInspe.Tests\PjInspe.Tests.csproj(0,0): Error NU1201:
Project PjInspe.Aplicacao is not compatible with netcoreapp3.1
(.NETCoreApp,Version=v3.1). Project PjInspe.Aplicacao supports:
net6.0 (.NETCoreApp,Version=v6.0)
But all the projects has this in the .csproj
And its the only project that is failing
Any ideas how to solve this?
EDIT:
My pipeline YAML
Iam using the windows 2022 to run the CI and I've seen the sdk from the net6 on the build logs
pool:
name: Azure Pipelines
steps:
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: restore
projects: '**/*Project.csproj'
vstsFeed: '***************'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(Parameters.RestoreBuildProjects)'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
projects: '$(Parameters.TestProjects)'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Publish
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'
zipAfterPublish: True
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
The right way is supposted to be to modify you dotnetcore cli task as follows
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
version: '6.0.x' //better to put this in a variable
projects: '$(Parameters.TestProjects)'
arguments: '--configuration $(BuildConfiguration)'
But I still had a similar build problem. I fixed it by using the use dotnet core task BEFORE all other tasks and specifying the version I need.
This is the yaml code that I used
- task: UseDotNet#2
displayName: 'Use .NET 6 Core sdk'
inputs:
packageType: 'sdk'
version: '6.0.x'

NETSDK1045: The current .NET SDK does not support 'newer version' as a target

I've created a simple ASP.NET CORE 6 Web API. I then pushed it to Github. When I try to create a pipeline in Azure Devops, I'm getting the error.
C:\Program Files\dotnet\sdk\5.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET
.TargetFrameworkInference.targets(141,5): error NETSDK1045: The current .NET SDK does not support
targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports
.NET 6.0.
I've just downloaded VS 2022 community edition. I've installed .Net SDK 6.
Here's my csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>
I chose what Azure devops suggested, so this is my yml file
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package
/p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true
/p:PackageLocation="$(build.artifactStagingDirectory)"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
I've followed what's said in this documentation, but it's not working. The build is still failing like this
[![enter image description here][2]][2]
Thanks for helping
After reading this 2 links, it there were 2 issues with the template from Azure devops.
the restore packages kept on looking for SDK 5. So this task fixed the issue. I've to include a step that mentions SDK 6. https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/package/nuget?view=azure-devops
also it kept on targeting Visual Studio 2019, which doesn't support .NET 6.0.x. Therefore, instead of vmImage: 'windows-latest', using vmImage: 'windows-2022' allow the build to succeed https://github.com/dotnet/core/issues/6907.
I ended up using the classic template so that I can understand better how the pipeline works. 1) Either using ubuntu-latest or windows-2022. 2) add a step for the version of SDK.
I chose what Azure devops suggested, so this is my yml file
Unfortunately it's easy to confuse things, since Microsoft's naming is anything but consistent.
You need to use .NET Core guide for this: Build, test, and deploy .NET Core apps
To save you time, here is a simple YAML pipeline to build ASP.NET Core applications:
trigger:
branches:
include:
- master
# Setup pipeline-level variables to keep things DRY
variables:
configuration: Release
projects: '**/*.csproj'
publish_dir: $(Build.ArtifactStagingDirectory)
vm_image: ubuntu-latest
stages:
- stage: Build
jobs:
- job: dotnet
displayName: .NET
pool:
vmImage: $(vm_image)
# Run builds in latest .NET 6 SDK container (Debian 11)
# This simplifies things and allows to easily target different SDKs:
# https://hub.docker.com/_/microsoft-dotnet-sdk
container: mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim
workspace:
clean: all
# Build tasks a separated (restore/build/test/publish)
# to make it easier to catch/debug issues
steps:
- task: DotNetCoreCLI#2
displayName: .NET | Restore [$(configuration)]
inputs:
command: restore
projects: $(projects)
- task: DotNetCoreCLI#2
displayName: .NET | Build [$(configuration)]
inputs:
command: build
projects: $(projects)
arguments: --configuration $(configuration) --no-restore
- task: DotNetCoreCLI#2
displayName: .NET | Test [$(configuration)]
inputs:
command: test
projects: $(projects)
publishTestResults: true
arguments: --configuration $(configuration) --no-restore --no-build
- task: DotNetCoreCLI#2
displayName: .NET | Publish [$(configuration)]
inputs:
command: publish
publishWebProjects: true
zipAfterPublish: true
modifyOutputPath: true
arguments: --configuration $(configuration) --output $(publish_dir) --no-restore --no-build
# Publish zipped build results so they can be downloaded from the pipeline UI
- publish: $(publish_dir)
displayName: Artifact | Publish
artifact: $(Build.DefinitionName)

DevOps IIS Release Pipeline Not Extracting Files To Correct Location

I am trying to setup a Release Pipeline for an ASPNET Core application to an on-premises IIS Virtual Machine (VM).
Everything is working as expected except when it deploys the files to the IIS server, but it's not extracting the files and it's dropping the zip file in the wrong folder location. For example, the application files are supposed to be deployed to %SystemDrive%\sites\{mysite} - This is setup in the Release Pipeline under the IIS Web App Manage section (shown below).
On the server, this is deploying to the correct location, but instead of all the app files being deployed to the \sites\{mysite} it's creating two additional folders and then dropping a zip file called "WebApp.zip" like this, (%SystemDrive%\sites\{mysite}\{artifactName}\{artifactName}\WebApp.zip)
How do I get this to actually only deploy the website app files to the \sites\{mysite} directory?
I do have a yaml file, shown below. Any help would be greatly appreciated.
trigger:
- development
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI#2
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifactName: '{artifactName}' #removed the name for this post
- task: IISWebAppDeploymentOnMachineGroup#0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: '{removed}' #removed the name for this post
Package: '$(System.DefaultWorkingDirectory)'
RemoveAdditionalFilesFlag: true
XmlVariableSubstitution: True
EDIT:
I've updated my yaml to look like what's shown below. If I do not include the PublishPipelineArtifact#1 task then no files ever get copied to the IIS server. This is also deploying now to a folder structure like this: {artifact}\{artifact}\(all of the files) which is an improvement, but still I don't need the two {artifact} folders, I just want the files deployed in the website's root directory.
Any help is greatly appreciated as I've spent a pretty ridiculous amount of time on this thus far.
Updated Yaml:
trigger:
- development
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: PublishPipelineArtifact#1
inputs:
targetPath: $(System.DefaultWorkingDirectory)/bin/Release/net5.0
artifact: {artifactName}
Specify the path to the *.zip package file for your IISWebAppDeploymentOnMachineGroup#0 task's Package input instead of just the default working directory.
Edit:
You have a lot going on that's confusing me and I believe it's probably because you're not sure what you're doing - forgive me if incorrect. First, you look like you're building and packaging the build results twice - with VSBuild#1 the first time, then using dotnet build and dotnet publish the second time via two DotNetCoreCLI#2 tasks. You then have a PublishPipelineArtifact#1 referencing the target of both packaging/publishing tasks.
You then attempt to deploy these packages but reference a completely different path/file. Typically a build and deployment would be split into two separate jobs and the deployment job would download the published pipeline artifact. That downloaded artifact is the path/file you want to reference in your deployment task's Package input.

Azure pipeline missing references

I'm trying to use the Azure pipelines to build and publish my projects. Unfortunately there is not much documentation or Q&A around.
My solution contains 12 libraries, most with .net core/standard, one with angular (But this is not the problem). In visual studio building/publishing works. In my dev.azure workspace it doesn't.
this is my YAML-file
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#0
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
and the problem is, that all the references are missing while building:
Error CS0234: The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
does anyone have any idea on how to fix this? The nuget restore works.
This issue look like about dependencies fail. It should because your project have referenced the dependencies, but did not installed them during the pipeline.
You can try with executing dotnet restore first before calling your VSBuild task.