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

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

问题描述

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

  [OperationContract的]
[WebInvoke(方法=POST,BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Xml,RequestFormat = WebMessageFormat.Xml)
公众诠释GetOne(字符串参数1,字符串参数2)
{
    返回1;
}

我从Flex应用程序发送XML,它需要一个对象,它看起来像这样: {参数1:测试,参数2:测试2} 和曲折它分为以下几个要求:

  POST HTTP://本地主机:8012 / MyService.svc / GetOne HTTP / 1.1
接受:应用/ XML
接受语言:EN-US
的X闪光版本:10,1,53,64
内容类型:应用程序/ XML
内容长度:52
接受编码:gzip,紧缩
用户代理:Mozilla的/ 5.0(兼容; MSIE 9.0; Windows NT的6.1; WOW64;三叉戟/ 5.0)
主机:本地主机:8012
连接:保持活动
编译:无缓存
饼干:ASP.NET_SessionId = drsynacw0ignepk4ya4pou23<&参数1 GT;&东西LT; /参数1><参数2>&东西LT; /参数2>

我得到的错误传入的消息有一个意外的消息格式原始。该操作的预期的消息格式的Xml','的Json。。我读过的一切表明,我只是需要内容类型为应用程序/ XML ,但它仍然认为这是原始的某些原因。鉴于我的方法签名,我很困惑,就是它的预期,以及如何我需要形式的请求,所以它会接受它作为XML。

我在这里丢失了一些东西明显?为什么它认为这是RAW时,它的指定XML和XML提供?

修改 - 这里的Flex端,以防我在这里失去了一些东西。

  VAR getOneService:HttpService的=新的HTTPService(myURL);getOneService.method =POST;
getOneService.resultFormat =E4X;
getOneService.contentType = HTTPService.CONTENT_TYPE_XML;
getOneService.headers = {接受:应用程序/ XML};getOneService.send({参数1:测试,参数2:测试2});


解决方案

我不认为你可以传递两个参数与 POST 操作来反序列化框架自动。你必须尝试一些下面的办法:


  1. 定义你的WCF方法如下是:

      [OperationContract的]
    [WebInvoke(方法=POST
        BodyStyle = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Xml,
        URITemplate =/ GetOne / {参数1})]
    公众诠释GetOne(字符串参数1,字符串参数2)
    {
        返回1;
    }

    您原始的POST请求将看起来像如下:

      POST HTTP://本地主机/ SampleService / RestService /的ValidateUser / myparam1 HTTP / 1.1
    用户代理:提琴手
    内容类型:应用程序/ XML
    主持人:本地主机
    内容长度:86<字符串的xmlns =htt​​p://schemas.microsoft.com/2003/10/Serialization/>我param2的< /串>


  2. 更​​改WCF REST方法如下是:

      [OperationContract的]
    [WebInvoke(方法=POST
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)
    公众诠释GetOne(字符串参数1,字符串参数2)
    {
        返回1;
    }

    现在您的原始请求应该看起来像如下:

      POST HTTP://本地主机/ SampleService / RestService /的ValidateUser HTTP / 1.1
    用户代理:提琴手
    内容类型:应用程序/ JSON
    主持人:本地主机
    内容长度:86{参数1:我的参数1,参数2:我的参数2}


  3. 更​​改WCF REST方法如下是:

      [OperationContract的]
    [WebInvoke(方法=POST
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Xml)
    公众诠释GetOne(字符串参数1,字符串参数2)
    {
       返回1;
    }

    现在您的原始请求看起来像下面的内容:

      POST HTTP://本地主机/ SampleService / RestService /的ValidateUser HTTP / 1.1
    用户代理:提琴手
    内容类型:应用程序/ XML
    主持人:本地主机
    内容长度:116<的ValidateUser的xmlns =htt​​p://tempuri.org/><用户名>我参数1< /用户名><密码> myparam2< /密码>< /的ValidateUser>


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

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.

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

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

解决方案

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

    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>
    

  2. 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"}
    

  3. 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天全站免登陆