You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps Pipeline加载Microsoft.Bcl.AsyncInterfaces程序集失败求助

Azure DevOps Pipeline执行Postgres交互脚本时缺少Microsoft.Bcl.AsyncInterfaces程序集问题

我在Azure DevOps Pipeline中执行代码仓库内的PowerShell脚本,脚本依赖rhubarb-geek-nz.NpgsqlConnection模块连接PostgreSQL数据库。本地运行完全正常,但在Pipeline中执行时抛出错误:无法加载文件或程序集'Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0'。

已尝试的操作:

  • 切换使用ubuntu-latest、windows-latest、mac13等虚拟机镜像
  • 添加Add-Type -Path "Assemblies\Microsoft.Bcl.AsyncInterfaces.dll"代码手动加载程序集,问题仍未解决

相关配置及脚本

Pipeline配置

trigger:
- none

stages:
- stage: Dev
  jobs:
  - job: "Setup"
    pool:
      vmImage: 'ubuntu-latest'#tried with 'windows-latest' and 'mac13' 
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: 'Write-Host "PowerShell $(pwsh --version)"'

    - task: PowerShell@2
      inputs:
        targetType: 'filePath'
        filePath: './setup.ps1'
        arguments: '-Env "dev"'

setup.ps1脚本

function Install-AdditionalModules {
    Write-Output "Installing the required modules"
    
    # This module is required to communicate with Postgres
    Install-Module -Name rhubarb-geek-nz.NpgsqlConnection -Confirm:$False -Force -ErrorAction SilentlyContinue
}

# Install Additional Modules
Install-AdditionalModules

function Close-DatabaseConnection {
    [CmdletBinding()]
    param(
        [Npgsql.NpgsqlConnection]$connection
    )
    
    try
    {
        $connection.Dispose()
    }
    catch {
       Write-Host "The connection to the Database could not be terminated: $($_.Exception.InnerException.Message)"
    }
}

function Open-DatabaseConnection {
    [CmdletBinding()]
    param(
        [PSCustomObject]$commonConfig
    )
    
    $connection = New-NpgsqlConnection -ConnectionString $commonConfig.connectionstring

    try
    {
        $connection.Open()
    }
    catch {
       Write-Host "The connection to the Database could not be established: $($_.Exception.InnerException.Message)"
    }

    return $connection
}

function Add-Record {
    [CmdletBinding()]
    param(
        [Npgsql.NpgsqlConnection]$connection,
        [string]$commandText
    )
    
    $command = $connection.CreateCommand()
    $command.CommandText = $commandText

    try
    {
        $command.ExecuteNonQuery() | Out-Null
    }
    catch {
        Write-Host "The record could not be added to the Database: $($_.Exception.InnerException.Message)"
     }
}

$connection = Open-DatabaseConnection -commonConfig $config.commonConfig
Add-Record -connection $connection -commandText "Insert into ..."

解决方案

1. 手动安装依赖的NuGet包并加载程序集

rhubarb-geek-nz.NpgsqlConnection依赖的Npgsql库需要Microsoft.Bcl.AsyncInterfaces,直接安装模块时可能因环境缺失.NET依赖导致包未部署。修改Install-AdditionalModules函数,手动安装并加载该包:

function Install-AdditionalModules {
    Write-Output "Installing the required modules"
    
    # 安装NuGet包提供器
    Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
    # 安装指定版本的Microsoft.Bcl.AsyncInterfaces
    Install-Package -Name Microsoft.Bcl.AsyncInterfaces -RequiredVersion 7.0.0 -Scope CurrentUser -Force
    # 安装Postgres模块
    Install-Module -Name rhubarb-geek-nz.NpgsqlConnection -Confirm:$False -Force -ErrorAction SilentlyContinue

    # 加载程序集
    $packagePath = Join-Path (Get-Package Microsoft.Bcl.AsyncInterfaces).Source "lib/netstandard2.0/Microsoft.Bcl.AsyncInterfaces.dll"
    Add-Type -Path $packagePath
}

2. 指定模块版本并清理旧缓存

部分新版本模块可能存在依赖兼容问题,尝试安装稳定版本的模块,并清理旧版本缓存:

function Install-AdditionalModules {
    Write-Output "Installing the required modules"
    
    # 卸载所有旧版本模块
    Uninstall-Module -Name rhubarb-geek-nz.NpgsqlConnection -AllVersions -Force -ErrorAction SilentlyContinue
    # 安装指定稳定版本
    Install-Module -Name rhubarb-geek-nz.NpgsqlConnection -RequiredVersion 3.0.0 -Confirm:$False -Force -ErrorAction Stop
    
    # 安装并加载依赖包
    Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
    Install-Package -Name Microsoft.Bcl.AsyncInterfaces -RequiredVersion 7.0.0 -Scope CurrentUser -Force
    $packagePath = Join-Path (Get-Package Microsoft.Bcl.AsyncInterfaces).Source "lib/netstandard2.0/Microsoft.Bcl.AsyncInterfaces.dll"
    Add-Type -Path $packagePath
}

3. 切换PowerShell执行环境(Windows镜像专属)

若使用windows-latest镜像,可尝试强制使用Windows PowerShell而非pwsh,修改Pipeline任务配置:

- task: PowerShell@2
  inputs:
    targetType: 'filePath'
    filePath: './setup.ps1'
    arguments: '-Env "dev"'
    pwsh: false # 启用Windows PowerShell

4. 检查PowerShell版本兼容性

执行pwsh --version确认Pipeline中的PowerShell版本,部分7.x版本对.NET Standard库加载策略不同,可尝试切换到PowerShell 7.3或更低版本。


内容的提问来源于stack exchange,提问作者One Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:07:05