Wcf, serialization, readonly member
I am learning WCF and one of the challenges I am faced with today is to implement this design:
[DataContract]
public class MyOwnFaultException : FaultException<MyResult>
{
public MyOwnFaultException(MyResult result) : base(result) {}
}
[DataContract]
public class MyResult
{
readonly DefinedResults dres;
public MyResult(DefinedResults res) {
dres = res;
}
[DataMember]
public DefinedResults DRes
{
get { return this.dres; }
//do not want to have the setter for this, but w.o it .net throws exception
}
}
[DataContract]
public enum DefinedResults
{
Success = 0,
Fail =开发者_如何学Go 1,
}
Service
[OperationContract]
[FaultContract(typeof(MyOwnFaultException))]
void TestRemoteException();
So, naturally when I try to create a reference to my 'faulty' service I get the "No Set For DRes member" exception which is understood. But how to I implement the readonly field pattern to the value object used for result indicator?
This is because services share schema and contract, not class.
Having said that, you could hack it by doing:
[DataMember]
public DefinedResults DRes
{
get { return this.dres; }
private set { /* NOOP */ }
}
There is no value object when using WCF. All object received and send by operation must be serializable - must have parameterless constructor (as marc pointed in comment this is not necessary for DataContractSerializer
) and all serialized field must be writable. If you want to send object which is not serializable you can implement IDataContractSurrogate or just implement your own Data transfer object (DTO).
精彩评论