WCF 显示属性
我有一个 DTO,我用 [Display(Name = "My Display Name")] 装饰了属性。
我正在使用 WCF 服务,但该属性似乎不起作用。在检查我的服务引用时,生成的 DTO 客户端未应用该属性。
也许我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在服务器端创建的类和对象在客户端不存在。当您使用 WCF 时,您将类的实例序列化为数据(通常是 XML,但它也可能是二进制的,具体取决于您的绑定),通过 Internet 发送它,然后将其反序列化为 的实例类似的客户端。这种类似的类通常由服务引用基于服务的 WSDL 创建。
这就是为什么服务器端类上的私有字段不会出现在客户端类上。如果您希望属性在客户端可用,则必须手动将它们添加到客户端。
话虽如此……如果您同时控制服务器和客户端,则可以使用一些技巧来确保双方使用相同的类。最简单的方法是将所有
DataContract
类放入单独的程序集中,并从服务器和客户端引用它。如果您使用服务引用来生成客户端代理,请务必在生成代理时选中“重用引用程序集中的类型”。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.你不能。这些属性不会使用 SOAP 消息中的 DTO 进行序列化。这是因为属性不独立于平台。认为您的服务是为不需要 .NET 实现的客户端提供的。
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.