当我使用对象作为参数时,Web服务命中错误

时间:2016-06-09 03:46:54

标签: c# json web-services

我已经创建了一个Web服务,在Web服务中,我得到了以json web格式返回的web函数,下面的代码是示例。

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
Public Sub getReligion()
    Dim ObjDBConn As SqlConnection
    Dim ObjReader As SqlDataReader
    Dim ObjAdapter As SqlDataAdapter
    ObjDBConn = ObjWebLiteSvc.InitConsoleDB
    Dim sb As StringBuilder = New StringBuilder()
    Dim religionArray As JArray = New JArray()
    Dim arrResponse As JObject = New JObject()

    Try
        ObjDBConn.Open()
        ObjCmd = New SqlCommand("SELECT Description from dbo.RELIGION", ObjDBConn)
        ObjAdapter = New SqlDataAdapter()
        ObjAdapter.SelectCommand = ObjCmd
        Dim data As DataTable = New DataTable()
        ObjAdapter.Fill(data)

        Dim religionList(data.Rows.Count) As String

        For i = 0 To data.Rows.Count - 1
            religionArray.Add(data.Rows(i).Item("Description").ToString())
        Next

        arrResponse("Status") = "1"
        arrResponse("List") = religionArray
    Catch ex As Exception
        arrResponse("Status") = "0"
        arrResponse("Error") = ex.ToString()
    Finally
        sb.Append("(")
        sb.Append(arrResponse.ToString)
        sb.Append(");")
        Context.Response.Clear()
        Context.Response.ContentType = "application/json"
        Context.Response.Write(sb.ToString)
        Context.Response.End()
        ObjDBConn.Close()
        ObjDBConn.Dispose()
    End Try
End Sub

但是,在另一个函数中,我创建了另一个不使用json格式作为返回格式的Web函数。我认为这很好,但问题是,当我使用对象作为参数时,当我调用getReligion函数时,它肯定会遇到一个奇怪的错误

Public Function RegisterMembership(ByVal membershipList As Object) As Integer
        Return 1
    End Function



System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

最后,在我将输入参数更改为字符串后,getreligion函数正常工作。当我在另一个函数上使用json返回对象时,如何允许插入object参数?

0 个答案:

没有答案
相关问题