Reputation: 3310
I am trying to replicate a webservice provided by a company which I am trying to use for my testing environment.
The original webservice returns the data like:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDoAccountMasters xmlns="http://tempuri.org/">
<doAccountMasters>
<MasterNum>int</MasterNum>
<MasterName>string</MasterName>
<ErrorCode>int</ErrorCode>
<ErrorDescription>string</ErrorDescription>
</doAccountMasters>
<doAccountMasters>
<MasterNum>int</MasterNum>
<MasterName>string</MasterName>
<ErrorCode>int</ErrorCode>
<ErrorDescription>string</ErrorDescription>
</doAccountMasters>
</ArrayOfDoAccountMasters>
At the moment I have the following function:
<WebMethod(Description:="Returns Array 5 IDs < x", EnableSession:=True)> _
Public Function GetCustomerIDs(ID As String) As List(Of String)
Dim output As New List(Of String)()
Dim sqlCommand As SqlCommand = New SqlCommand( _
"select top 5 p.MasterNum, p.MasterName, p.ErrorCode, p.ErrorDescription from person.person p where BusinessEntityID<" & ID, connection)
connection.Open()
Dim dr = SqlCommand.ExecuteReader()
While dr.Read
output.Add(dr.Item(0))
End While
connection.Close()
Return output
End Function
which returns the following:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns="http://tempuri.org/">
<string>string</string>
<string>string</string>
<string>string</string>
<string>string</string>
</ArrayOfString>
Apparently, my method doesn't return the data in the same format. How can I modify the code to make it look the same as their method? How do I need to format my return data?
Upvotes: 0
Views: 122
Reputation: 28530
Your method is returning exactly what you told it to - a List of Strings. The service you're attempting to imitate is returning a List of doAccountMasters. Your method needs to return a List of doAccountMasters to get an output like the original service.
I strongly recommend that you use WCF for this, as ASMX web services are legacy technology. In WCF you could use a DataContract
for the doAccountMasters
, and then the normal service interface and class that implements the interface. It would look something like this (There is more to WCF than just the below parts - you'll have to host the service in IIS or some other hosting solution):
<DataContract()>
Public Class doAccountMasters
<DataMember()>
Public Property MasterNum As Integer
<DataMember()>
Public Property MasterName As String
<DataMember()>
Public Property ErrorCode As Integer
<DataMember()>
Public Property ErrorDescription As String
EndClass
The Interface:
<ServiceContract()>
Public Interface IMyService
<OperationContract()>
Function GetCustomerIDs(ID As String) As List(Of doAccountMasters)
End Interface
The Service Implementation:
Public Class MyService
Implements IMyService
Public Function GetCustomerIDs(ID As String) As List(Of doAccountMasters)
Dim output As New List(Of doAccountMasters)()
Dim sqlCommand As SqlCommand = New SqlCommand( _
"select top 5 p.MasterNum, p.MasterName, p.ErrorCode, p.ErrorDescription from person.person p where BusinessEntityID<@ID", connection)
connection.Open()
sqlCommand.Paramters.Add("@ID", SqlDbType.Int, ID)
Dim dr = SqlCommand.ExecuteReader()
While dr.Read
Dim master As doAccountMasters = New doAccountMasters()
master.MasterNum = Convert.ToInt32(dr.Item(0))
master.MasterName = dr.Item(1).ToString()
master.ErrorCode = Convert.ToInt32(dr.Item(2))
master.ErrorDescription = dr.Item(3).ToString()
output.Add(master)
End While
connection.Close()
Return output
End Function
End Class
If you must stick with .ASMX, you could use the same class (minus the DataContract
and DataMember
attributes) and the same logic:
Public Class doAccountMasters
Public Property MasterNum As Integer
Public Property MasterName As String
Public Property ErrorCode As Integer
Public Property ErrorDescription As String
End Class
<WebMethod(Description:="Returns Array 5 IDs < x", EnableSession:=True)> _
Public Function GetCustomerIDs(ID As String) As List(Of doAccountMasters)
Dim output As New List(Of doAccountMasters)()
Dim sqlCommand As SqlCommand = New SqlCommand( _
"select top 5 p.MasterNum, p.MasterName, p.ErrorCode, p.ErrorDescription from person.person p where BusinessEntityID<@ID", connection)
connection.Open()
sqlCommand.Parameters.Add("@ID", SqlDbType.Int, ID)
Dim dr = SqlCommand.ExecuteReader()
While dr.Read
Dim master As doAccountMasters = New doAccountMasters()
master.MasterNum = Convert.ToInt32(dr.Item(0))
master.MasterName = dr.Item(1).ToString()
master.ErrorCode = Convert.ToInt32(dr.Item(2))
master.ErrorDescription = dr.Item(3).ToString()
output.Add(master)
End While
connection.Close()
Return output
End Function
The above codes also utilize parameterized queries, and I'm making a guess that ID
is an integer based on the SQL command you have.
Upvotes: 1