gpt4 book ai didi

f# - 无法使用 Suave F# 返回 JSON

转载 作者:行者123 更新时间:2023-12-04 12:29:24 25 4
gpt4 key购买 nike

我正在学习 suave 教程,并且正在努力将 JSON 返回到前端。我目前有代码。 (我没有使用 Chiron 包)。我可以很好地启动 Web 服务器,但是当我转到 localhost:8083/hello 时,我收到下面发布的错误消息。我不知道如何调试这个,或者是什么导致了这个。

Here是我正在关注的教程

#r "../packages/Suave/lib/net40/Suave.dll"
#r "System.Runtime.Serialization.dll"
#r "../packages/FParsec/lib/net40-client/FParsecCS.dll"
#r "../packages/Aether/lib/net35/Aether.dll"
#r "../packages/Chiron/lib/net40/Chiron.dll"
module Test =

open Suave // always open suave
open Suave.Successful // for OK-result
open Suave.Web // for config
open Suave.Operators
open Suave.Http
open Suave.Filters
open Suave.Json
open System.Runtime.Serialization
open Suave.Writers

[<DataContract>]
type Foo =
{
[<field: DataMember(Name = "foo")>]
foo : string;
}

[<DataContract>]
type Bar =
{
[<field: DataMember(Name = "bar")>]
bar : string;
}

let router =
choose
[path "/hello" >=> (mapJson (fun (a:Foo) -> { bar = a.foo })) >=> setMimeType "application/json; charset=utf-8";
path "/bye" >=> OK "GOOD WHAT"]

startWebServer defaultConfig router

Derived classes must implement it

System.NotImplementedException: Derived classes must implement it at System.Reflection.Module.GetCustomAttributes (System.Type attributeType, Boolean inherit) <0x104618170 + 0x00010> in :0 at System.Reflection.Emit.ModuleBuilder.GetCustomAttributes (System.Type attributeType, Boolean inherit) <0x1045f1800 + 0x0001b> in :0 at System.Runtime.Serialization.DataContract.GetGlobalDataContractNamespace (System.String clrNs, ICustomAttributeProvider customAttribuetProvider) <0x10da40f60 + 0x00045> in :0 at System.Runtime.Serialization.DataContract.GetDefaultDataContractNamespace (System.Type type) <0x10da40e80 + 0x00051> in :0 at System.Runtime.Serialization.DataContract.GetDCTypeStableName (System.Type type, System.Runtime.Serialization.DataContractAttribute dataContractAttribute) <0x10da3fd50 + 0x000f5> in :0 at System.Runtime.Serialization.DataContract.GetStableName (System.Type type, System.Collections.Generic.Dictionary2 previousCollectionTypes,
System.Boolean& hasDataContract) <0x10da3f570 + 0x00092> in :0 at
System.Runtime.Serialization.DataContract.GetStableName (System.Type
type, System.Boolean& hasDataContract) <0x10da3f4f0 + 0x00058> in :0
at
System.Runtime.Serialization.ClassDataContract+ClassDataContractCriticalHelper.GetStableNameAndSetHasDataContract
(System.Type type) <0x10da3f490 + 0x00031> in :0 at
System.Runtime.Serialization.ClassDataContract+ClassDataContractCriticalHelper..ctor
(System.Type type) <0x10da3e1e0 + 0x00044> in :0 at
System.Runtime.Serialization.ClassDataContract..ctor (System.Type
type) <0x10da3e120 + 0x00045> in :0 at
System.Runtime.Serialization.DataContract+DataContractCriticalHelper.CreateDataContract
(Int32 id, RuntimeTypeHandle typeHandle, System.Type type)
<0x10da3ab00 + 0x0031e> in :0 at
System.Runtime.Serialization.DataContract+DataContractCriticalHelper.GetDataContractSkipValidation
(Int32 id, RuntimeTypeHandle typeHandle, System.Type type)
<0x10da3aa50 + 0x0005c> in :0 at
System.Runtime.Serialization.DataContract.GetDataContractSkipValidation
(Int32 id, RuntimeTypeHandle typeHandle, System.Type type)
<0x10da3aa10 + 0x00023> in :0 at
System.Runtime.Serialization.DataContract.GetDataContract (Int32 id,
RuntimeTypeHandle typeHandle, SerializationMode mode) <0x10da3a9c0 +
0x00025> in :0 at
System.Runtime.Serialization.DataContract.GetDataContract
(RuntimeTypeHandle typeHandle, System.Type type, SerializationMode
mode) <0x10da3a240 + 0x00036> in :0 at
System.Runtime.Serialization.DataContract.GetDataContract (System.Type
type) <0x10da3a1f0 + 0x0002f> in :0 at
System.Runtime.Serialization.Json.DataContractJsonSerializer.get_RootContract
() <0x10da3a0f0 + 0x0005b> in :0 at
System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalIsStartObject
(System.Runtime.Serialization.XmlReaderDelegator reader) <0x10da39fa0
+ 0x00038> in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject
(System.Runtime.Serialization.XmlReaderDelegator xmlReader, Boolean
verifyObjectName) <0x10da39b60 + 0x00046> in :0 at
System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject
(System.Runtime.Serialization.XmlReaderDelegator reader, Boolean
verifyObjectName, System.Runtime.Serialization.DataContractResolver
dataContractResolver) <0x10da39b30 + 0x00020> in :0 at
System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions
(System.Runtime.Serialization.XmlReaderDelegator reader, Boolean
verifyObjectName, System.Runtime.Serialization.DataContractResolver
dataContractResolver) <0x10da39760 + 0x0020f> in :0 at
System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions
(System.Runtime.Serialization.XmlReaderDelegator reader, Boolean
verifyObjectName) <0x10da396f0 + 0x00025> in :0 at
System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject
(System.Xml.XmlDictionaryReader reader) <0x10da39480 + 0x0006d> in :0
at
System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject
(System.IO.Stream stream) <0x10da36030 + 0x0005c> in :0 at
Suave.Json.fromJson[T] (System.Byte[] bytes) <0x10da35890 + 0x00103>
in :0 at Suave.Json+mapJson@31[a,b].Invoke (Suave.HttpRequest r)
<0x10da35740 + 0x0004f> in :0 at
Microsoft.FSharp.Core.FSharpFunc
2[T,TResult].InvokeFast[V] (Microsoft.FSharp.Core.FSharpFunc2 func, Microsoft.FSharp.Core.T
arg1, Microsoft.FSharp.Core.TResult arg2) <0x1052faeb0 + 0x000a1> in
:0 at Suave.Json+mapJson@31-2.Invoke (Suave.HttpContext context)
<0x10da356f0 + 0x00034> in :0 at Suave.WebPart+bind@14-5[a,b].Invoke
(Microsoft.FSharp.Core.FSharpOption
1 _arg1) <0x10da331d0 + 0x0004c> in :0 at Microsoft.FSharp.Control.AsyncBuilderImpl+args@835-1[a,b].Invoke (a a) <0x10d4db780 + 0x000af> in :0

最佳答案

不知道为什么它不适用于 [<DataContract>]但这只是有效:

#I __SOURCE_DIRECTORY__
#r "../../../packages/FSPowerPack.Core.Community/Lib/Net40/FSharp.PowerPack.dll"
#r "../../../packages/FSPowerPack.Linq.Community/Lib/Net40/FSharp.PowerPack.Linq.dll"
#r "../../../packages/FSPowerPack.Metadata.Community/Lib/Net40/FSharp.PowerPack.Metadata.dll"
#r "../../../packages/FSPowerPack.Parallel.Seq.Community/Lib/Net40/FSharp.PowerPack.Parallel.Seq.dll"
#r "../../../packages/Informedica.GenUnits.lib/lib/net45/Informedica.GenUnits.lib.dll"
#r "../../../packages/Informedica.GenUtils.Lib/lib/net45/Informedica.GenUtils.Lib.dll"
#r "../../../packages/Suave/lib/net40/Suave.dll"
#r "../../../packages/Suave.Testing/lib/net40/Suave.Testing.dll"
#r "../../../packages/Fuchu/lib/Fuchu.dll"
#r "System.Core.dll"
#r "System.dll"
#r "System.Numerics.dll"
#r "System.Runtime.Serialization.dll"
#r "System.Net.Http.dll"

#time


//Environment.CurrentDirectory <- __SOURCE_DIRECTORY__



open System
open System.IO
open System.Text
open System.Net.Http

open Suave // always open suave
open Suave.Successful // for OK-result
open Suave.Web // for config
open Suave.Operators
open Suave.Http
open Suave.Filters
open Suave.Json
open System.Runtime.Serialization
open Suave.Writers

open Suave.Testing


//[<DataContract>]
type Bar =
{
// [<field: DataMember(Name = "bar")>]
bar : string;
}


//[<DataContract>]
//[<CLIMutable>]
type Foo =
{
// [<field: DataMember(Name = "foo")>]
foo : string;
}

let router =
choose
[ POST >=> path "/hello" >=> (mapJson (fun (a:Foo) -> { Bar.bar = a.foo })) >=> setMimeType "application/json; charset=utf-8";
path "/bye" >=> OK "GOOD WHAT"]


runWith defaultConfig router
|> req HttpMethod.POST "/hello" (Some <| new ByteArrayContent(toJson { foo = "foo" }))

编辑:更糟糕的是,我认为数据契约序列化被单声道破坏了。我运行 4.4.1 版,如果我使用在 Windows 上运行良好的相同代码,我会收到错误消息。

关于f# - 无法使用 Suave F# 返回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38163968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com