【发布时间】:2019-12-15 02:45:29
【问题描述】:
我正在试验 WCF 并使用 id 和 name 参数构建了一个标准产品类,我的目标是从休息中接收它,并返回状态。
[DataContract]
public partial class Product {
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class Message
{
[DataMember]
public bool isSucceed { get; set; }
}
用相对的 Post 方法来
[WebInvoke(UriTemplate = "ProductPingXML", Method = "POST",
RequestFormat = WebMessageFormat.Xml)]
[Description("Recive Post Message")]
public Message PingXmlProduct(Product Input)
{
Message message = new Message();
//Todo Capture what rest send
if (Input == null)
{
message.isSucceed = false;
}
else
{
message.isSucceed = true;
}
// strip the xml from the body
// Assign the values to the new obj class Product
return message;
}
我正在尝试通过邮递员使用 XML 帮助架构中的这个 XML 来调用它。
<Product xmlns="http://schemas.datacontract.org/2004/07/RestML.Data">
<Id>2147483647</Id>
<Name>String content</Name>
</Product>
使用 WCF 对我来说相对较新,所以我可能会在这里遗漏一些东西。所以我的问题是: 如何在 PingXmlProduct 中接收邮递员 XML 并将相应的值分配给新的 obj;
【问题讨论】:
-
此时(在
PingXmlProduct方法内),xml 已经被 WCF 框架“从正文中剥离”。您只需将Product类的一个实例分配给Input变量,并使用xml 中的值填充属性Id 和Name。 -
好的,如果我尝试使用 xml 请求发布,我会从邮递员那里得到“无法得到任何响应”?是不是因为响应是一个字符串而不是 xml
-
状态码是什么? 404?那将意味着“未找到”。我建议根据状态码采取后续步骤。