6

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

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

我正在从 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>

我得到错误The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.。我读过的所有内容都表明我只需要 content-type 是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" });
4

2 回答 2

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

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

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

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

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

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

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

    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>
    
于 2012-07-12T08:48:18.917 回答
1

有效的 XML 必须有一个根元素。WCF REST 中也没有将 XML 元素映射到字符串参数的魔法。您可以将 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;
}

您发送的数据将类似于:

<parameters>
    <param1>something</param1>
    <param2>something</param2>
</parameters>
于 2012-07-11T22:20:53.847 回答