WCF REST: what expects my XML to look in requests?

I have the following method in my WCF service:

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

I send xml from a Flex application and it takes an object that looks like this: { param1: "test", param2: "test2" } and turns it into the following request:

 POST http://localhost:8012/MyService.svc/GetOne HTTP/1.1 Accept: application/xml Accept-Language: en-US x-flash-version: 10,1,53,64 Content-Type: application/xml Content-Length: 52 Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Host: localhost:8012 Connection: Keep-Alive Pragma: no-cache Cookie: ASP.NET_SessionId=drsynacw0ignepk4ya4pou23 <param1>something</param1><param2>something</param2> 

I get the error message The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. . Everything I read indicates that I just need the content type application/xml , but still it still considers it Raw. Given my method signature, I am confused as to what it expects and how I need to formulate the request so that it accepts it as XML.

Did I miss something obvious here? Why does he consider this RAW when he defines XML and provides XML?

Change Here's the Flex side in case I missed something.

 var getOneService:HttpService = new HttpService("myURL"); getOneService.method = "POST"; getOneService.resultFormat = "e4x"; getOneService.contentType = HTTPService.CONTENT_TYPE_XML; getOneService.headers = { Accept: "application/xml" }; getOneService.send({ param1: "test", param2: "test2" }); 
+6
source share
2 answers

I don’t think that you can pass 2 parameters using the POST operation so that the infrastructure automatically deserializes it. You have tried some of the following approaches:

  • Define your WCF method as follows:

     [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 will look like this:

     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> 
  • Change your WCF REST method as follows:

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

    Your raw request should now look something like this:

     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"} 
  • Change your WCF REST method as follows:

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

    Your raw request will now look like this:

     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> 
+4
source

Valid XML must have one root element. There is also no magic in WCF REST, which maps XML elements to string parameters. You can use XElement as your working 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 will look something like this:

 <parameters> <param1>something</param1> <param2>something</param2> </parameters> 
+1
source

Source: https://habr.com/ru/post/920255/


All Articles