简体   繁体   English

在WCF中使用MetadataType

[英]Using a MetadataType with WCF

I have an auto-generated WCF Client and I thought that I would be able to add Data Annotations by using a partial class and adding a Metedatatype attribute to it. 我有一个自动生成的WCF客户端,我认为我可以通过使用部分类并向其中添加Metedatatype属性来添加数据注释。 Seemingly this will not work; 看来这行不通; as I have seen from other posts the MetadataType needs to be registered and I have tried this approach but with no success. 正如我从其他帖子中看到的那样,需要注册MetadataType,并且我尝试了这种方法,但没有成功。

Validation works if I place a validation attribute directly on the field, and maybe I should do this and manually create the client DTOs, but I was wondering if anyone out there knew of a way to get this working. 如果我将验证属性直接放在字段上,则验证有效,也许我应该这样做并手动创建客户端DTO,但是我想知道是否有人知道如何进行此工作。

I will happily post some code if people think it will help. 如果人们认为有帮助,我会很高兴地发布一些代码。

Thanks. 谢谢。

Create in your solution another project (let's call it "SolutionName.Infrastructure") responsible for storing DataContracts and ServiceContracts. 在解决方案中创建另一个项目(称为“ SolutionName.Infrastructure”),该项目负责存储DataContracts和ServiceContracts。 Then in your service and client projects add reference to Infrastructure project. 然后在您的服务和客户端项目中添加对基础结构项目的引用。 Such a solution declines the amount of code being created since now you store all code in one place depriving of auto-generated code. 由于现在您将所有代码都存储在一个地方,从而剥夺了自动生成的代码,因此这种解决方案减少了所创建的代码量。 You can effortlessly leverage of DataAnnotations. 您可以毫不费力地利用DataAnnotations。 The downside is that you need to adjust app.config file on client side manually. 缺点是您需要在客户端手动调整app.config文件。

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

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