Tuesday, April 1, 2008
Serializing a data contract in WCF
Imports System.Runtime.Serialization
Public Shared Function DeserializeObject(ByRef xmlizedString As String, ByRef objectType As Type) As Object
Dim memoryStream As New MemoryStream(StringToUTF8ByteArray(xmlizedString))
Dim dcs As New DataContractSerializer(objectType)
Return Convert.ChangeType(dcs.ReadObject(memoryStream), objectType)
End Function
Public Shared Function SerializeObject(ByRef xmlizableObject As Object, ByRef objectType As Type) As Object
Dim memoryStream As New MemoryStream()
Dim dcs As New DataContractSerializer(objectType)
dcs.WriteObject(memoryStream, xmlizableObject)
Return UTF8ByteArrayToString(memoryStream.ToArray())
End Function
sample code fragment to use the method :
dim student a new Student
dim studentData as string
studentData =SerializeObject(student , GetType(Student))
Tuesday, February 5, 2008
Method Overloading in WCF
First, let’s create a simple service contract with 2 overloaded function, something like the following,
Public Interface IService1
Function GetData(ByVal intParam As Integer) As String
Function GetData(ByVal intParam As Integer, ByVal strParam As String) As String
End Interface
Server Error in '/FundTransferService' Application.
Cannot have two operations in the same contract with the same name, methods GetData and GetData in type IService1 violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.
We will add a “Name” property to the “OperationContract” attribute with different value,like the following.
Public Interface IService1
Function GetData(ByVal intParam As Integer) As String
Function GetData(ByVal intParam As Integer, ByVal strParam As String) As String
End Interface
Dim resultInt As String = sev1.GetDataInt(5)
Dim resultString As String = sev1.GetDataString(5, "Five")
sev1.Close()
We will need to modify the client proxy class as well to fully implent the function overloading .
At the client side in the Reference.vb file, we have to add the same “Name” property to that OperationContract attribute in the Interface “IService1” and change the 2 method name to “GetData”.See below,
System.ServiceModel.ServiceContractAttribute(ConfigurationName:="Service1.IService1")>
Public Interface IService1
Function GetData(ByVal intParam As Integer) As String
End Interface
Now we will change the implimentation of that interface to make the 2 method of same method name. It is in the same Reference.VB class.
public Function GetData(ByVal intParam As Integer) As String Implements Service1.IService1.GetData
Return MyBase.Channel.GetData(intParam)
End Function
Public Function GetData(ByVal intParam As Integer, ByVal strParam As String) As String Implements Service1.IService1.GetData
Return MyBase.Channel.GetData(intParam, strParam)
End Function
Dim resultInt As String = sev1.GetData(5)
Dim resultString As String = sev1.GetData(5, "Five")
sev1.Close()