By default a WCF Service uses the QueryStringConverter to convert query parameter into typed objects. To the according MSDN Documentation (see link above) we cann see, that it supports by default following parameter/object types:
Byte
SByte
Int16
Int32
Int64
UInt16
UInt32
UInt64
Single
Double
Char
Decimal
Boolean
String
DateTime
TimeSpan
Guid
Byte array
Uri
Object
DateTimeOffset
Enums
Types that have a TypeConverterAttribute that can convert the type to and from a string representation
Hell, these are really lot of types which can be converted - but what if we like to have additional? Let’s guess we like to have Nullable types like DateTime? to make some sort of optional parameter. As the Nullable type is a build in framework type we do not have a chance to decorate it with a TypeConverterAttribute.aspx).
Writing our own QueryStringConverter to the rescue… The task is really simple - derive from QueryStringConverter, override 3 functions and fill in some rules:
As you can see this isn’t such a big task. Noticable (and this is why I chose DateTime) is the convention I used to convert DateTime into the RFC1123 pattern format. The client side has to do as well to circumvent any problems in converting from and to DateTime. You can read more about Standard Date and Time Format Strings at the MSDN Documentation.
As we have our very own QueryStringConverter (extending the default) supporting Nullable types now we need to tell our WCF Service to use this instead of the default. Therefore we need 2 additional classes to configure a custom behavior which uses our implementation. Theses two classes are a custom WebHttpBehavior and a custom BehaviorExtensionElement.
As you can see the custom behavior wires are implementation of the QueryStringConverter to our service. Next is the extension element to extend the service to use the custom behavoir.