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

.NET 8 ASP.NET Core Web API调用WSUS时加载BaseApi程序集失败求助

问题描述

我正在基于.NET 8.0开发对接Microsoft WSUS的ASP.NET Core Web API,已引用以下程序集:

C:\Program Files\Update Services\Api\Microsoft.UpdateServices.Administration.dll

运行以下代码时:

AdminProxy proxy = new AdminProxy();

objUpdateServer = proxy.GetRemoteUpdateServerInstance(sServerName, false, iPort);

在第二行触发异常:

{"Could not load file or assembly 'Microsoft.UpdateServices.BaseApi, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.":"Microsoft.UpdateServices.BaseApi, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"}

at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
at System.Reflection.Assembly.Load(String assemblyString)
at Microsoft.UpdateServices.Administration.AdminProxy.CreateUpdateServer(Object[] args)
at Microsoft.UpdateServices.Administration.AdminProxy.GetUpdateServer(String serverName, Boolean useSecureConnection, Int32 portNumber)
at Microsoft.UpdateServices.Administration.AdminProxy.GetRemoteUpdateServerInstance(String serverName, Boolean useSecureConnection, Int32 portNumber)
at iWSUS_Server.WSUSAPI..ctor(String sServerName, Int32 iPort, Logger pm_Logger) in C:\GitLab\Projects\iWSUS-Server\iWSUS_Server\Classes\WSUSAPI.cs:line 32

此前用.NET Framework 4.7.2开发WCF服务时,引用相同的DLL可正常运行,请问该如何解决这个问题?

解决方案
  • 补充引用依赖的BaseApi程序集
    Microsoft.UpdateServices.Administration.dll依赖Microsoft.UpdateServices.BaseApi.dll,该文件同样位于C:\Program Files\Update Services\Api\目录下。在.NET 8项目中添加对该DLL的引用,并将其「复制本地」属性设置为True(右键引用→属性→复制本地→设为True),确保编译时该DLL被复制到输出目录。

  • 启用.NET Framework兼容性配置
    WSUS管理API基于.NET Framework开发,在.NET 8中使用需开启兼容性支持:

    1. 在项目的.csproj文件中添加以下配置:
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <UseWindowsForms>true</UseWindowsForms>
        <EnableWindowsFormsCompatibility>true</EnableWindowsFormsCompatibility>
      </PropertyGroup>
      
    2. 安装Microsoft.Windows.Compatibility NuGet包,提供.NET Framework到.NET 8的兼容性桥接:
      Install-Package Microsoft.Windows.Compatibility
      
  • 手动加载缺失的程序集
    若上述方法无效,可在项目启动时添加程序集加载逻辑,强制指定BaseApi的路径:

    using System.Reflection;
    
    // 在Program.cs或启动类中添加
    AssemblyLoadContext.Default.Resolving += (context, assemblyName) =>
    {
        if (assemblyName.Name == "Microsoft.UpdateServices.BaseApi")
        {
            string path = @"C:\Program Files\Update Services\Api\Microsoft.UpdateServices.BaseApi.dll";
            return context.LoadFromAssemblyPath(path);
        }
        return null;
    };
    
  • 匹配运行环境位数
    WSUS的API DLL为32位,需将.NET 8项目的平台目标设置为x86(右键项目→属性→生成→平台目标→选择x86),避免因位数不匹配导致加载失败。

内容的提问来源于stack exchange,提问作者Le ZVince

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 03:35:01