XML 未传递给 WCF

XML not being passed to WCF

我 运行 在 API 上进行了一些本地测试,由于某种原因,数据 XML 数据没有进入 API。 rData.labelDetail 作为 null 提交给 API。

我似乎使用了不正确的 ContentType,但我尝试了 application/xml 和 text/xml 但没有成功。

要求:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;

public partial class TestAPI : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebRequest request = WebRequest.Create("http://localhost:56146/Packages.svc/auth");
        request.Method = "POST";
        string postData = "<?xml version='1.0' encoding='UTF-8'?><RequestData xmlns='http://www.labels.com/label'><details>Test|Second Part</details></RequestData>";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/xml";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine(responseFromServer);
        reader.Close();
        dataStream.Close();
        response.Close();
    }
}

服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace XYZService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IPackages" in both code and config file together.
    [ServiceContract]
    public interface IPackages
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "auth")]
        ResponseData Auth(RequestData rData);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace XYZService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Packages" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Packages.svc or Packages.svc.cs at the Solution Explorer and start debugging.
    public class Packages : IPackages
    {
        #region

        public ResponseData Auth(RequestData rData)
        {
            var data = rData.labelDetail.Split('|'); //Keep getting error here
            var response = new ResponseData
            {
                Name = data[0],
                PackageID = data[1]
            };

            return response;
        }

        #endregion
    }

    [DataContract(Namespace = "http://www.labels.com/label")]
    public class RequestData
    {
        [DataMember]
        public string labelDetail { get; set; }
    }

    [DataContract]
    public class ResponseData
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string PackageID { get; set; }
    }

}

您的元素名称不匹配。在您的 XML 请求中,您的根元素 RequestData 有一个名为 details:

的嵌套元素
    string postData = "<?xml version='1.0' encoding='UTF-8'?><RequestData xmlns='http://www.labels.com/label'><details>Test|Second Part</details></RequestData>";

但是在你的数据契约中,对应的属性被命名为labelDetail:

[DataContract(Namespace = "http://www.labels.com/label")]
public class RequestData
{
    [DataMember] // Notice that "Name" is not set
    public string labelDetail { get; set; }
}

您需要使名称匹配,例如:

    [DataMember(Name="details")] 
    public string labelDetail { get; set; }