我是创建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
}
}
在客户端应用程序上,我尝试使用带有DataContractSerializer(typeof(MessagingWS.Email))
生成的输出的HttpWebRequest对象POST到服务(使用此数据协定)(因为我在添加后可以使用Email类) WCF服务使用“添加服务引用...”作为客户端应用程序上的MessagingWS)。在任何情况下,这会产生以下输出,当我尝试POST时,它会生成一个400 - 来自服务的错误请求:
<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>
所以我最终得到了附加“field”的所有元素,它们不是我在DataContract中指定的顺序(它们假设默认顺序)。有人可以告诉我这里发生了什么,如果我想要完成一些不可能的事情吗?
我注意到当我将DataContract类从服务器复制到客户端(并将其包含在不同的命名空间下 - Messaging2)时,当我使用DataContractSerializer(typeof(Messaging2.Email))
时,输出按预期序列化。这是我真正应该这样做的方式吗?它似乎工作,但我想我应该能够使用运行“添加服务引用...”后在客户端获得的类。
非常感谢任何帮助 - 谢谢!
答案 0 :(得分:1)
您的DataContract属性表示Namespace =“”,但您的示例XML表示xmlns =“http://schemas.datacontract.org/2004/07/MessagingWS”。
尝试重新生成服务参考。
答案 1 :(得分:0)
添加服务参考导入的电子邮件类定义在客户端看起来是什么样的?如果我不得不猜测,我会说它可能是错的(无论出于何种原因)。有一点我不明白,但是......你确定你展示的XML实际上是DataContractSerializer在这种情况下生成的吗?这对我来说没有多大意义......元素来自哪里,因为它没有在上面的课程中定义?