質問

複雑なオブジェクトやその他の型を渡すことに懸念があります。なぜなら、私はいつも悪いリクエストを受け取るからです...以下のコードスニペット:

サービス:

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

=========

クライアント:

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

または私はこのように試してみました:

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 個のパラメーターを渡すため、複合型を使用したいと考えています。...または、他に提案がある場合は、お気軽にご提案ください。

ありがとう

よろしく、ラビ

役に立ちましたか?

解決 2

サービス:

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

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

が書かれたテキストファイルの出力(これはすべての制限はありません方法では:私が好きな私は多くのパラメーター限り渡すことができます):

CustomerCode=CUST001&CustomerName=Customer+One&ContactName=Fuebo+Gacia&Country=France&PostalCode=8234994&ClassTemplate=Class+Template&BusinessType=Wholesale&IsProspect=True
---基本的に私の懸念は正しく、これらの値を取得する方法である私は、XML文字列を渡してみましたが、それはまた別の文字書式多分、このニーズに解析するか何かで値を有していました。 うまくいけば、これは、私たちはこの問題を解決するのに役立ちます。

おかげ

他のヒント

複合型を XElement として渡します。これにより、事態がさら​​に複雑になります。厳密に型指定されたオブジェクトを渡すだけです。シリアライザーに作業を任せてください。さらに、型に応じて XML をシリアル化する方法を正確に示す自動ヘルプ ページが表示されます。をセットアップするための別のリソースは次のとおりです。 WCF REST サービス.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top