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

Oce*_*t20 6 c# apache-flex asp.net wcf wcf-rest

我的WCF服务中有以下方法:

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

我从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>
Run Code Online (Sandbox Code Playgroud)

我收到了错误The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'..我读过的所有内容都表明我只需要内容类型application/xml,但由于某些原因它仍然认为它是Raw.鉴于我的方法签名,我对它的期望以及我需要如何形成请求感到困惑,因此它将接受它作为XML.

我错过了一些明显的东西吗?当它指定XML并提供XML时,为什么认为它是RAW?

编辑 - 这是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" });
Run Code Online (Sandbox Code Playgroud)

Raj*_*esh 4

我不认为你可以通过一个POST操作传递 2 个参数,让框架自动反序列化它。您已尝试以下一些方法:

  1. 定义您的 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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    您的原始 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>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将您的 WCF REST 方法更改为如下:

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

    现在您的原始请求应如下所示:

    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"}
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将您的 WCF REST 方法更改为如下:

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

    现在您的原始请求将如下所示:

    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>
    
    Run Code Online (Sandbox Code Playgroud)