locked
SignalR method silently fails because can't serialize when custom constructor added RRS feed

  • Question

  • User147722384 posted

    I have a model that can be passed as an argument on hub methods.

    public class Card {
      public int Color;
      public int Number;
      public Card HelperCard;
    
      public Card(int color, int number) {
        Color = color;
        Number = number;
      }
    
      // second constructor
      public Card(int color, int number, bool isHelper) {
        if (isHelper) {
           HelperCard = new Card(color, number);
        }
      }
    }
    

    If I add the second constructor the hub methods silently failing, when I remove it it works fine.

    Saturday, June 20, 2015 11:26 AM

Answers

  • User1711366110 posted

    If I add the second constructor the hub methods silently failing, when I remove it it works fine.

        As per this case, you can use constructor chaining, which means one constructor calls another constructor in the same class using the ": this()" keyword.

    so you can try like below :

    public class Card {
      public int Color;
      public int Number;
      public Card HelperCard;
    
      public Card(int color, int number) {
        Color = color;
        Number = number;
      }
    
      // second constructor
      public Card(int color, int number, bool isHelper) : this (color, number)
      {
        if (isHelper) {
           HelperCard = new Card(color, number);
        }
      }
    }
    

    for more information, click here to refer it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 22, 2015 4:01 AM