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

前端 未结 2 1343
旧时难觅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:25

    Valid XML must have a single root element. Also there's no magic in WCF REST that maps XML elements to string parameters. You could take an XElement as your operation parameter.

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
    public int GetOne(XElement content)
    {
        string param1 = content.Elements().First(element => element.Name == "param1").Value;
        string param2 = content.Elements().First(element => element.Name == "param2").Value;
    
        return 1;
    }
    

    The data you send would be something like:

    <parameters>
        <param1>something</param1>
        <param2>something</param2>
    </parameters>
    
    0 讨论(0)
  • 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
      
      <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">my param2</string>
      
    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
      
      <ValidateUser xmlns="http://tempuri.org/"><Username>my param1</Username><Password>myparam2</Password></ValidateUser>
      
    0 讨论(0)
提交回复
热议问题