Autofac cannot be found in release pipeline - azure

I have a program MyProgram.csproj (which happens to be a .NET Core console app) and I am copying it's build (bin\netcoreapp2.2) into the release pipeline and trying to run it MyProgram.dll through Powershell as a task.
When I do so I get the error
2019-08-14T01:35:58.1153997Z An assembly specified in the application dependencies manifest (MyProgram.deps.json) was not found:
2019-08-14T01:35:58.1154837Z package: 'Autofac', version: '4.2.1'
2019-08-14T01:35:58.1155904Z path: 'lib/netstandard1.1/Autofac.dll'
The reference is
"Autofac/4.2.1": {
"dependencies": {
"System.ComponentModel": "4.0.1"
},
"runtime": {
"lib/netstandard1.1/Autofac.dll": {
"assemblyVersion": "4.2.1.0",
"fileVersion": "4.2.1.0"
}
}
}
Any way I can get more information about why this assembly cannot be found?

Can you try adding the following to my .csproj:
<PropertyGroup>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
and try updating all the references on the project

The better way to get rid of dependency chaos is to deploy a self-contained publish, so that includes .NET Core libraries, the .NET Core runtime and referenced package dependencies.
Following this link, you can determine the runtime identifier (e.g. win-x64) and the configuration settings for release or debug builds.
dotnet publish .\MyProgram.csproj -c Release -r "win-x64" --self-contained -o .\publish
You can either use the dotnet extension in the build definition or add a powershell inline command to run the mention codes.
YAML Example
resources:
- repo: self
queue:
name: DefaultAgentPool
steps:
- task: NuGetCommand#2
displayName: 'NuGet restore'
inputs:
feedsToUse: config
nugetConfigPath: nuget.config
- powershell: |
# Write your powershell commands here.
Write-Host "Starting dotnet cli publish command ..."
dotnet publish --runtime "win-x64" -o $(Build.ArtifactStagingDirectory) -c Release --self-contained
workingDirectory: src/Project
displayName: 'dotnet publish'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: win'
inputs:
ArtifactName: win-publish
This makes you sure that you're not going to get any dependency error in the specified runtime platform.

Related

How to resolve target-issues in Azure Devops Pipeline when creating Self-contained deployments

Because Azure AppServices doesn't (anymore) support .NET Core 2.1 on x64 with framework-dependent deployments, we are currently publishing self-contained win-x64 versions of our .NET Core 2.1 Web API.
I'm trying to setup an Azure Pipeline in Yaml for CI/CD purposes and deploy it to Azure App Service deployment slot.
The issue I'm trying to resolve is this error-message: project.assets.json' doesn't have a target for 'netcoreapp2.1/win10-x64'
/usr/bin/dotnet publish /home/vsts/work/1/s/MyApp.WebApi/MyApp.WebApi.csproj --configuration Release -f netcoreapp2.1 -r win10-x64 --self-contained true --no-restore --output /home/vsts/work/1/a/MyApp.WebApi
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
/usr/share/dotnet/sdk/5.0.102/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: Assets file '/home/vsts/work/1/s/MyApp.WebApi/obj/project.assets.json' doesn't have a target for 'netcoreapp2.1/win10-x64'. Ensure that restore has run and that you have included 'netcoreapp2.1' in the TargetFrameworks for your project. You may also need to include 'win10-x64' in your project's RuntimeIdentifiers. [/home/vsts/work/1/s/MyApp.WebApi/MyApp.WebApi.csproj]
##[error]Error: The process '/usr/bin/dotnet' failed with exit code 1
This is my yaml file:
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- develop
pool:
vmImage: 'ubuntu-20.04'
variables:
buildConfiguration: 'Release'
buildPlatform: x64
steps:
- task: DotNetCoreCLI#2
displayName: dotnet restore
inputs:
command: 'restore'
projects: '**/*.csproj'
feedsToUse: 'config'
nugetConfigPath: './NuGet.config'
externalFeedCredentials: 'Hangfire Pro'
- task: DotNetCoreCLI#2
displayName: dotnet publish
inputs:
command: 'publish'
publishWebProjects: true
feedsToUse: 'config'
nugetConfigPath: './NuGet.config'
externalFeedCredentials: 'Hangfire Pro'
arguments: '--configuration $(BuildConfiguration) -f netcoreapp2.1 -r win10-x64 --self-contained true --no-restore --output $(build.artifactstagingdirectory)'
# this code takes all the files in $(Build.ArtifactStagingDirectory) and uploads them as an artifact of your build.
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'MyAppWebApi'
I tried modifying the CSPROJ file by adding these:
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
**<RuntimeIdentifiers>win10-x64;win-x64</RuntimeIdentifiers>**
and
<PlatformTarget>x64</PlatformTarget>
Please delete the obj and bin folders from your project. Also the <TargetFramework> in your .csproj file was singular. Please try to add an "s" and it become "TargetFrameworks" to see if it works.
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
You could refer to this thread for more possible solutions.
BTW, there are different build environment between Microsoft-hosted Windows-2019 agents(which install Visual Studio Enterprise 2019) and your local machine, you could try to use self-hosted Windows agents to build your project without additional environment preparation steps.
A few things went wrong during my work. The most important one is that all the changes I've tried to make were pushed to the Develop-branch while the Pipeline was running on a different branch. My inexperience with Pipelines (and also Git) mainly caused this. I was under the impression that the trigger line in YAML pointed to the repository to restore and publish.
I finally got it working with the following steps. Like Edward Han mentioned, I tried to restore and publish by command line on my local machine. These also gave me some errors I needed to fix. Like adding this line to all (30+) referenced csproj files:
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
Then add this configuration to the webproject csproj:
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
Then my final YAML file is this:
trigger:
- develop
pool:
vmImage: 'windows-2019'
variables:
buildConfiguration: 'Release'
buildPlatform: x64
steps:
- task: UseDotNet#2
displayName: 'Use .Net Core sdk 2.1.x'
inputs:
version: 2.1.x
- task: DotNetCoreCLI#2
displayName: dotnet restore
inputs:
command: 'restore'
projects: '**/*.csproj'
arguments: '-r win-x64'
feedsToUse: 'config'
nugetConfigPath: './NuGet.config'
externalFeedCredentials: 'Hangfire Pro'
- task: DotNetCoreCLI#2
displayName: dotnet test
inputs:
command: 'test'
projects: '**/*[Tt]ests/*.csproj'
arguments: '--configuration $(BuildConfiguration)'
testRunTitle: 'dotnet test 2'
- task: DotNetCoreCLI#2
displayName: dotnet publish
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration $(BuildConfiguration) -r win-x64 --self-contained true --no-restore --output $(build.artifactstagingdirectory)'
# this code takes all the files in $(Build.ArtifactStagingDirectory) and uploads them as an artifact of your build.
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'MyAppWebApi'
Few important things I changed/added:
Explicitly tell to use .NET Core 2.1.*
Add -runtime parameter to the RESTORE command
Conslusion
Although I studied some Pluralsight courses, YouTube video's and blogs, I still ended up reading the Microsoft Docs/MSDN. Being used to use publishing by the Visual Studio User Interface, switching to command-line really was a big difference. A lot more depencenies are needed to be looked at. Tip: really read the urls these error messages provide, they do contain enough information to resolve your issue. At least, that's what I learned.

Why is msbuild.exe missing in C:\Program Files\dotnet\sdk\3.1.201? Causing AzureDevOps build step to fail

I have a very simple WPF project (.Net 4.7.2) and Unit Test project (MSTest .Net Core) and I'm trying to get it to build in Azure Pipelines and I'm getting following error:
2020-05-07T16:41:52.9562570Z C:\Program
Files\dotnet\sdk\3.1.201\Microsoft.Common.CurrentVersion.targets(3032,5):
error MSB4216: Could not run the "GenerateResource" task because
MSBuild could not create or connect to a task host with runtime "CLR4"
and architecture "x86". Please ensure that (1) the requested runtime
and/or architecture are available on the machine, and (2) that the
required executable "C:\Program Files\dotnet\sdk\3.1.201\MSBuild.exe"
exists and can be run.
[C:\agent_work\4\s\RsSolution4\WpfApp1\WpfApp1.csproj]
2020-05-07T16:41:53.1174401Z ##[error]Error: The process 'C:\Program
Files\dotnet\dotnet.exe' failed with exit code 1
I looked for msbuild.exe in suggested folder and sure enough it doesn't exist. The path exists and there are many files in folder, just not msbuild.exe. There's an msbuild.dll.
Here's my yaml file:
trigger:
- master
pool:
name: Default
demands: msbuild
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: DotNetCoreCLI#2
displayName: Restore NuGet
inputs:
command: 'custom'
projects: '**/*.csproj'
custom: 'restore'
- task: MSBuild#1
inputs:
solution: '**/*.sln'
msbuildLocationMethod: 'location'
msbuildLocation: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\msbuild.exe'
configuration: 'Release'
clean: true
- task: DotNetCoreCLI#2
displayName: SSP Automated Testing
inputs:
command: 'test'
projects: '**/*Test*.csproj'
arguments: '--configuration $(buildConfiguration)'
testRunTitle: 'SSP Testing'
It seems like this known issue for dotnet core. It said On .NET Core, MSBuild doesn't support task hosts of different architectures/runtime versions.
The workaround provided is to add CurrentArchitecture/CurrentRuntime to PropertyGroup. See this similar issue.
<PropertyGroup Condition="'$(MSBuildRuntimeType)' == 'Core' Or '$(TargetFrameworkIdentifier)' != '.NETFramework'">
<GenerateResourceMSBuildArchitecture Condition=" '$(GenerateResourceMSBuildArchitecture)' == '' ">CurrentArchitecture</GenerateResourceMSBuildArchitecture>
<GenerateResourceMSBuildRuntime Condition=" '$(GenerateResourceMSBuildRuntime)' == '' ">CurrentRuntime</GenerateResourceMSBuildRuntime>
</PropertyGroup>
You also try using msbuild task only to build all your projects, for msbuild works for both .net framework and .net core. You can specify solutions and projects for the solution parameter of Msbuild task.
Since there are .Net framework projects and .Net Core projects in your solution. I would suggest using Nuget restore task to restore the solution. Dotnet cli doesnot works properly with .NET Framework, which wil probably fail to restore the .net framework projects.
Hope above helps!
make sure you add a UseDotNet task to ensure the correct version of the SDK is available on the agent:
- task: UseDotNet#2
inputs:
packageType: 'sdk'
version: '3.1.201'
And I'd opt to use the dotnet build task to build the .NET Core project and the VisualStudioBuild task for the WPF project, that way you're not intermingling framework types and SDKs in then same solution build.

Azure DevOps .NET Core build and publish doesn't keep given application version

I've an ASP.NET Core application I'm publishing on a dedicated server via Azure DevOps build/release pipelines.
I'm managing the application version number with the GitVersion task (gittools.gitversion.gitversion-task.GitVersion#4) in the YAML build.
The build step is something like:
- task: DotNetCoreCLI#2
displayName: 'dotnet build'
inputs:
command: custom
custom: build
workingDirectory: src/MyAppProjectFolder
arguments: '-p:Version=$(GitVersion.FullSemVer)'
And is correctly generating the .exe with the given FullSemVer (I'm inspecting the Azure agent work folder)
Then I've the publish step:
- task: DotNetCoreCLI#2
displayName: 'dotnet publish'
inputs:
command: publish
publishWebProjects: false
arguments: '--output $(build.artifactstagingdirectory) --no-restore --no-build'
workingDirectory: src/MyAppProjectFolder
For some reason the same .exe i found in the C:\agent_work\1\a\a.zip created by the publish DOESN'T have the correct version number, but the generic 1.0.0.
If I "emulate" the pipelines manually on the same server (with dotnet build and dotnet publish manually via powershell, same parameters) everything works as expected.
What's going on? Is there a way to ensure the application to keep the $(GitVersion.FullSemVer) version?
Note: I had to add
- task: UseDotNet#2
displayName: 'Use .Net Core sdk 2.1.x'
inputs:
packageType: sdk
version: 2.1.x
installationPath: $(Agent.ToolsDirectory)/dotnet
includePreviewVersions: true
in front of each NET Core task, as explained here, after the agents have been updated to .NET Core 3.0 (before these builds worked well).
Try adding -p:Version=$(GitVersion.FullSemVer) to your arguments for the publish step.

How to create a YAML build pipeline in azure devops for xamarin projects with referenced dot net projects

I recently moved my sources to azure devOps to use cd/ci and all the other cool stuff.
Now i created my first build pipeline to build the android part of my Xamarin project. But I end up getting an error message, that a resource of a referenced project could not be found and i shall do a package restore and try again.
Now, since i have azure hosted build agents and not self hosted, i have no ways of setting the agent up properly before doing the build.
But i guess there should be some way to properly configure the build pipeline to do all the necessary stuff.
Its just that i have no clue what i should add to my yaml file in order to fix this stuff.
This is the error message i got:
##[error]C:\Program Files\dotnet\sdk\2.2.105\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5): Error NETSDK1004: Assets file 'd:\a\1\s\*****\*****\*****\*****\obj\project.assets.json' not found. Run a NuGet package restore to generate this file.
The problem is, that this file should be generated by compiling a referenced project and is not part of a nuget package.
Here is my build pipeline as far as i figured it out by myself.
# Xamarin.Android
# Build a Xamarin.Android project.
# Add steps that test, sign, and distribute an app, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/xamarin
trigger:
- Share/main
pool:
vmImage: 'VS2017-Win2016'
variables:
buildConfiguration: 'Debug'
outputDirectory: '$(build.binariesDirectory)/$(buildConfiguration)'
steps:
- task: NuGetToolInstaller#1
inputs:
versionSpec: 5.1.0
- task: NuGetCommand#2
displayName: 'Restore NuGet Packages'
inputs:
command: restore
restoreSolution: '**/*.sln'
- task: XamarinAndroid#1
inputs:
projectFile: 'Mobile4/Droid/Mobile4.Droid.csproj'
outputDirectory: '$(outputDirectory)'
configuration: '$(buildConfiguration)'
- task: AndroidSigning#3
inputs:
apksign: false
zipalign: false
apkFiles: '$(outputDirectory)/*.apk'
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(outputDirectory)'
The build always breaks on step XamarinAndroid
I hope you can help me.
The solution must be out there somewhere, i just cannot see it right now.
Thx in Advance.
Mav
[error]C:\Program Files\dotnet\sdk\2.2.105\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5):
Error NETSDK1004: Assets file
'd:\a\1\s********************\obj\project.assets.json' not found.
Run a NuGet package restore to generate this file.
According to this error message, the project is .NetCore and its SDK used is 2.2.105. For the file "....\obj\project.assets.json", whether the project.assets.json exists is determined by package restore step. Now, it prompt this could not be found, it means the package restore does not restore this file successfully.
As I mentioned previously, it is a .NetCore project. So you should use dotnet restore instead of nuget restore. For .NetCore project, the obj folder restored by nuget restore does not contain project.assets.json in it.
So, to solve the issue you meet, you should replace the task Nuget restore as dotnet restore: dotnet.
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '**/*.csproj'
vstsFeed: 'e157d03d-******-fc06f9e13177'

Visual Studio Team Services Release/Deploy fails - "No package found with specified pattern"

I'm trying to implement continuous integration and continuous deployment to my DEV Azure App Service. I'm using the hosted agent on Visual Studio Team Services. The "Deploy Website to Azure" step on my Release definition keeps failing with the error "No package found with specified pattern". Any ideas?
"More than one package matched with specified pattern. Please restrain the search patern [sic]." error usually occurs when 2 or more packages were found by the task since you entered "xxx\*.zip" in "Package or Folder" setting of the task. So you just need to update it to specify the detailed package name. Similar question here: Deploy azure website and webjobs in same sln using VSO - Error - There can be only one.
And for you original issue, you can also fix it by creating a new build definition with "Visual Studio" selected on "Build" tab and "Azure WebApp" selected on "Deployment" tab. This will create a build definition with required arugments added.
Had the same problem few hours ago. This how I was able to resolve the issue:
Ensure MSBuild arguments in Build solution step are:
/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\"
Add step Azure App Service Deployment: ARM
Configure subscription and App Service Name
Package or Folder should be $(build.artifactstagingdirectory)\**\*.zip
Steps:
Azure App Service Deployment Configuration:
if you are using the default azure app service deployment task, add this to end of YAML file:
- task: DotNetCoreCLI#2
displayName: 'dotnet publish $(buildConfiguration)'
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
- task: PublishBuildArtifacts#1
displayName: 'publish artifacts'
I had the same issue and this worked for me:
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- task: DotNetCoreCLI#2
displayName: 'dotnet publish $(buildConfiguration)'
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
- task: PublishBuildArtifacts#1
displayName: 'publish artifacts'
use the visual designer while creating build pipeline in azure devops,though your code sits at azure repos and github,
then select source
finally pick respective templates to your application
Make sure you didn't tick "Skip artifacts download"

Resources