在F#Web应用程序中使用某种C#类型时遇到问题。 我在F#应用程序中引用的库中存在以下C#类型:
public static class OuterType
{
public class Model
{
private readonly string id;
private readonly bool selected;
private string name;
internal Model() : this(string.Empty) { }
public Model(string id) : this(id, false) { }
public Model(string id, bool selected)
{
this.id = name = id;
this.selected = selected;
}
[Localizable(false)]
public string ID => id;
public bool Selected => selected;
[Localizable(true)]
public string Name
{
get => name;
set => name = value;
}
}
}
我已经为其编写了代理(作为F#Web应用程序的一部分):
[<Proxy(typeof<OuterType.Model>)>]
type ProxyModel =
{
ID : string
Selected : bool
Name : string
}
我的项目编译正常,但是我收到运行时序列化错误:
System.Exception:RPC JSON转换期间出错---> System.Exception:无法在OuterType + Model类型中查找具有字段的名称的转换后的字段名称ID:Name,Selected,Id
这使我相信转换器希望通过字段名称来匹配类型。因此,我将代理更改如下:
[<Proxy(typeof<OuterType.Model>)>]
type ProxyModel =
{
id : string
selected : bool
name : string
}
以上内容导致编译错误:
WebSharper错误FS9001:找不到F#记录类型的字段:OuterType.Model.Selected WebSharper错误FS9001:找不到F#记录类型的字段:OuterType.Model.Name
我被困住了。我有什么想念吗?
该应用程序的目标是netcoreapp2.1。它使用以下WebSharper依赖项(摘自我的paket.lock):
WebSharper (4.5.9.330)
WebSharper.AspNetCore (4.5.3.104)
WebSharper.FSharp (4.5.9.330)
WebSharper.Html (4.5.1.153)
WebSharper.Reactive (4.5.1.139)
WebSharper.UI (4.5.8.161)
答案 0 :(得分:0)
WebSharper远程处理使用反射来创建/读取对象,因此,如果它们是类,则它们必须遵循https://developers.websharper.com/docs/v4.x/cs/remoting#heading-6中的规则。
在这种情况下,您唯一需要做的就是将无参数构造函数公开。
一旦有了,就可以将C#类型用于RPC,但仍可以供客户端使用,您需要在该类型上使用[JavaScript]
(为C#项目添加了WebSharper和WebSharper.CSharp包) ),或与其匹配的代理,包括您无法使用F#记录重现的构造函数,则可能需要一个类。