简体   繁体   English

WCF显示属性

[英]WCF Display Attribute

I have a DTO which I have decorated properties with [Display(Name = "My Display Name")]. 我有一个DTO,它已使用[Display(Name =“ My Display Name”))装饰属性。

I'm using WCF services but the attribute does not appear to be working. 我正在使用WCF服务,但该属性似乎无法正常工作。 On inspecting my service reference the generated DTO client side does not have the attribute applied. 在检查我的服务参考时,生成的DTO客户端未应用该属性。

Maybe I'm doing something wrong? 也许我做错了什么?

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. 使用WCF时,您正在将一个类的实例序列化为数据(通常是XML,但也可以是二进制,具体取决于您的绑定),然后通过Internet发送它,然后将其反序列化为类似的类客户端的实例-侧。 This similar class is typically created by the Service Reference based on the WSDL of the service. 此相似类通常由服务参考基于服务的WSDL创建。

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. 最简单的方法是将所有DataContract类放入一个单独的程序集中,并从服务器和客户端两者中引用它。 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. 如果使用服务参考来生成客户端代理,请确保在生成代理时选中“在引用程序集中重用类型”。

You can't. 你不能 The attributes doesn't serialize with the DTO in a SOAP message. 该属性不会在SOAP消息中与DTO序列化。 This is because Attributes are not platform independent. 这是因为属性不是平台无关的。 Think that your service is for clients that are not necessary .NET implementations. 认为您的服务是针对不需要.NET实现的客户端的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM