我正在尝试学习网络服务。我的问题是我创建了一个类“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 但我无法解决我的问题。 请指导我完成这个。
答案 0 :(得分:0)
将[DataContract]属性添加到ShoppingBasket
,将[DataMember]属性添加到发送响应时需要包含的属性。 (正如所说的,将它们作为公共财产。)