Welcome to MSDN Blogs Sign in | Join | Help

Differences in Guid Serialization

Why do the guids in my contract turn into strings when generating a client?

You're probably mixing different types of serializers between the client and service. There's nothing wrong with this and the generated client will work correctly but you don't get the user-friendly types. To see why, let's look at the metadata.

A guid in a contract with the DataContractSerializer generates a type in the http://schemas.microsoft.com/2003/10/Serialization/ namespace that looks like this:

<xs:element name="guid" nillable="true" type="tns:guid" />
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
</xs:restriction>
</xs:simpleType>

On the other hand, a guid in a contract with the XmlSerializer generates a type in the http://microsoft.com/wsdl/types/ namespace that looks like this:

<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
</xs:restriction>
</xs:simpleType>

These generated types are needed because a guid is not a primitive type. DataContractSerializer came after XmlSerializer so it recognizes both definitions but XmlSerializer has to rely on the schema when it sees a DataContractSerializer guid. Since the schema is based on a string type, the generated client field is a string. The same thing happens with other serializers that don't know how to map a particular schema pattern to a user-friendly type.

Next time: TCP Throttling

Published Thursday, January 31, 2008 5:00 AM by Nicholas Allen

Comments

Thursday, January 31, 2008 12:25 PM by Nicholas Allen's Indigo Blog

# Finding Data in Client Certificates

Can I pass additional user data, such as identity information, in a message secured with a client certificate?

Thursday, January 31, 2008 3:41 PM by kevinowen

# re: Differences in Guid Serialization

I think there is a problem with the xs:pattern used for the http://schemas.microsoft.com/2003/10/Serialization/:guid type. In XML Schema, the "\d" multi-character excape translates to "\p{Nd}", which matches any character with a Unicode General Category of "Nd". This is much more permissive than System.Guid. The problem with this is that the following are all valid guids according to the pattern, but none of them can be deserialized as System.Guid:

٠١٢٣٤٥٦٧-٨٩۰۱-۲۳۴۵-۶۷۸۹-०१२३४५६७८९০১

২৩৪৫৬৭৮৯-੦੧੨੩-੪੫੬੭-੮੯૦૧-૨૩૪૫૬૭૮૯୦୧୨୩

୪୫୬୭୮୯௦௧-௨௩௪௫-௬௭௮௯-౦౧౨౩-౪౫౬౭౮౯೦೧೨೩೪೫

೬೭೮೯൦൧൨൩-൪൫൬൭-൮൯๐๑-๒๓๔๕-๖๗๘๙໐໑໒໓໔໕໖໗

໘໙༠༡༢༣༤༥-༦༧༨༩-០១២៣-៤៥៦៧-៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙

Thursday, January 31, 2008 5:24 PM by Rory Primrose

# Correct Regular Expression For GUID's

Ever wondered what the correct format of a GUID value is? Ever needed a RegEx to validate a GUID as a

Friday, February 01, 2008 12:07 AM by Nicholas Allen

# re: Differences in Guid Serialization

Hi Kevin-

That's something I noticed as well.  I know why the code does what it does but I need to do some archeology to find out why that schema was picked.

Friday, February 01, 2008 11:54 AM by Jason Haley

# Interesting Finds: February 1, 2008

New Comments to this post are disabled
 
Page view tracker