在可移植类库中使用F#JsonProvider失败 [英] Using F# JsonProvider within a portable class library fails

查看:48
本文介绍了在可移植类库中使用F#JsonProvider失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JsonProvider,并且在其上调用函数时遇到以下错误:

I'm trying to use the JsonProvider and I'm getting the following error when I call a function on it:

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in PortableLibrary1.dll
Additional information: The type initializer for '<StartupCode$PortableLibrary1>.$PortableLibrary1' threw an exception.

我有一个基本的控制台应用程序,如下所示:

I have a basic console application as follows:

module Pinit =
   open FSharp.Data

   type JsonT = JsonProvider<"""..\myFile.json""">
   let doc = JsonT.Load("""..\nyFile.json""")
   let result = doc.GeneratedAt

[<EntryPoint>]
let main argv = 
    printfn "%A" Pinit.doc.GeneratedAt
    0 

在ConsoleApplication中运行时,所有功能均可按预期运行. 如果我按如下方式创建F#可移植类库:

When run within the ConsoleApplication it all works as expected. If I create an F# Portable Class library as follows:

module Pinit =
   open FSharp.Data

   type JsonT = JsonProvider<"""..\myFile.json""">
   let doc = JsonT.Load("""..\nyFile.json""")
   let result = doc.GeneratedAt

创建另一个控制台应用程序,并引用该可移植类库,并按如下所示调用代码:

Create another Console Application and reference that Portable Class Library and call the code as follows:

open PortableLibrary1

[<EntryPoint>]
let main argv = 
    printfn "%A" Pinit.result
    0 

当我运行程序时,它会生成上面定义的异常:

When I run the program then it generates the exception defined above:

我怀疑是因为FSharp.Core的版本,但是想知道我是在做错什么还是有办法使它工作?

I'm suspecting it's because of the versions of FSharp.Core but was wondering whether I was doing something wrong or whether there was a way to get this to work?

版本:

-- ConsoleApplication --
FSharp.Core = 4.3.1.0

-- PortableClassLibrary --
FSharp.Core = 3.3.1.0
FSharp.Data = NuGet Version: 2.0.0

推荐答案

let绑定被编译成静态成员,并且在.NET中,当静态初始化程序中有异常时,真实的异常就被伪装了.

let bindings are compiled into static members, and in .NET when you have an exception in a static initializer the real exception is masqueraded.

如果通过更改为let result() = doc.GeneratedAtprintfn "%A" (Pinit.result())将其转换为函数调用,则会得到真正的异常:

If you turn that into a function call by changing to let result() = doc.GeneratedAt and printfn "%A" (Pinit.result()) you'll get the real exception:

Only web locations are supported

便携式配置文件不支持访问文件系统.因此,在PCL内部,您可以具有Web URL或从嵌入式资源手动加载.参见此处的示例: https://github.com/ovatsus /Apps/blob/master/Trains/Stations.fs#L65 .或者,您可以在控制台项目中使用File.ReadAllText加载文件并将其传递给PCL.

Portable profiles don't support accessing the file system. So inside the PCL you can either have web urls or load manually from an embedded resource. See here for an example: https://github.com/ovatsus/Apps/blob/master/Trains/Stations.fs#L65. Or you can load the file with File.ReadAllText in the console project and pass it to the PCL.

这是一个可移植配置文件(旧版)项目,即配置文件47(和FSharp.Core 2.3.6.0).然后,我还测试了一个可移植的个人档案项目,即个人档案7(和FSharp.Core 3.3),并注意到它无法正常工作,而是给出了此异常

This was with a Portable Profile (Legacy) project, which is Profile 47 (and FSharp.Core 2.3.6.0). I then also tested with a Portable Profile project, which is Profile 7 (and FSharp.Core 3.3), and noticed it's not working correctly, it's giving this exception instead

Method not found: 'FSharp.Data.Runtime.JsonValueOptionAndPath FSharp.Data.Runtime.JsonRuntime.TryGetPropertyUnpackedWithPath(FSharp.Data.Runtime.IJsonDocument, System.String)'.

我创建了一个github问题来跟踪该问题: https://github.com/fsharp /FSharp.Data/issues/521

I created a github issue to track that: https://github.com/fsharp/FSharp.Data/issues/521

这篇关于在可移植类库中使用F#JsonProvider失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆