Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WCF REST 4.0中发布复杂类型/HttpContent?_C#_Http_Rest_Parameters_Complextype - Fatal编程技术网

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

C# 在WCF REST 4.0中发布复杂类型/HttpContent?,c#,http,rest,parameters,complextype,C#,Http,Rest,Parameters,Complextype,我担心传递复杂对象/任何其他类型:因为我总是收到错误的请求…下面的代码片段: 服务: [OperationContract] [WebInvoke(UriTemplate = "saveXML/", Method="POST", BodyStyle= WebMessageBodyStyle.Bare)] bool saveXML(XElement xmlString) { return true; } [OperationContract] [WebInvoke(Method =

我担心传递复杂对象/任何其他类型:因为我总是收到错误的请求…下面的代码片段:

服务:

[OperationContract]
[WebInvoke(UriTemplate = "saveXML/", Method="POST", BodyStyle= WebMessageBodyStyle.Bare)]
bool saveXML(XElement xmlString)
{
       return true;
}
[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();
}
=========

客户:

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();
}
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();
}
或者我这样试过:

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());
…这两种方法都不起作用…这只是一个示例,我想使用复杂类型,因为我将传递至少50个参数…或者如果您有任何其他建议,请随意建议

多谢各位

致以最良好的祝愿,
Ravi

您将复杂类型作为XElement传递-这将使事情更加复杂。只需传递强类型对象。让序列化程序为您完成这项工作。另外,您将获得自动帮助页面,该页面将准确显示您应该如何序列化类型的XML。下面是另一个用于设置。

服务的资源:

[OperationContract]
[WebInvoke(UriTemplate = "saveXML/", Method="POST", BodyStyle= WebMessageBodyStyle.Bare)]
bool saveXML(XElement xmlString)
{
       return true;
}
[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();
}
=========

客户:

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();
}
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();
}
===============================

写入的文本文件的输出(顺便说一下,这完全没有限制:我可以传递任意多个参数):

---基本上,我现在关心的是如何正确地检索这些值,我尝试传递一个xml字符串,但它也有不同字符格式的值,可能需要对这些值进行解析或其他什么。 希望这能帮助我们解决这个问题


谢谢

谢谢Steve,但我如何在客户端实现这一点。。比如说在一个简单的windows应用程序中。关于Person实体,我可以这样创建它:公共类Customer{public string CustomerCode;public string CustomerName;public string Address;public string Country;}如果有人能告诉我如何在客户端应用程序中实现这一点,那将是非常有帮助的。Thanksim使用WCF REST 4.0,Win应用程序C#。。。基本上,我只是想做一个概念证明,在这里我可以传递这样的参数,而无需与真实实体或数据库建立任何连接。是的,你可以创建一个类似这样的类。将“参数”作为对象传递会更容易(长参数列表会很快变得难看)。此外,codeplex上的WCF REST Starter工具包具有“将XML粘贴为类型”功能,该功能将自动转换和XML片段,您可以将其用作客户端代理类。如果您完全关注参数,则可以在WebGet/WebInvoke属性中使用标记。请注意,大括号内的“id”参数与传递给方法的id参数匹配。[WebGet(UriTemplate=“Person/{id}”)]public Person GetPerson(string id)我试过发布xml,但唯一得到的是一个错误的请求。HttpUrlEncodedForm-->怎么样?我可以在任何情况下使用它吗?如果你能给我一个清晰的例子,那就更好了,因为我现在还不清楚我该如何构建它。谢谢为什么要处理流,当你可以处理一个强类型的对象,这使得代码更容易处理?我试着用一个强类型的对象来处理它…但它不起作用…如果你能给我展示一个关于如何实现它的示例项目,我将不胜感激。。我查看了你的博客,这很有帮助,但我相信缺少的是如何用客户端实现这一点,以及它们的未声明URI/操作契约/Web调用方法。