F#:为什么此代码会引发System.IO.FileNotFoundException:'无法加载文件或程序集'System.Runtime,版本= 4.1.2.0,...”

时间:2018-12-15 20:41:16

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

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

在F#库上,我有一个文件定义为资源:
File File Properties

这是使用库的以下代码:

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,但是该特定版本不可用:
NuGet F#

这是堆栈跟踪:

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.

1 个答案:

答案 0 :(得分:0)

这似乎是您的一个库与您使用的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>