调用Web服务时出现InvalidCastException

时间:2013-05-20 17:51:22

标签: vb.net web-services asmx

将数据从我的应用程序传递到ASMX webservice

时,总是会出现转换错误

我的WebService代码

   Public Function SetAlterLogTrx(ByVal qsTrx As List(Of String)) As String
    Dim oStatus As New LogAlterStatusDsp

    Dim iRec As Integer = 0
    Using DBCONN As New SqlConnection()
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("Conn").ConnectionString
        DBCONN.ConnectionString = sDBConnString

        If qsTrx.Count = 0 Then
            Return "Failed"
        Else
            Dim sQueryList As Array = qsTrx.ToArray
            For Each sQuery As String In sQueryList
                Using UpdateOutCommand As New SqlCommand(sQuery, DBCONN)
                    Try
                        With DBCONN
                            .Open()
                            iRec = UpdateOutCommand.ExecuteNonQuery()
                            .Close()
                        End With
                    Catch ex As Exception
                        Return "Failed"
                    End Try
                End Using
            Next
            Return "Ok"
        End If
    End Using

End Function

我的客户代码

    dim qsArray() as string
    'This array has many lines
    Using oSvc As New AnfaEngine.AnfaWSSoapClient
        Dim svcReplay As AnfaEngine.LogAlterStatusDsp
        Dim oList As New List(Of String)
        oList.AddRange(qsArray.Cast(Of String).ToList)
        svcReplay = oSvc.SetAlterLogTrx(oList)
    End Using

我总是收到此错误消息:

无法将'System.Collections.Generic.List`1 [System.String]'类型的对象强制转换为'WS.ArrayOfString'。

我该怎么做才能解决这个问题。

2 个答案:

答案 0 :(得分:0)

使用“添加服务引用”引用服务时,请尝试使用“高级”选项卡指定使用List(Of T)作为集合类型。

如果这不起作用,那么您只需向服务发送它想要接收的内容。

答案 1 :(得分:-1)

我在客户端代码中解决了我的问题,如下所示:

 dim qsArray() as string
'This array has many lines
Using oSvc As New AnfaEngine.AnfaWSSoapClient
    Dim svcReplay As AnfaEngine.LogAlterStatusDsp
    Dim oList As New List(Of AnfaEngine.ArrayOfString)
    oList.AddRange(qsArray.Cast(Of String).ToList)
    svcReplay = oSvc.SetAlterLogTrx(oList)
End Using