简体   繁体   English

WCF REST:我希望我的XML在请求中看起来像什么?

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

I have the following method in my WCF service: 我的WCF服务中有以下方法:

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

I am sending 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: 我从Flex应用程序发送xml,它需要一个如下所示的对象: { param1: "test", param2: "test2" }并将其转换为以下请求:

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 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'. The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. . Everything I've read indicates that I just need the content-type to be application/xml , but it still thinks it's Raw for some reason. 我读过的所有内容都表明我只需要内容类型为application/xml ,但由于某种原因它仍然认为它是Raw。 Given my method signature, I'm confused as to what it's expecting and how I need to form the request so it will accept it as XML. 鉴于我的方法签名,我对它的期望以及我需要如何形成请求感到困惑,因此它将接受它作为XML。

Am I missing something obvious here? 我错过了一些明显的东西吗? Why does it think it's RAW when it's specifying XML and providing XML? 当它指定XML并提供XML时,为什么认为它是RAW?

Edit - Here's the Flex side in case I'm missing something here. 编辑 - 这是Flex方面,以防我在这里遗漏了一些东西。

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

I don't think you can pass 2 parameters with a POST operation for the framework to deserialize it automatically. 我不认为您可以通过POST操作传递2个参数,以便框架自动反序列化它。 You have try some of the below approaches: 您尝试了以下一些方法:

  1. Define your WCF method to be as below: 将WCF方法定义如下:

     [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请求如下所示:

     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: 将您的WCF REST方法更改为如下所示:

     [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: 将您的WCF REST方法更改为如下所示:

     [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> 

Valid XML must have a single root element. 有效的XML必须具有单个根元素。 Also there's no magic in WCF REST that maps XML elements to string parameters. 此外,WCF REST中没有将XML元素映射到字符串参数的魔力。 You could take an XElement as your operation parameter. 您可以将XElement作为操作参数。

[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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM