I have a class like below
[DataContract]
public class Order
{
[DataMember]
int orderId
[DataMember]
string ProductCode
[DataMember]
Double Quantity
[DataMember]
Decimal AmountToPay
}
Is it possible to have AmountToPay field as readOnly(as it will be calculated backend) so that clients wont be able to set it?
I know the calculation can override what is set by the client but I think its much neater if the client is no开发者_高级运维t able to set it.
You can put your server models into a separate NuGet package (or assembly) and share the package (or assembly) with the client.
Note that your models have to have private setters in order to be serialized and deserialized.
Something like:
[DataContract]
public class Order
{
[DataMember]
public int orderId { get; private set; }
...
}
The client would then add your package and choose "Reuse types in Referenced Assemblies." See Configure Service Reference dialog box
If the client cannot set the property you will not be able to transport the message across the wire. Remember that the client needs to serialize the class before passing it to the server and if the serializer cannot write to this property the server will not be able to reconstruct it.
Not really - there might be tricks and ways around it, but basically, WCF will serialize your call into a XML-based message, and send it across the wire. You're not dealing with a .NET object you're setting properties on - you're dealing with something that gets serialized into a message to be sent across the wire.
Even if you define one field to be read-only on your server side, when the client detects that service creates the client side proxy for it, based on the service metadata that is exchanged (think: WSDL and XSD), both visibility (private, internal etc.) as well as get/set methods are ignored, since they're only .NET relevant constructs. They don't show up as such in the WSDL/XSD and thus cannot be recreated on the client side.
The WCF svcutil
will create a client-side copy of your data contract class, but it will create a standard class with all read/write properties.
精彩评论