开发者

xsd.exe - schema to class - for use with WCF

I have created a schema as an agreed upon interface between our company and an external company. I am now creating a WCF C# web service to handle the interface.

I ran the XSD utility and it created a C# class. The schema was built in BizTalk,开发者_运维问答 and references other schemas, so all-in-all there are over 15 classes being generated.

I put [DataContract} attribute in front of each of the classes. Do I have to put the [DataMember] attribute on every single property?

When I generate a test client program, the proxy does not have any code for any of these 15 classes.

We used to use this technique when using .asmx services, but not sure if it will work the same with WCF. If we change the schema, we would want to regenerate the WCF class, and then we would haev to each time redecorate it with all the [DataMember] attributes? Is there an newer tool similar to XSD.exe that will work better with WCF?

Thanks,

Neal Walters

SOLUTION (buried in one of Saunders answer/comments):

Add the XmlSerializerFormat to the Interface definition:

    [OperationContract]
    [XmlSerializerFormat]     // ADD THIS LINE 
    Transaction SubmitTransaction(Transaction transactionIn);

Two notes: 1) After I did this, I saw a lot more .xsds in the my proxy (Service Reference) test client program, but I didn't see the new classes in my intellisense. 2) For some reason, until I did a build on the project, I didn't get all the classes in the intellisense (not sure why).


Neal, besides all the options John has given you, you should also check out the WCSF.blue tool on Codeplex: http://wscfblue.codeplex.com/

It's a "contract first" approach to doing WCF, and one of the many options it offers is to create a WCF DataContract file from your XSD:

xsd.exe - schema to class - for use with WCF

This then pops up a dialog in which you can set a slew of parameters on how to create your C# class file from the XSD:

xsd.exe - schema to class - for use with WCF

Quite useful, even if you want to use it for nothing more than converting XSD to C# classes that work as WCF DataContracts :-)

Also see this blog post for more explanations on the XSD DataContract generation process.


Classes that use [DataContract] will serialize to a very limited schema. For instance, there will be no attributes, only elements. This is intentional, and is done for performance and interoperability.

In general, a schema of the kind you may be working with may be much more complicated. Such a schema will not be usable with the Data Contract Serializer. You will need to use the XML Serializer instead.

In particular, don't edit the classes created by XSD.EXE. In fact, you should never edit generated code, as your edits will be removed as soon as the code is generated again. These classes should already have the attributes on them that will be needed for WCF to work with them.

What happens if you just use them as-is?


I'm shocked that no one pointed me to this utility, which I believe is what I was asking for:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.xsddatacontractimporter.aspx

I haven't tried it yet, but it looks like XsdDataContractImporter was what I was looking for, or the SVCUTIL with the /dataContractOnly flag.

I learned this when interviewing a candidate yesterday. She said DataContract serialization is faster and would be preferred to use.

Neal


Re: your XsdDataContract class mentioned in your followup, IMO, This is a niche class (as is ServiceContractGenerator etc) so i'm not surprised no one pointed you to it. You still need to package it up to make it really useful. I'm pretty sure that code sample covers only very basic use cases so I would expect to do a lot of work with that to get it to a proper working state. . And like i mentioned on the WSCF forum, Svcutil, wscf, xsd.exe and others all use this class and related classes in the code gen process.

Regards the serializer performance, there are really good posts on Youssef Moussaoui's blog on the different serializers and their performance especially http://blogs.msdn.com/youssefm/archive/2009/07/10/comparing-the-performance-of-net-serializers.aspx

There’s also a really good discussion here on Connect regarding the two serializers (XS and DCS) and a useful point that XS is still the way forward for WSDL/XSD First development as the DCS is only intended to support a simplified programming model and thus doesn’t support various xml constructs. http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=451277

Hope this helps, Cheers, Benjy

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜