简体   繁体   English

Web服务未处理的异常

[英]Web service unhandled exception

To use webservice I added a web service reference and then added the following code to my MainPage.xaml.cs file. 要使用Web服务,我添加了Web服务参考,然后将以下代码添加到MainPage.xaml.cs文件中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WebServiceTest
{
  public partial class MainPage : PhoneApplicationPage
  {
    // Constructor
    public MainPage()
    {
      InitializeComponent();
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
      try
      {
        ServiceReference.PDAServiceSoapClient ws = 
          new ServiceReference.PDAServiceSoapClient();
        ws.GetResoureAroudCompleted += 
          new EventHandler<ServiceReference.GetResoureAroudCompletedEventArgs>(ws_GetResoureAroudCompleted);

        ws.GetResoureAroudAsync("基站,机楼", 113, 23, 10000);
      }
      catch
      {
        System.Windows.MessageBox.Show("error!");
      }
    }

    void ws_GetResoureAroudCompleted(object sender, ServiceReference.GetResoureAroudCompletedEventArgs e)
    {
      if (e.Error != null)
      {
        var result = e.Result;
      }
    }
  }
}

Then I ran PhoneApplication and got this exception: 然后我运行PhoneApplication并得到以下异常:

System.InvalidOperationException was unhandled Message=There was an error reflecting type 'WebServiceTest.ServiceReference.GetResoureAroudResponseGetResoureAroudResult'. 未处理System.InvalidOperationException。Message =发生错误,反映了类型'WebServiceTest.ServiceReference.GetResoureAroudResponseGetResoureAroudResult'。 InnerException: System.InvalidOperationException Message=There was an error reflecting property 'Any1'. InnerException:System.InvalidOperationException Message =存在错误,反映了属性“ Any1”。

in Reference.cs Code: 在Reference.cs代码中:

public WebServiceTest.ServiceReference.GetResoureAroudResponseGetResoureAroudResult EndGetResoureAroud(System.IAsyncResult result) {
                object[] _args = new object[0];
                **WebServiceTest.ServiceReference.GetResoureAroudResponseGetResoureAroudResult _result = ((WebServiceTest.ServiceReference.GetResoureAroudResponseGetResoureAroudResult)(base.EndInvoke("GetResoureAroud", _args, result)));**
                return _result;
            }

It's not caught by try-catch, anybody know why? 它不会被try-catch捕获,有人知道为什么吗?

I have faced the same problem and after investigating I did the following which solved the issue: 我曾经遇到过同样的问题,经过调查后,我做了以下工作来解决了这个问题:

usually you will find two properties in the class causing the error: 通常,您会在类中找到两个导致错误的属性:

private System.Xml.Linq.XElement[] anyField;

private System.Xml.Linq.XElement any1Field;

What I did was the following: 我所做的如下:

1- change the first property from an array to a single value variable as follows 1-将第一个属性从数组更改为单个值变量,如下所示

private System.Xml.Linq.XElement anyField;

2- change the getter and setter methods of this property , to match your changes 2-更改此属性的getter和setter方法,以匹配您的更改

[System.Xml.Serialization.XmlAnyElementAttribute(Namespace="http://www.w3.org/2001/XMLSchema", Order=0)]
public System.Xml.Linq.XElement Any {
    get {
        return this.anyField;
    }
    set {
        this.anyField = value;
        this.RaisePropertyChanged("Any");
    }
}

3- remove or comment out the second property 3-删除或注释掉第二个属性

// private System.Xml.Linq.XElement any1Field;

4- remove or comment out the second property's getter and setter methods 4-删除或注释掉第二个属性的getter和setter方法

/*   
[System.Xml.Serialization.XmlAnyElementAttribute(Namespace="urn:schemas-microsoft-com:xml-diffgram-v1", Order=1)] 
public System.Xml.Linq.XElement Any1 { 
    get { 
        return this.any1Field; 
    } 
    set { 
        this.any1Field = value; 
        this.RaisePropertyChanged("Any1"); 
    } 
}   
*/

At this point you can now access the resulting XML as follows by calling the "Any" property which will return an xml which you can manipulate : 此时,您现在可以通过调用“ Any”属性来访问生成的XML,如下所示,该属性将返回一个您可以操作的xml:

ex, in my case it was the following class causing the problems 例如,在我的情况下,这是导致问题的以下类别

public partial class GetUserBalancesClassAccounts

in my method I was able to access the xml as follows 在我的方法中,我能够如下访问xml

GetUserBalancesClassAccounts accts = balances.Accounts;

XElement doc = accts.Any;

foreach( XElement docElement in doc.Elements()){

    foreach (XElement account in docElement.Elements("Account"))   
    {

    ... do something ...

    }    
}

The exception is not caught by your exception handler because it occurs in the framework and is out of scope. 异常不会被您的异常处理程序捕获,因为它发生在框架中并且不在范围内。 Depending on whether the webservice conforms to best practice, it may surface an Error object and if so you should inspect this prior to attempting to retrieve any data. 根据Web服务是否符合最佳实践,它可能会出现一个Error对象,如果是,则应在尝试检索任何数据之前检查该对象。

This can produce symptoms similar to yours, but I'm not certain this is the problem you face. 这可能会产生类似于您的症状,但我不确定这是您面临的问题。

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

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