I’m making a car auction website as a college project. The site uses a web service to do everything, and to make my life easier I made this class to store information about a car:
public class Car
{
private int id;
private string name;
private string desc;
private int startbid;
private DateTime closingdate;
public int Id
{
get
{
return id;
}
}
public string Name
{
get
{
return name;
}
}
public string Description
{
get
{
return desc;
}
}
public int StartBid
{
get
{
return startbid;
}
}
public DateTime ClosingDate
{
get
{
return closingdate;
}
}
public Car(int id, string name, string desc, int startbid, DateTime closingdate)
{
this.id = id;
this.name = name;
this.desc = desc;
this.startbid = startbid;
this.closingdate = closingdate;
}
}
Whenever I tried to view the .asmx in my browser I get an error saying this class cannot be serialized because it does not have a parameterless constructor, yet everything that uses the Car class still works just fine in the web site (eg. a page that displays information about a car shows everything with no exceptions). So the web service is still working and somehow the Car class is still being serialized. How is this possible?
EDIT: I know that to get my .asmx to show up properly I need to add public Car() {} to my class but then I also need to add set methods to every property, which I don’t really want to do because these results are fetched from the database and are thus (in this case) read only. So what is the proper solution in this case? Perhaps an Obsolete attribute or something similar?
It is possible to allocate an instance of a class bypassing its constructor:
But, as MSDN remarks:
So I guess that could be going on under the hoods for your Web Service to work.