locked
XmlSerialization and Properties RRS feed

  • Question

  • User794609384 posted

    Hey all,

    I'm having a very frustrating time with WebServices at the moment. I have a WebMethod that accepts a custom paramater:

    1    namespace Services
    2    {
    3        public class SaveActivityItemResponse 
    4        {
    5            // Don't worry about the content of this class too much,
    6            // I'm just pointing out it's a custom class.
    7        }
    8    
    9        public class SaveActivityItemRequest
    10       {
    11           public SaveActivityItemRequest() { }
    12   
    13           private ActivityItem _activity = null;
    14           public ActivityItem Activity
    15           {
    16               get { return this._activity ; }
    17               set { this._activity = value; }
    18           }
    19   
    20           private Guid? _owningUserId = null;
    21           public Guid? OwningUserId
    22           {
    23               get { return this._owningUserId; }
    24               set { this._owningUserId = value; }
    25           }
    26       }    
    27   
    28       /// <summary>
    29       ///     Summary description for TaskUpdaterService
    30       /// </summary>
    31       [WebService(Namespace = "http://www.domain.com/API/TaskUpdaterService.asmx")]
    32       [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    33       // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    34       // [System.Web.Script.Services.ScriptService]
    35       public class TaskUpdaterService : System.Web.Services.WebService
    36       {
    37           public TaskUpdaterService()
    38           {
    39               //Uncomment the following line if using designed components 
    40               //InitializeComponent(); 
    41           }
    42   
    43           [WebMethod]
    44           public SaveActivityItemResponse SaveActivity(string sUsername, string sPassword, SaveActivityItemRequest request)
    45           {
    46               // Lines omitted for brevity.
    47               BusinessItem.Save(request.Activity);
    48               // Lines omitted for brevity.
    49           }
    50       }
    51   }
    

    And the ActivityItem:

    1    namespace Data.Activity
    2    {
    3        public class ActivityItem : BusinessItem
    4        {
    5            // Lines ommitted for brevity
    6    
    7            public enum EActivityTypeCode
    8            {
    9                Fax = 4204,
    10               PhoneCall = 4210,
    11               Email = 4202,
    12               Letter = 4207,
    13               Appointment = 4201,
    14               ServiceActivity = 4214,
    15               OpportunityClose = 4208,
    16               OrderClose = 4209,
    17               QuoteClose = 4211,
    18               CaseResolution = 4206,
    19               Task = 4212,
    20               CampaignResponse = 4401,
    21               CampaignActivity = 4402,
    22               BulkOperation = 4406
    23           }
    24   
    25           private EActivityTypeCode? _eActivityTypeCode = null;
    26           [SqlField("ActivityPointerBase", "ActivityTypeCode")]
    27           public EActivityTypeCode ActivityTypeCode
    28           {
    29               get
    30               {
    31                   if (!this._eActivityTypeCode.HasValue)
    32                   {
    33                       this._eActivityTypeCode = EActivityTypeCode.Task;
    34                   }
    35                   return this._eActivityTypeCode.Value;
    36               }
    37               set { this._eActivityTypeCode = value; }
    38           }
    39           public override Type ChildCollectionType
    40           {
    41               get { return null; }
    42           }
    43   
    44           // Lines ommitted for brevity
    45       }
    46   }
    

    Just to explain what's going on: You'll notice that ActivityItem inherits from a base class called BusinessItem, and there's an Attribute on the ActivityTypeCode property called SqlFieldAttribute. SqlFieldAttribute is a custom attribute that stores the table and field name within a given database that maps to whatever Property it is assigned to. The BusinessItem class has a static method - Save(BusinessItem item) - that will read out all the SqlAttributes from a BusinessItem object and then use these attributes to construct SqlStatements for inserts or updates on the fly.

    The problem I'm having is this: I don't want to have to set all my properties all the time. That's why I'm setting the value of the nullable ActivityItem._eActivityTypeCode property to Task in the ActivityItem.ActivityTypeCode property's get clause. This seems a bit clumsy here - I could just be setting the private field to Email instead of null. But that's not going to be adequate all the time. I do frequently need that get (or set) clause to do something clever whenever I try to read a given property.

    The problem I'm having is that, when I read in the ActivityItem from the WebService and handing it to the BusinessItem.Save() static method, the value that I get back into the database is null - it's as if the get clause isn't actually being called at all!

    This is weirding me out. I can't work out what's happening. Does the XmlSerialization do anything strange to Property accessors that I need to know about?

    Thursday, January 29, 2009 6:42 PM

Answers