WCF REST: What is it expecting my XML to look like in requests?

前端 未结 2 1345
旧时难觅i
旧时难觅i 2021-02-09 16:18

I have the following method in my WCF service:

[OperationContract]
[WebInvoke(Method = \"POST\", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessag         


        
2条回答
  •  礼貌的吻别
    2021-02-09 16:37

    I don't think you can pass 2 parameters with a POST operation for the framework to deserialize it automatically. You have try some of the below approaches:

    1. Define your WCF method to be as below:

      [OperationContract]
      [WebInvoke(Method = "POST", 
          BodyStyle = WebMessageBodyStyle.Bare, 
          ResponseFormat = WebMessageFormat.Xml, 
          RequestFormat = WebMessageFormat.Xml, 
          URITemplate="/GetOne/{param1}")]
      public int GetOne(string param1, string param2)
      {
          return 1;
      }
      

      Your raw POST request would looks like as below:

      POST http://localhost/SampleService/RestService/ValidateUser/myparam1 HTTP/1.1
      User-Agent: Fiddler
      Content-Type: application/xml
      Host: localhost
      Content-Length: 86
      
      my param2
      
    2. Change your WCF REST method to be as below:

      [OperationContract]
      [WebInvoke(Method = "POST", 
          BodyStyle = WebMessageBodyStyle.WrappedRequest, 
          ResponseFormat = WebMessageFormat.Json, 
          RequestFormat = WebMessageFormat.Json)]
      public int GetOne(string param1, string param2)
      {
          return 1;
      }
      

      Now your raw request should looks something like below:

      POST http://localhost/SampleService/RestService/ValidateUser HTTP/1.1
      User-Agent: Fiddler
      Content-Type: application/json
      Host: localhost
      Content-Length: 86
      
      {"param1":"my param1","param2":"my param 2"}
      
    3. Change your WCF REST method to be as below:

      [OperationContract]
      [WebInvoke(Method="POST", 
          BodyStyle=WebMessageBodyStyle.WrappedRequest, 
          ResponseFormat=WebMessageFormat.Xml, 
          RequestFormat= WebMessageFormat.Xml)]
      public int GetOne(string param1, string param2)
      {
         return 1;
      }
      

      Now your raw request would look like something below:

      POST http://localhost/SampleService/RestService/ValidateUser HTTP/1.1
      User-Agent: Fiddler
      Content-Type: application/xml
      Host: localhost
      Content-Length: 116
      
      my param1myparam2
      

提交回复
热议问题