开发者

Can I include custom attributes on auto-generated WCF classes?

I have a WS开发者_JS百科DL-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.

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.

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)


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.


By making the "minOccurs, maxOccurs, restrictions (regexp), etc" references in your question, it sounds like you're mainly concerned with serializing the wsdl:types element contents as data contract classes while maintaining the XSD metadata. If this is the case then you can try the XSD.EXE command line tool to generate the classes from the extracted XSDs from WSDL types. I don't know how the XSD utility enforces the XSD metadata/restrictions (if at all) but it will give you more control of the class structure than the DataContractSerializer based classes generated by SvcUtil.

UPDATE: The .NET XmlSerializer does support marking classes with minOccurs and other attributes but that functionality was part of the original ASMX based web services. I believe the XmlSerializer will still respect these attributes even though they were intended for ASMX use (YMMV).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜