WebService以某种格式返回数据

时间:2014-06-26 09:02:43

标签: c# asp.net vb.net web-services

我正在尝试复制我尝试用于测试环境的公司提供的Web服务。

原始web服务返回如下数据:

<?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>

目前我有以下功能:

<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

返回以下内容:

<?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>

显然,我的方法不会以相同的格式返回数据。如何修改代码使其看起来与方法相同?如何格式化返回数据?

1 个答案:

答案 0 :(得分:1)

您的方法正在返回您告诉它的内容 - 字符串列表。您尝试模仿的服务是返回doAccountMaster列表。您的方法需要返回一个doAccountMasters列表以获得与原始服务类似的输出。

我强烈建议您使用WCF,因为ASMX Web服务是传统技术。在WCF中,您可以使用DataContract作为doAccountMasters,然后使用正常的服务接口和实现接口的类。它看起来像这样(WCF不仅仅是以下部分 - 你必须在IIS或其他托管解决方案中托管服务):

<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

界面:

<ServiceContract()>
Public Interface IMyService

    <OperationContract()>
    Function GetCustomerIDs(ID As String) As List(Of doAccountMasters)
End Interface

服务实施:

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

如果你必须坚持.ASMX,你可以使用相同的类(减去DataContractDataMember属性)和相同的逻辑:

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

以上代码也使用参数化查询,我根据您的SQL命令猜测ID是一个整数。