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

can't receive xml post request values in wcf c#

我正在试验 WCF 并构建了一个带有 id 和 name 参数的标准产品 class,我的目标是从 rest 和 return 状态接收它。

[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 并将相应的值分配给 new obj;

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

<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。

[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中像下面这样调用服务(请注意自定义的命名空间class)。

关于WebMessageBodyStyle请参考我之前的回复属性.

如果问题仍然存在,请随时告诉我。