WCF REST:期望我的 XML 在请求中看起来像什么? [英] WCF REST: What is it expecting my XML to look like in requests?

查看:24
本文介绍了WCF REST:期望我的 XML 在请求中看起来像什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在从 Flex 应用程序发送 xml,它需要一个如下所示的对象:{ param1: "test", param2: "test2" } 并将其转换为以下请求:

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:

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>

我收到错误 传入的消息具有意外的消息格式原始".操作的预期消息格式为Xml"、Json"..我读过的所有内容都表明我只需要内容类型为 application/xml,但由于某种原因它仍然认为它是原始的.鉴于我的方法签名,我对它期望什么以及我需要如何形成请求以便它接受它作为 XML 感到困惑.

I get the error 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. 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并提供XML时认为它是RAW?

Am I missing something obvious here? Why does it think it's RAW when it's specifying XML and providing XML?

编辑 - 这是 Flex 方面,以防我在这里遗漏了一些东西.

Edit - Here's the Flex side in case I'm missing something here.

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

推荐答案

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

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. 定义您的 WCF 方法如下:

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

您的原始 POST 请求如下所示:

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>

  • 将您的 WCF REST 方法更改为如下所示:

  • 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"}
    

  • 将您的 WCF REST 方法更改为如下所示:

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

  • 这篇关于WCF REST:期望我的 XML 在请求中看起来像什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆