C#-使用(JSON序列化)从Web Service检索数据

时间:2018-11-12 05:16:50

标签: web-services asmx

美好的一天,

我正在尝试从Web服务(.asmx)检索数据, 但是在控制器部分,我有这个异常

  

“ System.AggregateException-发生一个或多个错误”

关于如何解决此问题的任何想法?谢谢

TestService

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]

public class TestService : System.Web.Services.WebService
{

    Core connection = new Core();
    DataTable dtUnits = new DataTable();
    QServiceQuery query = new QServiceQuery();

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string LoadInvoices()
    {
        dtUnits = connection.DataTable_return(query.LoadInvoices());

        List<Invoice> list = new List<Invoice>();
        Invoice inv = new Invoice();

        //for (int i = 0; i < dtUnits.Rows.Count; i++)
        //{
        inv.transNo = dtUnits.Rows[0]["transNo"].ToString();

        list.Add(inv);
        //}
        //JavaScriptSerializer js = new JavaScriptSerializer();
        //Context.Response.Write(js.Serialize(list));
        return new JavaScriptSerializer().Serialize(list);
    }
}

TestController

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:9834/");
        //client.DefaultRequestHeaders.Add("appkey", "myapp_key");
        client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync("WebService/TestService.asmx/LoadInvoices").Result;
        if (response.IsSuccessStatusCode)
        {
            try
            {
                var transNo = response.Content.ReadAsAsync<IEnumerable<Invoices>>().Result;
                invoice.Add(new Invoices.Invoice() { transNo = transNo.ToString() });
                listView.ItemsSource = invoice;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
        else
        {
            MessageBox.Show("Error Code " + response.StatusCode + " : Message - " + response.ReasonPhrase);
        }
    }

内部异常

  

第1行第37位错误。期望元素'ArrayOfInvoices'来自   命名空间'http://schemas.datacontract.org/2004/07/TestController'..   遇到名称为“字符串”的“元素”,名称空间   'http://tempuri.org/'

异常消息 https://i.stack.imgur.com/mmfRf.png

0 个答案:

没有答案