无法加载文件或程序集...但一开始没有?

Could not load file or assembly ... but not at first?

我有一个归档 Exchange 邮箱的 C#/.NET Windows 服务,我最近“打包”了 Veeam O365 备份 DLL,以便能够自动备份 Exchange Online 邮箱。

我将向您展示一些日志,显示该程序运行一段时间没有问题,然后开始莫名其妙地失败并出现错误:

...直到服务重启:

(见Edit1,我的图片被覆盖了)

堆栈跟踪:

System.Management.Automation.CmdletInvocationException: Could not load file or assembly 'System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) ---> System.IO.FileLoadException: Could not load file or assembly 'System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at Veeam.Core.FileLoggerTransport.AutoArchive()
   at Veeam.Core.FileLoggerTransport.ArchiveAndDelete()
   at Veeam.Core.FileLoggerTransport.ControlLogSize()
   at Veeam.Core.FileLoggerTransport.WriteLine(UInt32 level, String message)
   at Veeam.Core.LoggerTransport.LogDefault(UInt32 level, String message, Object[] args)
   at Veeam.PowerShell.Core.BasePSCmdlet.BeginProcessing()
   at System.Management.Automation.Cmdlet.DoBeginProcessing()
   at System.Management.Automation.CommandProcessorBase.DoBegin()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at VeeamArchiverConnector.VeeamArchiverConnector.ConnectToVeeamServer(String serverName, Int32 port)
   at REMOVED.DeprovisionObject.DeprovisionOnline(String deprovisioningOu) in C:\Users\REMOVED\Source\Repos\ExchangeProvisioning\Deprovisioning\DeprovisionObject.cs:line 442
   at REMOVED.DeprovisionObject.Start() in C:\Users\REMOVED\Source\Repos\ExchangeProvisioning\Deprovisioning\DeprovisionObject.cs:line 125 

该项目确实引用了 System.IO.Compression,尽管:

在我的 .csproj 文件中我有这个:

<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
      <Private>True</Private>
      <Private>True</Private>
</Reference>

我无法访问“Veeam.Core.*”的代码/项目,因为它们是 Veeam O365 备份安装程序附带的 Veeam DLL。

但总体上我不明白的是,它工作了一段时间(通常大约一两个小时),然后错误开始,除非我重新启动服务?

欢迎任何建议!

谢谢


编辑 1:

我在我的“Veeam 连接器”项目中添加了更多日志和自定义异常抛出,但这是一回事:最终错误仍然是 System.IO.Compression 缺失,并且输入从未改变:

detailedlogswithsteps

stillsameerrorevenwithcustomexception

@howcheng 谢谢!成功了,服务 运行 多个小时没有任何问题。

所以问题是这个绑定:

<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />

那是重定向到 4.2.0.0。我假设它在我唯一的 .NETFramework 项目上很好,但正如你提到的,.NETStandard System.IO.Compression 强制使用版本 4.1.2.0,并且在安装时没有更新绑定 > 所以它现在试图找到 4.2。 0.0,已经不存在了,只有 4.1.2.0。

更新绑定 "newVersion=4.1.2.0" 成功了!

我遇到了同样的问题。 netstandard2.0 库正在使用 System.IO.Compression 并从 .NET Fx 4.7.2 测试项目中使用。尝试从 netstandard2.0 加载 System.IO.Compression 时发生 无法加载文件或程序集 'System.IO.Compression, Version=4.2.2.0, Culture=neutral' 异常代码。

修复只是为了

  1. 引用 System.IO.Compression 来自 .NET Fx 4.7.2 测试项目

  2. 从测试调用此方法,这样 System.IO.Compression 从 .NET Fx 环境解析。

    private static void AvoidCannotLoadSystemIOCompressionException() {
     var justToAvoidStrangeException = ZipArchiveMode.Create;
    }
    

奇怪,但它有效