Is it possible for a .NET web service to accept an immutable object (or an object containing references to immutable objects) in a web method?
For example, if this is my web service method:
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public void SetStatus(StatusData data)
{
// do something
}
}
and StatusData contains an immutable property, Server will throw a System.Web.Services.Protocols.SoapException when method is called:
Unable to generate a temporary class (result=1).
Error CS0200: Property or indexer ‘TestProject.Entities.StatusData.Id’ cannot be assigned to — it is read only
Can I still use immutable objects somehow, or is the only way to do it to map everything into mutable DTOs?
[Edit]
Obviously, I am not talking about strings, since string is a base type in .Net, and is really an exception to this problem. For example, XmlSerializer will not work (without explicit IXmlSerializable implementation) for any other immutable class.
But I was hoping that I could avoid manually writing a bunch of mutable DTOs (although the actual mapping can be automated).
Well, it seems that there is no way but to use mutable DTOs for SOAP, so we used DTOs and AutoMapper to convert from immutable objects automatically.