【问题标题】:F#: Why does this code throws a System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.1.2.0, ...'F#: 为什么这段代码会抛出 System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.1.2.0, ...'
【发布时间】:2018-12-15 20:41:16
【问题描述】:

我创建了一个 F# 库 (.NET Standard 2.0) 和一个 WPF 应用程序 (.NET Framework 4.7.2)

在 F# 库中,我有一个定义为资源的文件:

这是使用这个库的代码:

let LeResourceFile(resourceName) = 
    let assembly = System.Reflection.Assembly.GetExecutingAssembly();
    //var resourceName = "MyCompany.MyProduct.MyFile.txt";

    use stream = assembly.GetManifestResourceStream(resourceName)
    use reader = new System.IO.StreamReader(stream)

    let result = reader.ReadToEnd();

    result

let LogoBanco (CodigoDoBanco:string) =
    match CodigoDoBanco with
    | "001" -> LeResourceFile("BoletoBancarioFacil.templates.Logos.BB.png")
    | _ -> ""

当我尝试从 C# (NET 4.7.2) 访问库函数 (.NET Standard 2.0) 时,我得到了这个异常:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

为什么会发生这种情况以及如何解决它以使其正常工作?

编辑:我试图在那个 F# 库的 NuGet 上找到这个 System.Runtime,但这个特定版本不可用:

这是堆栈跟踪:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. O sistema não pode encontrar o arquivo especificado.
File name: 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at BoletoBancarioFacil.HTML.LogoBanco(String CodigoDoBanco)
   at BoletoBancarioFacil.HTML.GeraBoletoHTML(Decimal amount, String LinhaDigitavel, String CodigoBarras) in C:\Users\tonyv\source\repos\siteApelosUrgentes\socio\cadastro\BoletoBancarioFacil\HTML.fs:line 49
   at cadastro.MainWindow.GeraHTMLboleto(Titulo titulo) in C:\Users\tonyv\source\repos\siteApelosUrgentes\socio\cadastro\cadastro\MainWindow.xaml.cs:line 389
   at cadastro.MainWindow.ExibeBoleto(Titulo titulo, Boolean Imprime) in C:\Users\tonyv\source\repos\siteApelosUrgentes\socio\cadastro\cadastro\MainWindow.xaml.cs:line 737
   at cadastro.MainWindow.ListBoxTitulos_SelectionChanged(Object sender, SelectionChangedEventArgs e) in C:\Users\tonyv\source\repos\siteApelosUrgentes\socio\cadastro\cadastro\MainWindow.xaml.cs:line 987

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Users/tonyv/source/repos/siteApelosUrgentes/socio/cadastro/cadastro/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : BoletoBancarioFacil, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\tonyv\source\repos\siteApelosUrgentes\socio\cadastro\cadastro\bin\Debug\cadastro.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.1.2.0 redirected to 4.1.2.0.
LOG: Post-policy reference: System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/Users/tonyv/source/repos/siteApelosUrgentes/socio/cadastro/cadastro/bin/Debug/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/tonyv/source/repos/siteApelosUrgentes/socio/cadastro/cadastro/bin/Debug/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/tonyv/source/repos/siteApelosUrgentes/socio/cadastro/cadastro/bin/Debug/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/tonyv/source/repos/siteApelosUrgentes/socio/cadastro/cadastro/bin/Debug/System.Runtime/System.Runtime.EXE.

【问题讨论】:

  • 可以添加异常的堆栈跟踪吗?
  • 好的,我添加了堆栈跟踪信息。
  • 在这种情况下,当我将 C# WPF 应用程序降级到 .NET 4.7.0 时。并且它没有那个例外。 github.com/dotnet/standard/issues/481#issuecomment-429653699
  • 您是否尝试将 .dll 复制到与应用程序相同的文件夹中?
  • 不,我使用 Visual Studio 添加了 F# 项目作为参考。所以它会构建并自动复制文件。

标签: f# .net-standard-2.0 .net-4.7.2


【解决方案1】:

这看起来您的某个库与您正在使用的 dotnet 运行时不兼容。您通常可以通过绑定重定向来解决此问题。我强烈建议使用paket 来管理您的库依赖项。我也会看看binding redirects feature。它可以通过创建运行时配置来修复其中一些版本不一致,这些配置在运行时将错误的版本重新映射到您实际拥有的版本上。您也可以手动创建这些.config 文件,但这很痛苦。这是一个重定向示例,用于将所有依赖的 F# 核心版本映射到我在项目中实际拥有的版本

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <Paket>True</Paket>
        <assemblyIdentity name="FSharp.Core" 
    publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" 
    newVersion="4.5.0.0" />
          </dependentAssembly>
        </assemblyBinding>
    </runtime>
    </configuration>

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 2016-09-18
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多