Geeks With Blogs
mipsen

It happens once in a while (to me at least):

You get an XML schema and your task is to create a WCF service that accepts this data as parameter or uses it as return value.

So you generate a class from the schema using xsd.exe (like "xsd XMLSchema1.xsd /c /l:CS /n:mipsen.schemas"). You create your ServiceContract and implementation (adding ServiceKnownType to avoid funny behaviour concerning the schema-generated class), create the web.config and svc-file and host your service (in IIS, for example). You are done.

Then someone creates a reference to your WCF service to call it in his or her code. When using the generated class of the parameter or return value something happend to the names of the fields: they are all named [fieldname]field, like elementQfield, instead of just ElementQ. The slightly confused colleague comes over and asks what happend.

WCF uses the XmlObjectSerializer by default to handle serialization of the data contracts. If you use XML based data contracts you might consider using the XmlSerializer instead. The fields in the reference will be named as expected.

More information abot using XmlSerialzer can be found here

To tell WCF to use XmlSerializer you can apply the attribute [XmlSerializerFormat] to the operation or even the service in the interface.

Example:

[ServiceContract(Namespace="http://somenamespace.com/contracts")]   
    public interface ISchemaService
    {
        [OperationContract]
        [XmlSerializerFormat]
        void DoSomething(GeneratedType data);
    }

Why am I writing this? It might be a pretty obvious thing to WCF-experts. But when it first happend to me some time ago it took me a while searching the internet (without success) and trying different things until I found the right way...

 

Posted on Saturday, February 6, 2010 8:33 AM WCF | Back to top


Comments on this post: Funny *field Postfix in WCF Reference?

# re: Funny *field Postfix in WCF Reference?
Requesting Gravatar...
Thanks, it was very useful! Saved me few hours of research!
Left by Csaba on Feb 16, 2010 6:53 PM

# re: Funny *field Postfix in WCF Reference?
Requesting Gravatar...
Thanks a lot :)
Left by Ondřej on Apr 30, 2013 2:30 PM

Your comment:
 (will show your gravatar)


Copyright © mipsen | Powered by: GeeksWithBlogs.net