1

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?

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • 2
    Wild guess: Serializers? If there's any serialization/deserialization going on, it wouldn't know how to access your constructor. – Josh Nov 18 '19 at 21:32
  • @Josh Thanks Josh, you're right, no doubt this is a problem with serialization but I guess I was hoping for some sugggestions on how you might be able to get around this problem. I've updated my question to be more clear. – Maxim Gershkovich Nov 18 '19 at 21:35
  • Which constructor the serializer should use, then creating an instance ? Most often you need a parameterless constructor. By removing your 2 constructors, actually you add an auto-generated parameterless constructor. Instead of removing your two constructors, you can also add a third (parameterless) constructor. – Holger Nov 18 '19 at 22:31
  • Try adding a default constructor to `GetStockBarcodeByBarcodeResponse` with other constructors. Serializers expects a default constructor. – vendettamit Nov 18 '19 at 22:32
  • 2
    Akka.NET by default uses JSON.NET so all of it's limitations apply here. See: https://stackoverflow.com/questions/24678734/unable-to-deserialize-classes-with-multiple-constructors-with-json-net – Bartosz Sypytkowski Nov 18 '19 at 23:43
  • I tried to do that too, but for me helped add `default ctor` only. When I marked some ctor as `JsonConstructor` it started to throw an exception. – EgoPingvina Dec 10 '19 at 07:35

1 Answers1

1

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

Aaronontheweb
  • 8,224
  • 6
  • 32
  • 61