简体   繁体   English

在WCF REST 4.0中发布复杂类型/ HttpContent?

[英]Posting Complex types/HttpContent in WCF REST 4.0?

I have a concern in passing complex objects/any other types: because I always get a bad request...Code snippet below: 我担心传递复杂的对象/其他任何类型:因为我总是收到一个错误的请求...下面的代码片段:

Service: 服务:

[OperationContract]
[WebInvoke(UriTemplate = "saveXML/", Method="POST", BodyStyle= WebMessageBodyStyle.Bare)]
bool saveXML(XElement xmlString)
{
       return true;
}

========= =========

Client: 客户:

private HttpUrlEncodedForm frm = new HttpUrlEncodedForm();

frm.Add("CustomerCode", "123");
frm.Add("CustomerName", "Joseph");
frm.Add("Address", "4th Street Washington Ave. New York");
frm.Add("Country", "United States of America");

using (HttpResponseMessage response = m_RestHttpClient.Post("saveXML/", frm.CreateHttpContent()))
{
   response.EnsureStatusIsSuccessful();
}

or I tried it this way: 还是我这样尝试过:

var request = new XDocument(
              new XElement("Customer",
              new XElement("CustomerCode", "123"),
              new XElement("CustomerName", "Joseph"),
              new XElement("Address", "4th Street Washington Ave. New York"),
              new XElement("Country", "United States of America")));

 frm.Add("body", request.ToString());

..both ways did not work....this is just a sample i want to use complex types because i will be passing atleast 50 parameters...or if you have any other suggestions feel free to suggest. ..两种方式都行不通....这只是一个示例,我想使用复杂类型,因为我将传递至少50个参数...或者如果您有任何其他建议可以随时提出。

Thank you 谢谢

Best Regards, Ravi 最好的问候,拉维

You're passing the complex type as an XElement - this is going to make things more complicated. 您将复杂类型作为XElement传递-这会使事情变得更复杂。 Just pass the strongly-typed object. 只需传递强类型的对象。 Let the serializer do the work for you. 让串行器为您完成工作。 Plus, you'll get the automatic help page which will show you exactly how you should serialize the XML for your type. 另外,您将获得自动帮助页面,该页面将确切显示如何为您的类型序列化XML。 Here's another resource for setting up a WCF REST service . 这是用于设置WCF REST服务的另一资源。

Service: 服务:

[OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Upload", ResponseFormat= WebMessageFormat.Xml, RequestFormat= WebMessageFormat.Xml)]
public void Upload(Stream data)
{
    StreamReader reader = new StreamReader(data);
    String res = reader.ReadToEnd();
}

========= =========

Client: 客户:

private HttpClient m_RestHttpClient = new HttpClient("http://localhost:17471/CustomerService/");


var form = new HttpUrlEncodedForm();
form.Add("CustomerCode", txtCustomerCode.Text);
form.Add("CustomerName", txtCustomerName.Text);
form.Add("ContactName", txtContactName.Text);
form.Add("Country", txtCountry.Text);
form.Add("PostalCode", txtPostalCode.Text);
form.Add("ClassTemplate", txtClassTemplate.Text); 
form.Add("BusinessType", cmbBusinessType.Text);
form.Add("IsProspect", cmbIsProspect.Text);

using (HttpResponseMessage response = m_RestHttpClient.Post("Upload", frm.CreateHttpContent()))
{
    response.EnsureStatusIsSuccessful();
}

=============================== ==============================

Output of the text file which was written(by the way this has no limit at all: I can pass as much as many parameters as I want): 写入的文本文件的输出(通过这种方式完全没有限制:我可以根据需要传递尽可能多的参数):

CustomerCode=CUST001&CustomerName=Customer+One&ContactName=Fuebo+Gacia&Country=France&PostalCode=8234994&ClassTemplate=Class+Template&BusinessType=Wholesale&IsProspect=True

---Basically my concern now is how to retrieve these values properly I tried passing an xml string but it also had values with different character formats maybe this needs to be parsed or something. ---基本上我现在关心的是如何正确地检索这些值,我尝试传递xml字符串,但是它也具有具有不同字符格式的值,也许这需要解析或其他。 Hopefully this helps us solve the issue. 希望这可以帮助我们解决问题。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM