8

While creating a WCF Rest service, I've noticed that not all the parameters in my web service are making it into my implementation.

Here's the interface:

[ServiceContract(Namespace="http://example.com/recordservice")]
public interface IBosleySchedulingServiceImpl
{
    [OperationContract]
    [WebInvoke(UriTemplate = "Record/Create",
        RequestFormat = WebMessageFormat.Xml, 
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
    string CreateRecord(Record record);
}

[DataContract(Namespace="http://example.com/recordservice")]
public class Appointment
{
    [DataMember]
    public int ResponseType { get; set; }

    [DataMember]
    public int ServiceType { get; set; }

    [DataMember]
    public string ContactId { get; set; }

    [DataMember]
    public string Location { get; set; }

    [DataMember]
    public string Time { get; set; }        
}

I'm passing this XML in:

<Appointment xmlns="http://ngs.bosley.com/BosleySchedulingService">
  <ContactId>1123-123</ContactId>
  <Location>Fresno</Location>
  <Time>2012-05-05T08:30:00</Time>
  <ResponseType>45</ResponseType>
  <ServiceType>45</ServiceType>
</Appointment>

In my service, I'm just outputting the values to a log so I can verify the values are coming through for the time being:

logger.Debug("ContactId: " + appointment.ContactId);
logger.Debug("Time Field: " + appointment.Time);
logger.Debug("Location: " + appointment.Location);
logger.Debug("Response Type: " + Convert.ToInt32(appointment.ResponseType));
logger.Debug("ServiceType: " + Convert.ToInt32(appointment.ServiceType));

However, in my output, the integer values are coming across as zeroes:

ContactId: 1123-123
Time Field: 2012-05-05T08:30:00
Location: Fresno
Response Type: 0
ServiceType: 0

When I remove the strings from the DataContract and the service implementation, the integer values come through without a problem.

Response Type: 45
ServiceType: 45

I am utterly confused by this and any help would be greatly appreciated.

Ryan N. Bell
  • 85
  • 1
  • 6
  • Any specific reason why you are explicitly converting your integers to Int32? – Satyajit May 06 '12 at 22:37
  • Let's start by forgetting the conversion to integer, and just output the `ResponseType` and `ServiceType` to the logger as bare strings. – Robert Harvey May 06 '12 at 22:45
  • The ConvertTo was just an attempt to determine if my values were being lost because of incorrect cast on the part of the service implementation. Now that I can actually get the values using the answer below, I'll just let them utilize the ToString method like they normally would. Thanks though. – Ryan N. Bell May 07 '12 at 14:13

1 Answers1

12

By default when you send an object through wcf the properties will be sent in Alphabetical order unless you specify the order.

You can specify the order of the properties or change the ordering so that they appear in alphabetical order.

[DataContract(Namespace="http://example.com/recordservice")]
public class Appointment
{
    [DataMember(Order = 1)]
    public int ResponseType { get; set; }

    [DataMember(Order = 2)]
    public int ServiceType { get; set; }

    [DataMember(Order = 3)]
    public string ContactId { get; set; }

    [DataMember(Order = 4)]
    public string Location { get; set; }

    [DataMember(Order = 5)]
    public string Time { get; set; }        
}
SCB
  • 3,034
  • 2
  • 25
  • 24
  • If the properties are not sent in the correct order then they will not get populated when they are sent to the service – SCB May 06 '12 at 23:40
  • 1
    Would that show up in properties that get populated in the wrong slot? Or will it just populate the ones that it has? Seems like an odd requirement for a web service. – Robert Harvey May 06 '12 at 23:46
  • 2
    In the above case the first 3 items are sent in alphabetical order, the last 2, Location and Time are out of sequence. My understanding is that WCF will essentially ignore these. http://msdn.microsoft.com/en-us/library/ms729813.aspx – SCB May 06 '12 at 23:49
  • 8
    This is the kind of stuff that makes programmers want to strangle someone. – Robert Harvey May 06 '12 at 23:56
  • Yep, but you only get bitten once by it – SCB May 06 '12 at 23:58
  • Thank you so much. This was giving me problems for days. I suppose, since it is mapping it dynamically this makes some iota of sense. – Ryan N. Bell May 07 '12 at 14:04
  • 1
    OK, lost a full day on this and was about to give up and found this answer. Thanks a lot for that - appreciate it – Liam May 11 '12 at 18:07