Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webservice cannot be serialized because it does not have a parameterless constructor

I have a webservice which I have edited, before it worked without a problem. however right now im getting this error: cannot be serialized because it does not have a parameterless constructor I have posted my class below.

public class Class
{
    public class AnsweredQ
    {
        public string Question { get; set; }
        public string Answer { get; set; }

        public AnsweredQ(string _Question, string _Answer)
        {
            Question = _Question;
            Answer = _Answer;
        }
    }
    public class UnAnsweredQ
    {
        public string Question { get; set; }
        public string[] Options { get; set; }

        public UnAnsweredQ(string _Question, string[] _Options)
        {
            Question = _Question;
            Options = _Options;
        }
    }
    public class Trial
    {
        public string User { get; set; }
        public DateTime TrialDate { get; set; }
        public bool Expired { get; set; }

        public Trial (string _User, DateTime _TrialDate, bool _Expired)
        {
            User = _User;
            TrialDate = _TrialDate;
            Expired = _Expired;
        }
    }
}

if you can solve this please explain what i did wrong :)

like image 891
Kage Avatar asked May 20 '12 09:05

Kage


1 Answers

To be able to serialize / deserialize a class, the serializer requires a parameterless constructor. So, you need to add the parameterless constructors to your classes, i.e.:

public class AnsweredQ
    {
        public string Question { get; set; }
        public string Answer { get; set; }

       public AnsweredQ() {  }


        public AnsweredQ(string _Question, string _Answer)
        {
            Question = _Question;
            Answer = _Answer;
        }
    }
    public class UnAnsweredQ
    {


        public string Question { get; set; }
        public string[] Options { get; set; }

        public UnAnsweredQ() {}

        public UnAnsweredQ(string _Question, string[] _Options)
        {
            Question = _Question;
            Options = _Options;
        }
    }


    public class Trial
    {
        public string User { get; set; }
        public DateTime TrialDate { get; set; }
        public bool Expired { get; set; }

        public Trial ()
        {
        }

        public Trial (string _User, DateTime _TrialDate, bool _Expired)
        {
            User = _User;
            TrialDate = _TrialDate;
            Expired = _Expired;
        }
    }
}
like image 121
platon Avatar answered Sep 24 '22 03:09

platon