Web服务直接从浏览器访问时出错

时间:2011-07-15 12:06:28

标签: .net asp.net vb.net .net-3.5 asmx

我有以下网络服务,只要从客户端脚本(javascript,jquery)访问它就可以正常工作

<%@ WebService Language="VB" Class="WebService" %>

Imports System.Web.Script.Services
Imports System.Web.Services

<ScriptService()> _
Public Class WebService
    Inherits System.Web.Services.WebService

    Public Class Person
        Public FirstName As String
        Public LastName As String

        Public Sub New(ByVal m_FirstName As String, ByVal m_LastName As String)
            FirstName = m_FirstName
            LastName = m_LastName
        End Sub
    End Class

    <WebMethod()> _
    Public Function GetPersons() As List(Of Person)
        Dim People As List(Of Person) = New List(Of Person)

        People.Add(New Person("firstname_1", "surname_1"))
        People.Add(New Person("firstname_2", "surname_2"))

        Return People
    End Function
End Class

但是出于某种原因,如果我尝试直接在浏览器中访问它,它会给我带来错误

http://localhost/webservices/WebService.asmx
http://localhost/webservices/WebService.asmx/GetPersons

查看显示所有支持的操作的标准Web服务服务描述

我得到的错误是:

WebService.Person cannot be serialized because it does not have a parameterless constructor.

1 个答案:

答案 0 :(得分:1)

为您的人员添加无参数构造函数

  Public Class Person
        Public FirstName As String
        Public LastName As String

        Public Sub New()
          'just add this constructor  
        End Sub
    End Class