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

时间:2022-10-24 12:50:48

Here is the web service. It builds and launches and I can browse to it. The ValidateCoupon method works because it returns an int.

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

The problem is with the GetCouponInfo method. It should return an object of type Coupon but the XML it generates looks like this:

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

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

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 个解决方案

#1


4  

You are getting nothing because all your properties except couponDetails into Coupon class are readonly.

你没有得到任何东西,因为除了couponDetails到Coupon类之外的所有属性都是readonly。

Quote from this link :

从这个链接引用:

If a Web service contains a Web method that either accepts a parameter or returns a value that is an object reference, and the class definition of the object contains a read-only property, the read-only property is not available when you build a proxy assembly for the Web service.

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

The workaround, if you persist to have a readonly properties, is to implement the setter by throwing an NotImplementedException

如果您坚持拥有只读属性,那么解决方法是通过抛出NotImplementedException来实现setter

like this one :

像这个 :

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

#1


4  

You are getting nothing because all your properties except couponDetails into Coupon class are readonly.

你没有得到任何东西,因为除了couponDetails到Coupon类之外的所有属性都是readonly。

Quote from this link :

从这个链接引用:

If a Web service contains a Web method that either accepts a parameter or returns a value that is an object reference, and the class definition of the object contains a read-only property, the read-only property is not available when you build a proxy assembly for the Web service.

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

The workaround, if you persist to have a readonly properties, is to implement the setter by throwing an NotImplementedException

如果您坚持拥有只读属性,那么解决方法是通过抛出NotImplementedException来实现setter

like this one :

像这个 :

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