Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to return a JSON with Suave F#

Tags:

f#

suave

I'm following the suave tutorial and I'm struggling to return a JSON to the front end. I currently have the code. (I'm not using the Chiron package). I can start the web server just fine but when I go to localhost:8083/hello I get the error message posted below. I'm not sure how to go about debugging this, or what's causing this.

Here is the tutorial I'm following

#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.FSharpFunc2[T,TResult].InvokeFast[V] (Microsoft.FSharp.Core.FSharpFunc2 func, Microsoft.FSharp.Core.T arg1, Microsoft.FSharp.Core.TResult arg2) <0x1052faeb0 + 0x000a1> in :0 at [email protected] (Suave.HttpContext context) <0x10da356f0 + 0x00034> in :0 at Suave.WebPart+bind@14-5[a,b].Invoke (Microsoft.FSharp.Core.FSharpOption1 _arg1) <0x10da331d0 + 0x0004c> in :0 at Microsoft.FSharp.Control.AsyncBuilderImpl+args@835-1[a,b].Invoke (a a) <0x10d4db780 + 0x000af> in :0

like image 749
Luke Xu Avatar asked Jul 02 '16 19:07

Luke Xu


1 Answers

Don't know why it doesn't work with [<DataContract>] but simply this works:

#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" }))

EDIT: It's even worse, I think datacontract serialization is broken with mono. I run version 4.4.1 and if I use the same code that runs fine with .net on windows, I get the error.

like image 95
halcwb Avatar answered Nov 24 '22 00:11

halcwb