无法在 wcf c# 中接收 xml post 请求值

编程入门 行业动态 更新时间:2024-10-28 00:20:46
本文介绍了无法在 wcf c# 中接收 xml post 请求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在试验 WCF 并构建了一个带有 id 和 name 参数的标准产品类,我的目标是从休息中接收它,并返回状态.

I'm experimenting with WCF and built a standard product class with id and name parameters, my goal is to receive it from rest, and return status.

[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 通过邮递员调用它.

and I'm trying to call it through postman using this XML found in the XML help schema.

<Product xmlns="schemas.datacontract/2004/07/RestML.Data"> <Id>2147483647</Id> <Name>String content</Name> </Product>

使用 WCF 对我来说相对较新,所以我可能会错过一些东西.所以我的问题是:如何在 PingXmlProduct 中接收邮递员 XML 并将相应的值分配给新的 obj;

Working with WCF is relatively new to me, so I might miss something here. so my question is: how can I receive the postman XML inside PingXmlProduct and assign the respective values into new obj;

推荐答案

我们应该使用 webhttpbinding 创建 Restful 风格的 WCF 服务.请参考以下配置.

We should create Restful style WCF service by using webhttpbinding. Please refer to the below configuration.

<system.serviceModel> <services> <service name="Server1.MyService"> <endpoint address="" binding="webHttpBinding" contract="Server1.IService" behaviorConfiguration="rest"></endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint> <host> <baseAddresses> <add baseAddress="localhost:5577"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="rest"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel>

然后我们应该指定 webmessagebodystyle.

Then we should specify the webmessagebodystyle.

[OperationContract] [WebInvoke(BodyStyle =WebMessageBodyStyle.Bare)] void GetData(Product product);

假设有以下定义.

[DataContract] public class Product { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } }

我们可以在 Postman 中像下面这样调用服务(请注意自定义类的命名空间).关于WebMessageBodyStyle属性请参考我之前的回复.使用JSON获取对象为空WCF 服务如果问题仍然存在,请随时告诉我.

We could call the service like the below way in Postman(please pay attention to the namespace of the custom class). Please refer to my previous reply related to the WebMessageBodyStyle property. Get the object is null using JSON in WCF Service Feel free to let me know if the problem still exists.

更多推荐

无法在 wcf c# 中接收 xml post 请求值

本文发布于:2023-10-11 04:45:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1480720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:求值   wcf   xml   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!