0

我有以下代码和网络方法:

public class RaumklassenHelper
{
   internal static Array Raumklasse()
   {
       List<object> raumKlassenObject = new List<object>();

       using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
       using (SqlCommand cmd = new SqlCommand(@"SELECT BEZEICHNUNG, BEZEICHNUNG_EN FROM RAUMKLASSE", con))
       {
           con.Open();
           using (SqlDataReader rdr = cmd.ExecuteReader())
           {
               while (rdr.Read())
               {
                   if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["BEZEICHNUNG_EN"] != DBNull.Value)
                   {
                       raumKlassenObject.Add(new
                           {
                               Name = rdr["BEZEICHNUNG"].ToString(),
                               Name_En = rdr["BEZEICHNUNG_EN"].ToString()
                           });
                   }
               }
           }
       }
       return raumKlassenObject.ToArray();
   }
}



 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
   [WebMethod]
   public Array Raumklasse()
   {
       return RaumklassenHelper.Raumklasse();
   }

如果我尝试调用该方法,则会收到错误消息:

这是第一次出现错误。

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: &lt;&gt;f__AnonymousType0`2[System.String,System.String] cannot be serialized because it does not have a parameterless constructor.
   at System.Xml.Serialization.TypeDesc.CheckSupported()
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_ArrayOfAnyType(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()
4

1 回答 1

5

尝试使用 object[] 作为返回类型而不是 Array


对于另一个问题:

    public class RaumklassenHelper
{

        public class RAUMKLASSE
        {
            public string Name { get; set; }
            public string Name_En { get; set; }
        }


        internal static List<RAUMKLASSE> Raumklasse()
   {
       List<RAUMKLASSE> raumKlassenObject = new List<RAUMKLASSE>();

       using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
       using (SqlCommand cmd = new SqlCommand(@"SELECT BEZEICHNUNG, BEZEICHNUNG_EN FROM RAUMKLASSE", con))
       {
           con.Open();
           using (SqlDataReader rdr = cmd.ExecuteReader())
           {
               while (rdr.Read())
               {
                   if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["BEZEICHNUNG_EN"] != DBNull.Value)
                   {
                       raumKlassenObject.Add(new RAUMKLASSE()
                           {
                               Name = rdr["BEZEICHNUNG"].ToString(),
                               Name_En = rdr["BEZEICHNUNG_EN"].ToString()
                           });
                   }
               }
           }
       }
       return raumKlassenObject;
   }
}



 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
   [WebMethod]
   public List<RAUMKLASSE> Raumklasse()
   {
       return RaumklassenHelper.Raumklasse();
   }
于 2012-06-06T08:49:39.360 回答