David
David

Reputation: 3609

WCF Display Attribute

I have a DTO which I have decorated properties with [Display(Name = "My Display Name")].

I'm using WCF services but the attribute does not appear to be working. On inspecting my service reference the generated DTO client side does not have the attribute applied.

Maybe I'm doing something wrong?

Upvotes: 1

Views: 540

Answers (2)

Agustin Meriles
Agustin Meriles

Reputation: 4854

You can't. The attributes doesn't serialize with the DTO in a SOAP message. This is because Attributes are not platform independent. Think that your service is for clients that are not necessary .NET implementations.

Upvotes: 0

Randolpho
Randolpho

Reputation: 56381

The class and object you created server-side doesn't exist client-side. When you are using WCF, you are serializing an instance of a class to data (typically XML, but it could also be binary depending on your binding), sending it across the internet, and then deserializing it into an instance of a similar class client-side. This similar class is typically created by the Service Reference based on the WSDL of the service.

This is why private fields on your server-side class do not appear on your client-side class. If you want the attributes available client-side, you're going to have to manually add them client-side.

That having been said... if you control both the server and client, there are tricks you can use to ensure you use the same class on both sides. The simplest is to put all of your DataContract classes into a separate assembly and reference it from both the server and client. If you use a Service Reference to generate your client-side proxy, be sure to check "Reuse types in referenced assemblies" when generating the proxy.

Upvotes: 1

Related Questions