gpt4 book ai didi

c# - WCF 休息 : What is it expecting my XML to look like in requests?

转载 作者:太空狗 更新时间:2023-10-29 21:40:39 24 4
gpt4 key购买 nike

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

我收到错误 传入的消息具有意外的消息格式“原始”。该操作的预期消息格式为“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" });

最佳答案

我认为您不能通过 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>

关于c# - WCF 休息 : What is it expecting my XML to look like in requests?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11439367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com