Why do multiple constructors in messages cause Akka.NET to fail delivery?

298 Views Asked by At

I've got a very simple implementation using Akka.NET remoting.

Specifically I have two actors:

public class ClientQueryActor : ReceiveActor
{
    public ClientQueryActor(ActorSelection stockBarcodeActor)
    {
        this._stockBarcodeActor = stockBarcodeActor ?? throw new ArgumentNullException("stockBarcodeActor must be provided.");

        this.Receive<GetStockBarcodeByBarcodeRequest>(this.HandleStockBarcodeByBarcodeRequestReceived);
        this.Receive<GetStockBarcodeByBarcodeResponse>(this.HandleStockBarcodeByBarcodeResponseReceived);
    }

    private void HandleStockBarcodeByBarcodeRequestReceived(GetStockBarcodeByBarcodeRequest obj)
    {
        this._stockBarcodeActor.Tell(obj);
    }

    private void HandleStockBarcodeByBarcodeResponseReceived(GetStockBarcodeByBarcodeResponse obj)
    {

    }
}


public class StockBarcodeQueryActor : ReceiveActor
{

    public StockBarcodeQueryActor()
    {    
        this.Receive<GetStockBarcodeByBarcodeRequest>(this.HandleStockBarcodeByBarcodeRequestReceived);
    }

    private void HandleStockBarcodeByBarcodeRequestReceived(GetStockBarcodeByBarcodeRequest obj)
    {
        this.Sender.Tell(new GetStockBarcodeByBarcodeResponse(true, null, null));
    }
}

For the most part these actors seem to be working properly the issue is in the messages I am sending.

My message class looks roughly like this:

public class GetStockBarcodeByBarcodeResponse 
{
    public GetStockBarcodeByBarcodeResponse(bool success) { }

    public GetStockBarcodeByBarcodeResponse(bool success, IEnumerable<string> errors) { } 
}

However when I attempt to send a message using this class I get the error

'Association with remote system akka.tcp://client@localhost:2552 has failed; address is now gated for 5000 ms. Reason is: [Akka.Remote.EndpointDisassociatedException: Disassociated'

When I remove the multiple constructors the message sends successfully.

I have been unable to find anything in the documentation referencing this problem, can someone please explain this limitation to me?

Could anyone provide any suggested workarounds?

1

There are 1 best solutions below

0
Aaronontheweb On BEST ANSWER

Bartosz's comment is the correct answer - we use JSON.NET serialization by default for user-defined messages and in order for deserialization to work you need to mark one of the constructors using the JsonConstructor attribute: Unable to deserialize classes with multiple constructors with Json.NET