0

我正在尝试设置一个简单的服务,该服务将允许用户在 POST 的正文中将 Pickup 对象作为 XML 传递。如果我将服务设置为接收 Stream 类型,我可以让它正常工作,但我不确定我需要改变什么才能让它而不是自定义类型工作正常。当我告诉服务器期待一个 Pickup 类型时,我得到“请求错误。服务器遇到了......”

我有人可以告诉我需要在服务器/客户端中进行哪些更改才能使其正常工作,我将非常感激。

我的数据合约

[DataContract]
public class Pickup
{        
    [DataMember]
    public string DelZip
    { get; set; }

}

运营合同

  [OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "/pickups",
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare)]
    string RequestPickup(Pickup pickup);


    public string RequestPickup(Pickup pu)
    {
        return pu.DelZip;
    }

配置

      <services>
  <service name="TestAPI.Services.TestServices" >
    <endpoint address=""
              binding="webHttpBinding" behaviorConfiguration="webHttp"
              contract="TestAPI.Services.ITestServices" />
  </service>
</services>    
<behaviors>
  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp/>
    </behavior>
  </endpointBehaviors>      
</behaviors>

我的客户代码

 const string url = "http://localhost:18463/TestServices.svc/pickups";
                req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/xml";                

                var xmlDoc = new XmlDocument { XmlResolver = null };
                xmlDoc.LoadXml(@"<Pickup><DelZip>55555</DelZip></Pickup>");
                string sXml = xmlDoc.InnerXml;
                req.ContentLength = sXml.Length;
                var sw = new StreamWriter(req.GetRequestStream());
                sw.Write(sXml);
                sw.Close();

                res = (HttpWebResponse)req.GetResponse();
                Stream responseStream = res.GetResponseStream();
                var streamReader = new StreamReader(responseStream);
4

0 回答 0