我是创建 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 对象发布到服务(使用此数据协定),输出由 a 生成DataContractSerializer(typeof(MessagingWS.Email))
(因为在使用“添加服务引用”添加 WCF 服务后,我可以使用 Email 类...' 作为客户端应用程序上的 MessagingWS)。无论如何,这会产生以下输出,当我尝试 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>first.last@gmail.com</addressfield>
<eventidfield>123456</eventidfield>
<firstnamefield>First</firstnamefield>
<lastnamefield>Last</lastnamefield>
<messagefield>Message Content</messagefield>
<notifywindowendfield>DateTime value</notifywindowendfield>
<subjectfield>Test Subject</subjectfield>
</propertychanged>
所以我最终得到了所有附加“字段”的元素,它们不是我在 DataContract 中指定的顺序(它们采用默认顺序)。有人可以告诉我这里发生了什么,如果我试图完成一些不可能的事情吗?
我确实注意到,当我将 DataContract 类从服务器复制到客户端(并将其包含在不同的命名空间 - Messaging2 下)时,当我使用DataContractSerializer(typeof(Messaging2.Email))
. 我真的应该这样做吗?它似乎有效,但我认为我应该能够在运行“添加服务引用......”后使用我在客户端获得的类。
非常感谢任何帮助 - 谢谢!