2016-11-18 26 views
0

RESTful WCFサービスを作成し、Fiddlerを使用してPOSTリクエストにパラメーターとしてクラスを渡そうとしましたが、次のようなエラーが発生しました: "HTTP/1.1 400不正な要求」POST要求のあるRESTful WCFサービスへのクラス・オブジェクトの受け渡し

インタフェース - IXmlService.cs

<code> 

    [ServiceContract()] 
     public interface IXmlService 
     { 
      [OperationContract(Name = "Read")] 
      [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Read", BodyStyle = WebMessageBodyStyle.Wrapped)] 
      bool ReadData(Order data); 

      [OperationContract(Name = "Generate")] 
      [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Generate/")] 
      bool GenerateXml(); 
     } 
</code> 

実装 - XmlService.cs

<code> 

    public class XmlService : IXmlService 
     { 
      public bool ReadData(Order data) 
      { 
       bool result = false; 
       var path = "@C:\\order.xml"; 
       XmlSerializer serializer; 
       TextWriter writer; 

       try 
       { 
        if (data != null) 
        { 
         //serializer = new XmlSerializer(typeof(Order), new XmlRootAttribute("HEADER")); 
         serializer = new XmlSerializer(typeof(Order)); //No need to provide XmlRootAttribute as I have defined it on Order Model. 
         writer = new StreamWriter(path); 
         serializer.Serialize(writer, data); 
         result = true; 
        } 
       } 
       catch (Exception) 
       { 
        throw; 
       } 
       return result; 
      } 

      public bool GenerateXml() 
      { 
       throw new NotImplementedException(); 
      } 
     } 

</code> 

データモデル - Order.cs

<code> 

    [XmlRootAttribute("OrderDetails", Namespace = "http://www.ProofOfConcept.com", IsNullable = false)] 
     [DataContract] 
     public class Order 
     { 
      // The XmlArrayAttribute changes the XML element name 
      // from the default of "OrderedItems" to "Items". 
      [XmlElement("OrderId")] 
      [Key] 
      [DataMember] 
      public int OrderId { get; set; } 

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

      // Setting the IsNullable property to false instructs the 
      // XmlSerializer that the XML attribute will not appear if 
      // the City field is set to a null reference. 
      [XmlElementAttribute(IsNullable = false)] 
      [DataMember] 
      public string Info { get; set; } 

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

      //[DataMember] 
      //public DateTime CreatedOn { get; set; } 
     } 

</code> 

のWeb.config

<code> 

     <system.serviceModel> 
     <services> 
      <service name="WCF_XML_Service.XmlService" behaviorConfiguration="ServiceBehavior"> 
      <!-- Service Endpoints --> 
      <host> 
       <baseAddresses> 
       <add baseAddress="http://localhost:16999"/> 
       </baseAddresses> 
      </host> 

      <endpoint address="/xml/" binding="webHttpBinding" contract="WCF_XML_Service.IXmlService" behaviorConfiguration="Web"/> 
      </service> 
     </services> 

     <behaviors> 
      <serviceBehaviors> 
      <behavior name="ServiceBehavior"> 
       <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
       <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
       <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
      </serviceBehaviors> 

      <endpointBehaviors> 
      <behavior name="Web"> 
       <webHttp helpEnabled="true"/> 
      </behavior> 
      </endpointBehaviors> 
     </behaviors> 

</code> 

私のWeb.Configの 'webHttpBinding' に結合変換によって異なるアプローチを試みました。また、 'BodyStyle = WebMessageBodyStyle.Wrapped'をWebInvoke属性に追加しようとしましたが、まだフィドラーを使用してサービスにヒットできませんでした。

フィドラー要求:

<code> 
Url - http://localhost:16999/XmlService.svc/xml/Read 
Method - POST 
Request Header: 
User-Agent: Fiddler 
Host: localhost:16999 
Content-Type: text/xml 
Content-Length: 155 

Request Body: 
    { 
    "OrderId": "1", 
    "Owner": "Sam Shipping", 
    "Info": "First delivery shipment", 
    "Recipient": "Singapore Shipping Corporation" 
    } 
    </code> 
+0

http://stackoverflow.com/questions/40505971/wcf-method-not-erroring-but-not-receiving-myに今から日常的に私の眼鏡をきれいにする必要があります-json/40506041#40506041 –

+0

@Div HTTP 400を受信した場合、サービスコードがヒットしていない。 –

+0

@KirkBroadhurst、ええ、教えてくれてありがとう。 –

答えて

0

まあ、私は最終的に "コンテンツタイプ" で、エラーを考え出し3日無駄にした後:「コンテンツタイプでなければならJSONデータ用: を私は "text/xmlでのContent-Type" を使用していました:text/json "


の推測では、私は:)

0

利用OrderId代わりのIdパス代わりにそれintGuid。それが助けにならない場合は、を一時的にヌル入力可能なDateTimeにして、それを日付体裁の場合に備えてPOST本体から削除してください。 CreatedOnを変更することができない場合は、「1/1/2013 ...」(曜日と月は< = 12)のような「重要ではない」値を渡します。

これが役に立ちます。

+0

IdをOrderIdとintに変換してモデルを変換しようとしました。 CreatedOnプロパティも削除されましたが、 "HTTP/1.1 400 Bad Request"の問題が発生しました。 JSONは、 - "OrderId": "1"、 "Owner": "Sam Shipping"、 "Info": "First delivery shipment"、 "受取人": "Singapore Shipping Corporation" } –

関連する問題