tags:

views:

47

answers:

1

I need to access an enum through a webservice.

As a webservice allocates 0 based integers to an enumeration (ignoring preset values in enum definition), I built the following:

public class StatusType
{
    public StatusVal Pending { get { return new StatusVal( 1, "Pending"); } }
    public StatusVal Authorised { get { return new StatusVal(2, "Authorised"); } }
    public StatusVal Rejected { get { return new StatusVal(3, "Rejected"); } }
    public StatusVal Sent { get { return new StatusVal(4, "Sent"); } }
    public StatusVal InActive { get { return new StatusVal(5, "InActive"); } }

    public List<StatusVal> StatusList()
    {
        List<StatusVal> returnVal = new List<StatusVal>();
        StatusType sv = new StatusType();
        returnVal.Add(sv.Pending);
        returnVal.Add(sv.Authorised);
        returnVal.Add(sv.Rejected);
        returnVal.Add(sv.Sent);
        returnVal.Add(sv.InActive);

        return returnVal;
    }
}

public class StatusVal
{
    public StatusVal(int a, string b) 
    {           
        this.ID = a;
        this.Name = b;
    }
    public int ID { get; set; }
    public string Name { get; set; }
}

I then get the list of StatusVal with the following webmethod:

[WebMethod]   
public List<ATBusiness.StatusVal> GetStatus()
{
   ATBusiness.StatusType a = new ATBusiness.StatusType();
   return a.StatusList();
}

I cannot however use this webmethod as referring it, I get the error: StatusVal cannot be serialized because it does not have a parameterless constructor.

I don't quite understand: should I pass params into the StatusValue type defined as the WebMethod's return Type?

I need this to return a list of StatusVals as per the StatusList() method.

+1  A: 

As the error says, your class needs a constructor without parameters. When unserializing, the runtime will use that constructor instead of the one you have defined.

Something like:

public StatusVal()
{
}

When you created a constructor with parameters, you are automatically removing the default no-parameter constructor, and that's what the compiler is complaining about.

pgb
So what do I need in order to still return the populated list created by the StatusList() method?
callisto
Add the no-parameter constructor, and it should work.
pgb