PowerShell中运行dotMemory提示“无法附加到进程”问题求助
现象
使用JetBrains dotMemory独立命令行版本(2024.1.1)自动化分析进程时,偶发弹出错误:
Unable to attach to the process #mynumber: Make sure it exists and it is a .NET Framework 4.0+/.NET Core 3.0+/.NET 5+ process.
一旦触发该错误,当前PowerShell实例无论重试多少次都失败,必须关闭并重新打开PowerShell窗口才能恢复正常。
复现方式
直接执行命令:
.\dotMemory-2024.1.1.exe --log-level=trace attach 14768
(14768为目标进程PID)
通过PowerShell脚本执行也会出现相同问题:
$proc = New-Object System.Diagnostics.Process $proc.StartInfo.FileName = "dotMemory-2024.1.1.exe" $proc.StartInfo.Arguments = "--log-level=trace attach 14768" $proc.StartInfo.UseShellExecute = $false $proc.StartInfo.RedirectStandardInput = $true $proc.Start()
日志异常信息
在%Temp%\2\JetLogs\JetBrainsLog.JetBrains.2024-04-23T12-20-58.dotMemory-2024_1_1#14768.log日志中捕获到以下异常:
--- EXCEPTION #1/2 [FileLoadException] Message = “Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)” ExceptionPath = Root.InnerException ClassName = System.IO.FileLoadException HResult = FUSION_E_REF_DEF_MISMATCH=80131040 Source = System.Memory FileLoad_FileName = “System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” FileLoad_FusionLog = “ === Pre-bind state information === LOG: DisplayName = System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a (Fully-specified) LOG: Appbase = file:///C:/Users/someuser/AppData/Local/Temp/2/JetBrains/dotMemory.exe/Qudorinirexidelu LOG: Initial PrivatePath = NULL Calling assembly : System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51. === LOG: This bind starts in default load context. LOG: No application configuration file found. LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a LOG: Attempting download of new URL file:///C:/Users/someuser/AppData/Local/Temp/2/JetBrains/dotMemory.exe/Qudorinirexidelu/System.Runtime.CompilerServices.Unsafe.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
疑问
日志显示dotMemory加载了Framework64\v4.0.30319,但已知dotMemory需要.NET Framework 4.7.2,怀疑是否因.NET版本不匹配导致问题?
解决方案建议
确认.NET Framework 4.7.2安装状态
打开控制面板的「程序和功能」,查看是否已安装.NET Framework 4.7.2相关更新。若未安装,下载并安装官方包。添加程序集绑定重定向配置
在dotMemory可执行文件同目录下创建dotMemory-2024.1.1.exe.config文件,添加以下绑定规则,强制.NET加载正确版本的程序集:<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>注意:需根据系统中实际存在的
System.Runtime.CompilerServices.Unsafe版本调整newVersion值。清理JetBrains临时文件
删除%Temp%\2\JetBrains\dotMemory.exe下的临时目录,避免缓存的旧版本程序集干扰。强制PowerShell使用高版本.NET运行
在PowerShell脚本开头添加以下代码,指定使用高版本.NET Framework:$env:COMPLUS_Version = "v4.0.30319"
内容的提问来源于stack exchange,提问作者RvdK

