【发布时间】:2014-02-13 17:39:11
【问题描述】:
我有一个 Wcf 服务 .NET 4.5.1,我可以使用 WcfTestClient.exe 连接到它并发送一个测试对象,或者使用以下的(soap)Xml;
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/PostData</Action>
</s:Header>
<s:Body>
<PostData xmlns="http://tempuri.org/">
<person xmlns:d4p1="PersonNameSpace" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:Id>1</d4p1:Id>
<d4p1:Name>My Name</d4p1:Name>
</person>
</PostData>
</s:Body>
</s:Envelope>
我的界面如下;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "PostData",
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
string PostData(Person person);
}
[DataContract(Namespace = "PersonNameSpace")]
public class Person
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
我的方法如下;
public string PostData(Person person)
{
//do something with the object
return "Well done";
}
这很好用。但是现在,我想通过从经典 ASP 页面传递原始 Xml 或 Json 作为示例来调用相同的方法 PostData;
<PostData>
<person>
<Id>1</Id>
<Name>My name</Name>
</person>
</PostData>
或Json格式为
{
"PostData": {
"person": {
"Id": "1",
"Name": "My name"
}
}
}
如何将这些数据作为 Xml 或 Json 使用,以便我可以使用 XmlSerializer 或类似的东西;
JavaScriptSerializer.Deserialize(PostDataString);
基本上我想要做的是,无论请求是来自使用 SOAP 的应用程序,还是来自使用基本 Xml 帖子的网站,都获取这些数据并将其反序列化到我的对象中。
【问题讨论】: