无法从我的.Net Web服务返回一个对象

时间:2016-02-05 21:55:19

标签: c# .net asmx

这是网络服务。它构建和启动,我可以浏览它。 ValidateCoupon方法有效,因为它返回一个int。

问题在于GetCouponInfo方法。它应返回Coupon类型的对象,但它生成的XML如下所示:

<Coupon/>

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

/// <summary>
/// Web Services for Coupon Processing in SimGrocery
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 CouponService : System.Web.Services.WebService {

    /// <summary>
    /// Summary description for Coupon
    /// </summary>
    public class Coupon {
        private List<CouponDetail> mCouponDetails;
        private String mCoupon;
        private String mDescription;
        private String mCouponSource;
        private DateTime mStartDate, mThroughDate;

        // A Parameterless constructor is required for an object to be serialized.
        public Coupon() { }

        public Coupon(String coupon, String description, String couponSource, DateTime startDate, DateTime throughDate) {
            List<CouponDetail> mCouponDetails = new List<CouponDetail>();
            mCoupon = coupon;
            mDescription = description;
            mCouponSource = couponSource;
            mStartDate = startDate;
            mThroughDate = throughDate;
        }
        public String coupon { get { return mCoupon; } }
        public String description { get { return mDescription; } }
        public String couponSource { get { return mCouponSource; } }
        public DateTime startDate { get { return mStartDate; } }
        public DateTime throughDate { get { return mThroughDate; } }

        public List<CouponDetail> couponDetails {
            get { return mCouponDetails; }
            set { mCouponDetails = value; }
        }

        public void addCouponDetail(CouponDetail couponDetail) {
            mCouponDetails.Add(couponDetail);
        }

    }

    public class CouponDetail {
        String mProduct;
        double mAmountOff;
        int mPercentageDiscount;
        String mDiscountType;

        // A Parameterless constructor is required for an object to be serialized.
        public CouponDetail() { }

        public CouponDetail(String product, double amountOff, int percentageDiscount, String discountType) {
            mProduct = product;
            mAmountOff = amountOff;
            mPercentageDiscount = percentageDiscount;
            mDiscountType = discountType;
        }
        public String product { get { return mProduct; } }
        public double amountOff { get { return mAmountOff; } }
        public int percentageDiscount { get { return mPercentageDiscount; } }
        public String discountType { get { return mDiscountType; } }

    }

    public CouponService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public int ValidateCoupon(String coupon) {
        int couponID = 0;
        // (String pTarget, String pDomain, String pCriteria, String pAggregate)
        return couponID;
    }
    [WebMethod]
    public Coupon GetCouponInfo(int CouponID) {
        // ToDo: Write this
        // Use fGetCouponInfo table-valued function in SQL Server
        Coupon coupon = new Coupon("XXXXX","Test Coupon","Penny Saver", Convert.ToDateTime("1/1/2016"), Convert.ToDateTime("12/31/2016"));

        return coupon;
    }
}

1 个答案:

答案 0 :(得分:4)

您没有得到任何结果,因为除couponDetails之外的所有属性都是优惠券类。

引用此link

  

如果Web服务包含接受参数或返回作为对象引用的值的Web方法,并且该对象的类定义包含只读属性,则只读属性不可用为Web服务构建代理程序集。

如果您坚持使用只读属性,则解决方法是通过抛出NotImplementedException

来实现setter 像这样:

public DateTime throughDate 
{ 
    get { return mThroughDate; } 
    set { throw new NotImplementedException("Cannot set read-only property 'throughDate '"); }
}