DataContractSerializer 输出带有“字段”的 XML 附加到所有元素并且无序

发布于 2024-07-30 09:51:19 字数 3968 浏览 8 评论 0原文

我是创建 WCF REST 服务的新手 - 所以如果我在这里做错了,请告诉我。

我有一个使用 WCF 开发的基于 REST 的服务,定义了以下 DataContract:

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

在客户端应用程序上,我尝试使用 HttpWebRequest 对象以及由 生成的输出来 POST 到服务(使用此数据协定) DataContractSerializer(typeof(MessagingWS.Email)) (因为在客户端应用程序上使用“添加服务引用...”作为 MessagingWS 添加 WCF 服务后,我可以使用 Email 类)。 无论如何,这会产生以下输出,当我尝试 POST 时,该输出会生成从服务返回的 400 - Bad Request:

<email xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MessagingWS">
<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>

因此,我最终得到的所有元素都附加了“field”,并且它们不按照我在 DataContract 中指定的顺序(它们采用默认顺序)。 有人可以告诉我这里发生了什么,以及我是否正在尝试完成一些不可能的事情吗?

我确实注意到,当我将 DataContract 类从服务器复制到客户端(并将其包含在不同的命名空间 - Messaging2 下)时,当我使用 DataContractSerializer(typeof(Messaging2.Email)) 时,输出按预期序列化。 这真的是我应该做的吗? 它似乎有效,但我认为我应该能够使用运行“添加服务引用...”后在客户端获得的类。

非常感谢任何帮助 - 谢谢!

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 DataContractSerializer(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">
<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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-08-06 09:51:19

您的 DataContract 属性显示 Namespace="" 但示例 XML 显示 xmlns="http://schemas.datacontract.org/2004/07/MessagingWS"。

尝试重新生成您的服务参考。

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

Try regenerating your service reference.

温柔少女心 2024-08-06 09:51:19

通过“添加服务引用”导入的电子邮件类定义在客户端看起来是什么样的? 如果我必须猜测,我会说这可能是错误的(无论出于何种原因)。 不过,我不明白一件事...您确定您显示的 XML 实际上是在本例中由 DataContractSerializer 生成的 XML 吗? 这对我来说不太有意义......该元素来自哪里,因为它没有在上面的类中定义?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文