C# 交互式 - 无法加载文件或程序集

9

我正在尝试使用C# Interactive测试一些mongodb驱动程序,但是当我尝试创建MongoClient时,我会收到以下异常:

> var client = new MongoClient();
Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  + MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateOSDocument()
  + Lazy<T>.CreateValue()
  + Lazy<T>.get_Value()
  + MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateClientDocument(string)
  + MongoDB.Driver.Core.Connections.BinaryConnectionFactory..ctor(MongoDB.Driver.Core.Configuration.ConnectionSettings, MongoDB.Driver.Core.Connections.IStreamFactory, MongoDB.Driver.Core.Events.IEventSubscriber)
  + MongoDB.Driver.Core.Configuration.ClusterBuilder.BuildCluster()
  + MongoDB.Driver.ClusterRegistry.CreateCluster(MongoDB.Driver.ClusterKey)
  + MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(MongoDB.Driver.ClusterKey)
  + MongoDB.Driver.MongoClient..ctor(MongoDB.Driver.MongoClientSettings)
  + MongoDB.Driver.MongoClient..ctor()

以下是详细的复现步骤:

通过命令行安装最新版本的MongoDB驱动程序。

C:\dev>nuget.exe install MongoDB.Driver -OutputDirectory tools -ExcludeVersion

Feeds used:
  https://api.nuget.org/v3/index.json
  C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
  C:\Program Files\Microsoft SDKs\Service Fabric\packages\

Attempting to gather dependencies information for package 'MongoDB.Driver.2.4.3' with respect to project 'tools', targeting 'Any,Version=v0.0'
Attempting to resolve dependencies for package 'MongoDB.Driver.2.4.3' with DependencyBehavior 'Lowest'
Resolving actions to install package 'MongoDB.Driver.2.4.3'
Resolved actions to install package 'MongoDB.Driver.2.4.3'
Adding package 'MongoDB.Bson.2.4.3' to folder 'C:\dev\tools'
Added package 'MongoDB.Bson.2.4.3' to folder 'C:\dev\tools'
Successfully installed 'MongoDB.Bson 2.4.3' to tools
Adding package 'System.Runtime.InteropServices.RuntimeInformation.4.0.0' to folder 'C:\dev\tools'
Added package 'System.Runtime.InteropServices.RuntimeInformation.4.0.0' to folder 'C:\dev\tools'
Successfully installed 'System.Runtime.InteropServices.RuntimeInformation 4.0.0' to tools
Adding package 'MongoDB.Driver.Core.2.4.3' to folder 'C:\dev\tools'
Added package 'MongoDB.Driver.Core.2.4.3' to folder 'C:\dev\tools'
Successfully installed 'MongoDB.Driver.Core 2.4.3' to tools
Adding package 'MongoDB.Driver.2.4.3' to folder 'C:\dev\tools'
Added package 'MongoDB.Driver.2.4.3' to folder 'C:\dev\tools'
Successfully installed 'MongoDB.Driver 2.4.3' to tools

打开 C# 交互窗口并导入 dll 文件。

Microsoft (R) Roslyn C# Compiler version 2.0.0.61501
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
> #r "c:\dev\tools\MongoDB.Driver\lib\net45\MongoDB.Driver.dll"
> #r "c:\dev\tools\MongoDB.Bson\lib\net45\MongoDB.Bson.dll"
> #r "c:\dev\tools\MongoDB.Driver.Core\lib\net45\MongoDB.Driver.Core.dll"
> #r "c:\dev\tools\System.Runtime.InteropServices.RuntimeInformation\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll"

然后尝试创建MongoClient
> var client = new MongoClient();
Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  + MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateOSDocument()
  + Lazy<T>.CreateValue()

2
我没有你的答案,但我曾经与类似的恶魔作战。融合日志记录是你的朋友:
  1. https://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx
  2. https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer
- ScottWelker
1个回答

0

最近我遇到了这个问题,从 web.config 中删除 System.Runtime.InteropServices.RuntimeInformation 代码解决了它:(如果你有 Windows 应用程序,请查看 app.config)。

移除这段代码就可以解决问题:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接