Skip to main content
 首页 » 编程设计

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

2024年02月05日29zengkefu

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

问题出在 GetCouponInfo 方法上。它应该返回一个 Coupon 类型的对象,但它生成的 XML 看起来像这样:

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; 
    } 
} 

请您参考如下方法:

你什么也得不到,因为除了 Coupon 类中的 couponDetails 之外,你的所有属性都是只读的。

引自此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.

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

来实现 setter

喜欢这个:

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