Warm tip: This article is reproduced from serverfault.com, please click

Akka.Remote F# Serialization works only on the first field of a discriminated union

发布于 2020-12-02 20:48:21

To send a discriminated union field to a remote actor I am using Hyperion as the serializer, but it seems to serialize only the first field but does not serialize the rest of the union. The sample code is as follows:

Server.fsx

#r "nuget: Akka.FSharp" 
#r "nuget: Akka.TestKit"
#r "nuget: Akka.Remote"
#r "nuget: Akka.Serialization.Hyperion"
#load "Message.fsx"

open Message
open System
open Akka.FSharp
open Akka.Remote
open Akka.Configuration
open Akka.Serialization
let configuration = 
    ConfigurationFactory.ParseString(
        @"akka {
            actor {
                provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
                debug : {
                    receive : on
                    autoreceive : on
                    lifecycle : on
                    event-stream : on
                    unhandled : on
                }
                serializers {
                    hyperion = ""Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion""
                }
                serialization-bindings {
                    ""System.Object"" = hyperion
                }                
            }
            remote {
                helios.tcp {
                    port = 9002
                    hostname = 192.168.0.94
                }
            }
        }")

let serversystem = System.create "Server" configuration



let server (mailbox:Actor<_>) =
    let rec loop () = actor {
        let! message = mailbox.Receive()
        match message with
        | Message(num, num1, str) -> printfn "Got a number %d %d %s" num num1 str
        | CreateDone(num, num1, str) -> printfn "Got a create %d %d %s" num num1 str
    }
    loop ()

let serveRef = spawn serversystem "server" server
Console.ReadLine() |> ignore

Client.fsx

#r "nuget: Akka.FSharp" 
#r "nuget: Akka.TestKit"
#r "nuget: Akka.Remote"
#r "nuget: Akka.Serialization.Hyperion"
#load "Message.fsx"

open Message
open System
open Akka.FSharp
open Akka.Remote
open Akka.Configuration
open Akka.Serialization

let configuration = 
    ConfigurationFactory.ParseString(
        @"akka {
            actor {
                provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
                serializers {
                    hyperion = ""Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion""
                }
                serialization-bindings {
                    ""System.Object"" = hyperion
                }               
            }
            remote {
                helios.tcp {
                    port = 2552
                    hostname = localhost
                }
            }
        }")

let clientSystem = System.create "client" configuration

let serveRef = select ("akka.tcp://Server@192.168.0.94:9002/user/server") clientSystem
serveRef <! Message(10L, 20, "Hello")
serveRef <! CreateDone(10L, 20, "Hi")
Console.ReadLine()

Message.fsx

type Message = Message of int64 * int * string
              | CreateDone of int64 * int * string

The output of the server is:

F:\UF\Courses\FALL2020\DOS\Projects\Project4.1>dotnet fsi --langversion:preview serve.fsx
[INFO][12/2/2020 1:42:13 AM][Thread 0001][remoting (akka://Server)] Starting remoting
[INFO][12/2/2020 1:42:13 AM][Thread 0001][remoting (akka://Server)] Remoting started; listening on addresses : [akka.tcp://Server@192.168.0.94:9002]
[INFO][12/2/2020 1:42:13 AM][Thread 0001][remoting (akka://Server)] Remoting now listens on addresses: [akka.tcp://Server@192.168.0.94:9002]
Got a number 10 20 Hello

So it seems like only the first field gets serialized in a discriminated union. How to overcome this?

Questioner
Rajath Ganesh
Viewed
0
neil danson 2020-12-03 05:29:41

Your actor isn’t looping so terminates after the first message

Add a

return! loop()

inside your recursive loop.

I think the question is slightly confusing. It states the 1st field is printed, but all the fields are present in the print output. I assume you mean only the first message

Akka in c# examples don’t need the loop, but Akka.fsharp matches the MailboxProxessor syntax so requires explicit looping.