【问题标题】:FSharp.Data missing method exceptionFSharp.Data 缺少方法异常
【发布时间】:2017-08-29 07:10:50
【问题描述】:

我正在尝试在预编译的 F# Azure 函数中解决 MissingMethodException。当我从 FSharp.Data.CssSelectorExtensions 调用扩展方法时会引发异常。

该函数在 .Net Framework 4.6.2 类库中定义。我正在使用当前版本的 FSharp.Core (4.2.3) 和 FSharp.Data (2.3.3)。 (我已经尝试过两者的旧版本,但问题仍然存在。)我已根据此类问题的标准指南为 FSharp.Core 添加了绑定重定向。代码编译干净,但执行时失败。如果我尝试将扩展方法直接作为简单的静态方法调用,它也会失败。

任何关于我如何摆脱这个异常的指导将不胜感激!

功能代码

module httpfunc

open System.Net
open System.Net.Http
open Microsoft.Azure.WebJobs.Host
open FSharp.Data
open FSharp.Data.CssSelectorExtensions

let Run(req: HttpRequestMessage, log: TraceWriter) =
    async {
        let doc = HtmlDocument.Load("https://google.com")
        let node = doc.CssSelect("div.ctr-p") // <-- method is missing
        return req.CreateResponse(HttpStatusCode.OK)
    } |> Async.RunSynchronously

异常消息

 mscorlib: Exception while executing function: Functions.httpfunc. 
 mscorlib: Exception has been thrown by the target of an invocation. 
 fsfuncs: Method not found: 'Microsoft.FSharp.Collections.FSharpList`1<FSharp.Data.HtmlNode> CssSelectorExtensions.CssSelect(FSharp.Data.HtmlDocument, System.String)'.

app.config

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

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="FSharp.Core" version="4.2.3" targetFramework="net462" />
  <package id="FSharp.Data" version="2.3.3" targetFramework="net462" />
  ...
</packages>

.fsproj

<Project ToolsVersion="15.0" ... />
  <PropertyGroup>
    <RootNamespace>fsfuncs</RootNamespace>
    <AssemblyName>fsfuncs</AssemblyName>
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <TargetFSharpCoreVersion>4.4.1.0</TargetFSharpCoreVersion>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Name>fsfuncs</Name>
  </PropertyGroup>
  ...
</Project>

编辑

根据 Fyodor Soikin 的建议,我确定正在加载多个版本的 FSharp.Core.dll:一个来自 GAC,另一个来自 NuGet 包文件夹。

【问题讨论】:

  • 我会在调试器下运行它并查看加载了哪些模块,以及您是否获得了 FSharp.Core 的两个不同版本的副本。
  • 不幸的是,我不知道该怎么做——据我所知,Visual Studio 还没有对 F# Azure Functions 的调试器支持。另外,当只涉及一个类库时,您能否帮助我理解如何获得两个不同版本的FSharp.Core?也就是说,第二个版本是从哪里来的?
  • 有一种方法可以在本地运行 Azure Functions,check it out。不同的版本可能来自非常意想不到的地方。一个显而易见的选择是 GAC。
  • 酷,感谢您的指点!看起来确实正在加载 FSharp.Core.dll 的两个版本(请参阅我刚才的编辑。)是从 GAC 中删除 FSharp.Core.dll 的解决方案,还是我可以阻止它从那里加载?
  • 这意味着你的bindingRedirect没有被占用。我现在要验证通常的嫌疑人:配置是否在正确的位置?版本是否正确?等

标签: f# f#-data missingmethodexception


【解决方案1】:

Azure Functions 背后的执行引擎已经加载了FSharp.Core.dll(因为它依赖于 F# 编译器服务来运行您的 F# 脚本),我认为您将始终获得由execution engine's app.config 指定的FSharp.Core.dll 版本,这是4.4.0.0。

我可能遗漏了一些东西,但我认为你最好的机会是让你的函数使用版本 4.4.0.0。您可以尝试删除显式的FSharp.Core 引用吗?这样,运行时应该只加载(已经预加载的)FSharp.Core 版本。

【讨论】:

  • 谢谢托马斯,这很有帮助。我正在尽力使用打包的FSharp.Core,但根据 Fyodor Soikin 的 cmets,我的绑定重定向似乎不适用于类库,因此不适合编译的函数。我很欣赏指向执行引擎 app.config 的指针;这将使我的FSharp.Core GAC 参考与平台版本保持同步变得更容易。
猜你喜欢
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多