无法在 wcf c# 中接收 xml post 请求值 [英] can't receive xml post request values in wcf c#

查看:44
本文介绍了无法在 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="http://schemas.datacontract.org/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="http://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 请求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆