tl;博士:
所以你只需要找到这个程序集并将它添加到与 MongoDriver 相同的文件夹中。我通过在 Visual Studio 中创建了一个控制台应用程序并从 Nuget 添加了驱动程序来找到它。互操作程序集将位于包目录中。
https://www.nuget.org/packages/mongocsharpdriver/2.5.0/
研究:
按照此处所述启用 Fusion Logging:
https://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx
现在初始化 MongoClient
Add-Type -Path "C:\temp\Mongo\MongoDB.Bson.dll"
Add-Type -Path "C:\Temp\Mongo\MongoDB.Driver.dll"
$mongoClient = New-Object MongoDB.Driver.MongoClient
您应该会在融合日志中看到类似的内容:
*** Assembly Binder Log Entry (12/29/2017 @ 1:50:26 PM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = powershell.exe
Calling assembly : MongoDB.Driver.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
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: Publisher policy file is not found.
LOG: Post-policy reference: System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: All probing URLs attempted and failed.
因此,根据错误,它正在加载程序集MongoDB.Driver.Core,这就是引发异常的地方。
所以你只需要找到这个程序集并将其添加到与 MongoDriver 相同的文件夹中。我通过在 Visual Studio 中创建了一个控制台应用程序并从 Nuget 添加了驱动程序来找到它。互操作程序集将位于包目录中。
https://www.nuget.org/packages/mongocsharpdriver/2.5.0/