[英]Unable to properly proxy a C# type to client code in an F# WebSharper application
I am experiencing issues when consuming a certain C# type in an F# web application. 在F#Web应用程序中使用某种C#类型时遇到问题。 The following C# type is present in a library I refer to in my F# app:
我在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;
}
}
}
I have written a proxy for it (as part of my F# web app): 我已经为其编写了代理(作为F#Web应用程序的一部分):
[<Proxy(typeof<OuterType.Model>)>]
type ProxyModel =
{
ID : string
Selected : bool
Name : string
}
My project compiles fine, but I am receiving a runtime serialization error: 我的项目编译正常,但是我收到运行时序列化错误:
System.Exception: Error during RPC JSON conversion ---> System.Exception: Failed to look up translated field name for id in type OuterType+Model with fields: Name, Selected, Id
System.Exception:RPC JSON转换期间发生错误---> System.Exception:无法在OuterType + Model类型的ID中查找具有以下字段的ID的转换字段名称:名称,选定的ID
This lead me to believe the converter is looking to match the type by field names. 这使我相信转换器希望通过字段名称来匹配类型。 So I changed my proxy as follows:
因此,我将代理更改如下:
[<Proxy(typeof<OuterType.Model>)>]
type ProxyModel =
{
id : string
selected : bool
name : string
}
The above leads to a compilation error: 以上导致编译错误:
WebSharper error FS9001: Could not find field of F# record type: OuterType.Model.Selected WebSharper error FS9001: Could not find field of F# record type: OuterType.Model.Name
WebSharper错误FS9001:找不到F#记录类型的字段:OuterType.Model.Selected WebSharper错误FS9001:找不到F#记录类型的字段:OuterType.Model.Name
and I am stuck. 我被困住了。 Is there anything I am missing?
我有什么想念的吗?
The application is targeting netcoreapp2.1. 该应用程序的目标是netcoreapp2.1。 It uses the following WebSharper dependencies (excerpt from my paket.lock):
它使用以下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)
WebSharper remoting uses reflection to create/read objects, so if they are classes, they must follow rules in https://developers.websharper.com/docs/v4.x/cs/remoting#heading-6 . WebSharper远程处理使用反射来创建/读取对象,因此,如果它们是类,则它们必须遵循https://developers.websharper.com/docs/v4.x/cs/remoting#heading-6中的规则。
In this case, the only change you need is to make the parameterless constructor public. 在这种情况下,您唯一需要做的更改就是将无参数构造函数公开。
Once you have that, you can use the C# type for RPC, but still for client-side use, you need a either [JavaScript]
on the type (with WebSharper and WebSharper.CSharp packages added for the C# project), or a proxy that matches it well, including constructors which you won't be able to reproduce with an F# record, you might need a class. 一旦有了它,就可以将C#类型用于RPC,但是仍然可以在客户端使用,您需要在该类型上使用
[JavaScript]
(为C#项目添加了WebSharper和WebSharper.CSharp包),或者使用代理与之匹配的很好,包括您将无法使用F#记录重现的构造函数,您可能需要一个类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.