简体   繁体   English

访问Web服务时WPF应用程序中的SOAP异常

[英]SOAP exception in WPF Application while accessing Web Service

I am learning C#, and in one of the assignment i have to display the data from an Access file into a DataGrid upon button click using a WPF Application, and a web reference. 我正在学习C#,在一项任务中,我必须使用WPF应用程序和Web参考在单击按钮时将Access文件中的数据显示到DataGrid中。

There is a SOAP Exception which i am not able to resolve, any help is appreciated, thanks in advance - 有一个SOAP异常,我无法解决,感谢您的帮助,在此先感谢-

Web reference asmx file contains the below method, that is called upon button click - Web参考asmx文件包含以下方法,该方法在单击按钮时调用-

[WebMethod]
    public ArrayList queryDB()
    {
        string selectStmt = "select * from Students";
        OleDbConnection conn_obj = new DBConnect().checkDBStatus(databaseLocation);
        conn_obj.Open();

        OleDbCommand query = new OleDbCommand(selectStmt, conn_obj);
        OleDbDataReader dbReader = query.ExecuteReader();

        ArrayList dbRecordList = new ArrayList();

        foreach (DbDataRecord dbRecord in dbReader)
        {
            dbRecordList.Add(dbRecord);
        }

        conn_obj.Close();
        return dbRecordList;
    }

WPF file code is as below - "Button click method" WPF文件代码如下-“按钮单击方法”

    private void GetData_Click(object sender, RoutedEventArgs e)
    {
        ArrayList datagridList = (ArrayList) websrvc.queryDB();
        dataGrid.ItemsSource = datagridList;
    }

The above code is creating an error - Cannot implicitly convert type 'object[]' to 'System.Collections.ArrayList' 上面的代码创建了一个错误- 无法将类型'object []'隐式转换为'System.Collections.ArrayList'

Hence based on a suggestion over web, changed the code as below - 因此,根据网上的建议,更改了以下代码-

private void GetData_Click(object sender, RoutedEventArgs e)
    {
        object obj = websrvc.queryDB();
        ArrayList datagridList = (ArrayList) obj;
        dataGrid.ItemsSource = datagridList;
    }

Now, there seems to be no errors, but an exception at the below line of code, which i am not able to solve - 现在,似乎没有错误,但是下面的代码行有一个异常,我无法解决-

object obj = websrvc.queryDB();

{"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: System.Data.Common.DataRecordInternal cannot be serialized because it does not have a parameterless constructor.\\n at System.Xml.Serialization.TypeDesc.CheckSupported()\\n at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)\\n at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)\\n at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)\\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)\\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_queryDBResponse(Object[] p)\\n at Mic {“ System.Web.Services.Protocols.SoapException:服务器无法处理请求。---> System.InvalidOperationException:生成XML文档时出错。---> System.InvalidOperationException:System.Data.Common。 DataRecordInternal无法序列化,因为它没有无参数的构造函数。\\ n在System.Xml.Serialization.TypeDesc.CheckSupported()\\ n在System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,MemberInfo source,Boolean directReference,Boolean System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(类型类型)\\ n在System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(字符串名称,字符串ns,对象o,布尔值xsiType)\\ n在Microsoft.Xml。 Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n,String ns,Object o,Boolean isNullable,Boolean NeedType)\\ n在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_queryDBResponse(Object [] p)\\ n rosoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)\\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\\n --- End of inner exception stack trace ---\\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\\n at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)\\n at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)\\n at System.Web.Services.Protocols.WebServiceHandler.Invoke()\\n --- End of inner exception stack trace ---"} rosoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(Object objectToSerialize,XmlSerializationWriter writer)\\ n在System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,Object o,XmlSerializerNamespaces) -内部异常堆栈跟踪的结尾--- \\ n在System.Web.Services.Protocols.SoapServerProtocol处(XmlWriter xmlWriter,对象o,XmlSerializerNamespaces命名空间,字符串encodingStyle,字符串ID)\\ n .WriteReturns(Object [] returnValues,Stream outputStream)\\ n在System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object [] returnValues)\\ n在System.Web.Services.Protocols.WebServiceHandler.Invoke()\\ n- -内部异常堆栈跟踪结束---“}

  1. Create a class on web service side named Student with all the properties that you want to send to client. 在Web服务端创建一个名为Student的类,其中包含您要发送给客户端的所有属性。
  2. Mark this class as serializable. 将此类标记为可序列化。
  3. Map the DB object properties one by one to this Student class. 将DB对象属性一个一映射到此Student类。
  4. From the service, instead of returning ArrayList, return ArrayList(); 从服务中,而不是返回ArrayList,而是返回ArrayList();。

Let me know if this works. 让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM