How To: QueryStringConverter in WCF Using VB.Net

In this blog, we can discuss on how to use a QueryStringConverter class using a simple VB.Net application.

1. Lets start by defining the service contract method in the interface

<WebGet(BodyStyle:=WebMessageBodyStyle.Bare,RequestFormat:=WebMessageFormat.Xml, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="WCFService?SumA={SumA}")>
Function WCFservice(ByVal SumA() As Integer) As Integer

2. Once the interface is defined, we can implement the interface and add code to the method.

Public Function WCFService(ByVal sumA() As Integer) As Integer Implements WCFRestful.WCFservice
Dim sum As Integer
For Each Totalvalue As Integer In sumA
sum += Totalvalue
Return sum
End Function

QueryStringConverter is a feature in .Net framework, that helps convert the query string values to a desired type or value between operations . We will use this feature in this article to convert the string values into an integer array.
Now let us create a custom class to perform this functionality by inheriting QueryStringConverter class.

Public Class StringArrayQueryStringConverter Inherits QueryStringConverter
Public Overloads Function ConvertStringToValue(sumA As String, parameterType As Type) As Object
Dim converter As New QueryStringConverter()
If (converter.CanConvert(GetType(Int32))) Then
converter.ConvertStringToValue(sumA, GetType(Int32))
End If
If (parameterType.IsArray) Then
Dim MemeberList As String() = sumA.Split(",")
Dim result As Array = Array.CreateInstance(parameterType, MemeberList.Length)
For i As Integer = 0 To MemeberList.Length - 1
result.SetValue(MyBase.ConvertStringToValue(MemeberList(i), parameterType), i)
Return result
End If
Return MyBase.ConvertStringToValue(sumA, parameterType)
End Function
End Class

3. Create a subclass WebHttpBehavior to replace the converter

In order to use a custom QueryStringConverter, we would have to create a custom WebHttpBehavior and WebHttpBehaviorExtension class respectively.
WCFWebHttpBehavior class that will return the instance of StringArrayQueryStringConverter using GetQueryStringConverter method.

Public Class WCFWebHttpBehavior Inherits WebHttpBehavior
Protected Overrides Function GetQueryStringConverter(operationDescription As OperationDescription) As QueryStringConverter
Return New StringArrayQueryStringConverter
End Function

Protected Overrides Function GetRequestDispatchFormatter(operationDescription As OperationDescription, endpoint As ServiceEndpoint) As IDispatchMessageFormatter
Return New MyArrayAwareFormatter(operationDescription, GetQueryStringConverter(operationDescription))
End Function

Private Class MyArrayAwareFormatter Implements IdispatchMessageFormatter
Private operation As OperationDescription
Private querystringconverter As QueryStringConverter
Public Sub New(operation As OperationDescription, queryStringConverter As QueryStringConverter)
Me.operation = operation
Me.queryStringConverter = queryStringConverter
End Sub
Public Sub DeserializeRequest(message As Channels.Message, parameters() As Object) Implements IdispatchMessageFormatter.DeserializeRequest
If message.Properties.ContainsKey(“UriMatched”) AndAlso
Cbool(message.Properties(“UriMatched”)) Then
Dim match As UriTemplateMatch = TryCast(message.Properties(“UriTemplateMatchResults”), UriTemplateMatch)
Dim queryValues As NameValueCollection = match.QueryParameters
For Each parameterDescr As MessagePartDescription In Me.operation.Messages(0).Body.Parts
Dim parameterName As String = parameterDescr.Name
Dim index As Integer = parameterDescr.Index
If parameterDescr.Type.IsArray Then
Dim elementType As Type = parameterDescr.Type.GetElementType()
Dim values As String() = queryValues.GetValues(parameterName & “[]”)
Dim array__1 As Array = Array.CreateInstance(elementType, values.Length)
For I As Integer = 0 To values.Length – 1
array__1.SetValue(Me.querystringconverter.ConvertStringToValue(values(i), elementType), i)
parameters(index) = array__1
parameters(index) = Me.querystringconverter.ConvertStringToValue(queryValues.GetValues(parameterName)(0), parameterDescr.Type)
End If
End If
End Sub
Public Function SerializeReply(messageVersion As Channels.MessageVersion, parameters() As Object, result As Object) As Channels.Message Implements IdispatchMessageFormatter.SerializeReply
Throw New NotSupportedException(“This is a request-only formatter”)
End Function
End Class

Let’s create a WebHttpBehaviorExtension class inheriting BehaviorExtensionElement
BehaviorExtensionElement has two members BehaviorType and CreateBehavior of subclasses which has to be overridden in a subclass.

Public Class WCFWebHttpBehaviorExtension Inherits BehaviorExtensionElement
Public Overrides ReadOnly Property BehaviorType As Type
Dim behaviour As New WCFWebHttpBehavior
Return behaviour.GetType()
End Get
End Property
Protected Overrides Function CreateBehavior() As Object
Return (New WCFWebHttpBehavior)
End Function
End Class

4. Result in XML
Now, it is ready to launch the application in the browser.


Thoughts on “How To: QueryStringConverter in WCF Using VB.Net”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Manju Kumaravel

More from this Author

Follow Us