Yossi Dahan [BizTalk]

Google
 

Sunday, February 11, 2007

AnonymousTypes and serialization

In my current project we have to deal a lot of serialization of deserialization of objects; mostly due to the fact that we have a common object model accross most of our services and that mostly it is these objects that gets passed between web services in our solution.

In fact, almost every object we have is represented by both a .net class and a BizTalk schema and for that reason we have decorated most of our classes with serilization attributes to control the format of the xml generated.

As you all know, when objects are used in exposed web services, a schema that represents them is generated and embedded in the WSDL file, from which proxy classes will be generated in consuming applications.

A while ago I’ve noticed that in most of our classes have within the xml serialization related attributes the following attribute –

System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)

Hvaing this instructs the the schema generator to avoid treating this class/type as a global type which prevents re-use.

To demonsrate the issue consider the following –

I have a web method HelloWorld that takes in 1 parameter myenum of type MyEnum (which is an enumeration)


If the enum type has the discussed dattribute the wsdl is generated as the following –




Notice that that enumeration is a local definition of the element.

Without the attribute the wsdl is generated like this –



Notice that now the enum is declared globally and can be re-used across the methods.

I'm not sure why we had this in, I suspect, if I'm not mistaken, that the xsd.exe tool adds this attribute to classes generated from xml, and we've used this in many cases as our template.
Either way - I now try to avoid this attribute generally and only apply it when it makes sense. otherwise I have all the types declared as non anonymous which enables reuse.

Labels:

0 Comments:

Post a Comment

<< Home