views:

337

answers:

3

I'm new to creating WCF REST services - so please let me kwow if I'm doing things wrong here.

I have a REST-based service developed using WCF, with the following DataContract defined:

namespace Messaging
{
    [DataContract(Name = "Email", Namespace = "")]
    public class Email
    {
        #region Fields

        private string subject;
        private string message;
        private string address;
        private string firstName;
        private string lastName;
        private string notifyWindowEnd;
        private string eventNotificationID;

        #endregion

        #region Properties

        /// <summary>
        /// Email Subject.
        /// </summary>
        [DataMember(Name = "Subject", Order = 1)]
        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }

        /// <summary>
        /// Email Body.
        /// </summary>
        [DataMember(Name = "Message", Order = 2)]
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        /// <summary>
        /// Email Address of Recipient.
        /// </summary>
        [DataMember(Name = "Address", Order = 3)]
        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        /// <summary>
        /// First Name of Recipient.
        /// </summary>
        [DataMember(Name = "FirstName", Order = 4)]
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        /// <summary>
        /// Last Name of Recipient.
        /// </summary>
        [DataMember(Name = "LastName", Order = 5)]
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        /// <summary>
        /// Time at which the Email will cease to be sent.
        /// </summary>
        [DataMember(Name = "NotifyWindowEnd", Order = 6)]
        public string NotifyWindowEnd
        {
            get { return notifyWindowEnd; }
            set { notifyWindowEnd = value; }
        }

        /// <summary>
        /// ID of the Event for which the Email has been generated.
        /// </summary>
        [DataMember(Name = "EventID", Order = 7)]
        public string EventID
        {
            get { return eventID; }
            set { eventID = value; }
        }

        #endregion
    }
}

On the client application, I am trying to POST to a service (that uses this Data Contract) using the HttpWebRequest object with the output generated by a DataContractSerilizer(typeof(MessagingWS.Email)) (since I have the Email class available to me after adding the WCF service using 'Add Service Reference...' as MessagingWS on the client application). In any event, this produces the following output which generates a 400 - Bad Request coming back from the service when I try to POST:

<email xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MessagingWS"&gt;
<propertychanged xmlns:a="http://schemas.datacontract.org/2004/07/System.ComponentModel" i:nil="true">
 <addressfield>[email protected]</addressfield>
 <eventidfield>123456</eventidfield>
 <firstnamefield>First</firstnamefield>
 <lastnamefield>Last</lastnamefield>
 <messagefield>Message Content</messagefield>
 <notifywindowendfield>DateTime value</notifywindowendfield>
 <subjectfield>Test Subject</subjectfield>
</propertychanged>

So I end up with all elements appended with "field", and they are not in the order I specified in the DataContract (they assume default order). Could someone please tell me what is going on here, and if I'm trying to accomplish something that isn't possible?

I did notice that when I copied the DataContract class from the server to the client (and included it under a different namespace - Messaging2), the output was serialized as expected when I used a DataContractSerializer(typeof(Messaging2.Email)). Is this the way I really should be doing it? It seems to work, but I figure I should be able to use the class that I get on the client side after running "Add Service Reference...".

Any help is greatly appreciated - thanks!

A: 

What does the Email class definition imported by Add Service Reference look like on the client side? If I had to guess, I'd say it's probably wrong (for whatever reason). One thing I don't understand, though... are you sure the XML you show is actually the one generated by the DataContractSerializer in this case? It doesn't quite make sense to me... where does the element come from, since it's not defined in your class above?

tomasr
+1  A: 

Your DataContract attribute says Namespace="" but your example XML says xmlns="http://schemas.datacontract.org/2004/07/MessagingWS".

Try regenerating your service reference.

JLamb
A: 

Did you solve this I have the same problem?

Ritesh Ramesh