【问题标题】:Return multiple values with web service使用 Web 服务返回多个值
【发布时间】:2016-04-14 19:50:55
【问题描述】:

我正在尝试学习网络服务。我的问题是我创建了一个类“Item”,如下所示,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebServiceTaxCalc
{
public enum Type { BASIC, IMPORTED, EXEMPT}
public class Item
{
    string name;
    double basePrice;
    int quantity;
    Type TaxType;
    public Item(string name, double price, int quantity, Type type)
    {
        this.basePrice = price;
        this.name = name;
        this.quantity = quantity;
        this.TaxType = type;
    }
    public string getName()
    {
        return name;
    }
    public double getPrice()
    { return basePrice; }
    public int getQuantity()
    { return quantity; }
    public Type getType() { return TaxType; }
}
}

还有一个计算税的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace WebServiceTaxCalc
{
public class ShoppingBasket
{
    double itemsTotalPrice = 0.0;
    double totalTax = 0.0;
    public void addItem(List<Item> i)
    {

        foreach (Item a in i)
        {
            totalTax += taxPerItem(a);
            double eachItemPrice = (a.getPrice() + taxPerItem(a));
            itemsTotalPrice += eachItemPrice;

            //Console.WriteLine(a.getQuantity() + " " + a.getName() + ": " + eachItemPrice);
        }

        //Console.WriteLine("sales tax: " + totalTax);
        //Console.WriteLine("total cost: "+itemsTotalPrice);
    }

     public double taxPerItem(Item i)
    {
        double tax = 0;
        if (i.getType() == Type.BASIC)
        {
            tax = i.getPrice() * 5 / 100;
            return tax;
        }
        else if (i.getType() == Type.EXEMPT)
        {
            tax = 0;
            return tax;
        }
        else
        {
            tax = i.getPrice() * 15 / 100;
            return tax;
        }

    }

}

}

我正在尝试将值传递给 Web 服务并让 Web 服务调用类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;


namespace WebServiceTaxCalc
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public ShoppingBasket Calc(string name, double price, int quantity, Type catg)
    {
        List<Item> l1 = new List<Item>();
        Item i1 = new Item(name, price, quantity,catg);
        l1.Add(i1);
        ShoppingBasket sb = new ShoppingBasket();
        sb.addItem(l1);

        return sb;

    }
}
}

我给出这样的输入, input image:

在调用之后我得到了这个: Output image

我没有得到我通过的文档树。 我在这里看到了一个有用的帖子https://stackoverflow.com/a/12039010/3768995 但我无法解决我的问题。 请指导我完成此操作。

【问题讨论】:

  • 尝试将ShoppingBasket中的字段转换为public属性。

标签: c# asp.net web-services oop


【解决方案1】:

ShoppingBasket 添加一个[DataContract] 属性,向发送响应时需要包含的属性添加一个[DataMember] 属性。 (如前所述,将它们设为公共属性。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 2012-08-23
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多