索引超出范围 - 通过.NET在WCF上传递列表值

时间:2017-02-01 19:16:50

标签: arrays vb.net wcf

我在VB.NET中有以下代码:
我的合同:

 <ServiceContract(Namespace:=Constants.GlobalNamespace, Name:=Constants.Name)>
Public Interface IMyCoolInterface

    <OperationContract()>
    Function fetchDataBaseNameByParam(QueryInfo As Database) As Database

End Interface

<DataContract(Name:="DatabaseNameContract", Namespace:=Constants.GlobalNamespace), KnownType(GetType(List(Of String)))>
Public Class Database

    <DataMember(Name:="ArrayOfParams")>
    Public ListOfParams As List(Of String)
    <DataMember(Name:="DatabaseNames")>
    Public DatabaseNames As ArrayList

End Class

实施

<ServiceBehavior(Namespace:=Constants.GlobalNamespace, Name:=Constants.Name, ConfigurationName:="GetCatalogDBC", IncludeExceptionDetailInFaults:=True)>
Public Class MyCoolClass
    Implements IMyCoolInterface
    Private conString As String = "Data Source=myDBServer;User ID=user;Password=pass;"
    Property connectionString() As String
        Get
            Return conString
        End Get
        Set(ByVal Value As String)
            conString = Value
        End Set
    End Property
    Private queryString As String
    Property Query() As String
        Get
            Return queryString
        End Get
        Set(ByVal Value As String)
            queryString = Value
        End Set
    End Property
    Public Sub New()
    End Sub

    Public Function fetchDataBaseNameByParam(ByVal QueryInfo As Database) As Database Implements IMyCoolInterface.fetchDataBaseNameByParam
        Dim myConnection As TdConnection = New TdConnection()
        myConnection.ConnectionString = Me.connectionString()
        Dim myCommand As TdCommand = myConnection.CreateCommand()
        Me.Query() = "SELECT DatabaseName FROM DBC.Databases WHERE UPPER(RTRIM(DatabaseName)) like any( '" + QueryInfo.ListOfParams.Item(0).ToString + "%_T') group by DatabaseName;"
        myCommand.CommandText = Me.Query()
        Dim myReader As TdDataReader
        Try
            myConnection.Open()
            myReader = myCommand.ExecuteReader()
            While myReader.Read()
                QueryInfo.DatabaseNames.Add(myReader.Item("DatabaseName").ToString)
            End While
            myReader.Close()
        Catch ex As Exception
            Console.WriteLine(ex.StackTrace)
        End Try
        Try
            myConnection.Close()
        Catch ex As Exception
            Console.WriteLine(ex.StackTrace)
        End Try
        Return QueryInfo
    End Function

运行此命令会生成以下SOAP请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:get="http://iis-mrd.pncbank.com/GetTeradataDBC" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
   <soapenv:Header/>
   <soapenv:Body>
      <get:fetchDataBaseNameByParam>
         <!--Optional:-->
         <get:QueryInfo>
            <!--Optional:-->
            <get:ArrayOfParams>
               <arr:string>?</arr:string>
            </get:ArrayOfParams>
            <get:DatabaseNames>
               <!--Zero or more repetitions:-->
               <arr:anyType>?</arr:anyType>
            </get:DatabaseNames>
         </get:QueryInfo>
      </get:fetchDataBaseNameByParam>
   </soapenv:Body>
</soapenv:Envelope>

但是当我尝试发送像<arr:string>PARAM</arr:string>这样的参数时, 我得到以下错误,好像列表是空的:

<Message>Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index</Message>

我能够通过从List(of String)更改为String 来获得成功的响应,但我稍后会发送多个值,因此我需要某种数组..任何帮助?

1 个答案:

答案 0 :(得分:0)

通过创建包装类

来解决