我可以在自动生成的 WCF 类中包含自定义属性吗?

问题描述:

我使用 WSDL-first 方法来生成 Web 服务.基本上,这意味着,我有一个我希望我的服务外观的手工版本,然后使用 svcutil 生成 WCF 服务.

I have a WSDL-fisrt approach to generating Web Services. Basically, this means, I have a hand-crafted version of how I want my service to look, and then generate the WCF service by using svcutil.

此 WSDL 包含许多有用的信息,例如 minOccurs、maxOccurs、限制 (regexp) 等.但是,当我获得自动生成的类时,这些信息完全丢失了.

This WSDL contains a lot of useful information such as minOccurs, maxOccurs, restrictions (regexp), etc. However, this information is completely lost when I get my auto-generated classes.

有什么方法可以在属性的注释(无论是标准的".net 提供的还是自定义的)中捕获这些信息,以便在对象反序列化后对它们执行验证?

Is there any way I can capture this information in annotations (whether "standard" .net provided, or custom) on the properties so that I can perform validation on the objects after they've been deserialized?

注意:我知道您可以通过服务行为和消息检查器对传入流执行验证,但我正在寻找一种解决方案,在该解决方案中,我可以对生成的代码执行验证,而无需编写我已经编写的复杂验证规则已经在别处定义了.(即,我不想重复)

Note: I know you can perform validation on the incoming stream through service behaviors and message inspectors, but I'm looking for a solution where I can perform validation on the generated code without having to write complex validation rules that I've already defined elsewhere. (i.e., I don't want duplication)

是的,您可以将 WSDL 中的任何信息转换为代码,但不一定使用 svcutil.如果您遇到 svcutil 支持的限制,您可能需要考虑另一种代码生成机制,例如 T4 模板.

Yes, you can translate any information from the WSDL into code, but not necessarily using svcutil. If you're running up against the limitations of what svcutil supports, you might want to consider another code generation mechanism, such as T4 templates.