【问题标题】:Upload file together with multiple parameters using restful wcf使用restful wcf上传文件和多个参数
【发布时间】:2013-03-09 07:59:19
【问题描述】:

我正在尝试使用 Visual Studio 2012 和 .net Framework 4.5 创建 wcf 休息服务。 该服务将尝试上传包含 4 个或更多参数的文件。我只想在一个电话中做到这一点。我想使用http“put”方法。但我一直有这两个错误。

合约 'IRestBasketService' 中的操作 'AddFile' 有多个请求体参数,其中一个是 Stream。当 Stream 为参数时,body 中不能有其他参数。 (当我使用 BodyStyle 包裹时)

合约 'IRestBasketService' 的操作 'AddFile' 指定要序列化的多个请求主体参数,而无需任何包装器元素。最多一个 body 参数可以在没有包装元素的情况下被序列化。删除额外的正文参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped。 (当我使用 BodyStyle 裸露时)

我该怎么办?

下面是给出错误的 2 个签名

 [WebInvoke(Method = "PUT",
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "AddFile?key={key}&email={email}&fileName={fileName}&groupID={groupID}&ID={objectID}")]
  Response AddFile(string key, string email, string fileName, string type, string objectID, string groupID, Stream fileStream );

[WebInvoke(Method = "PUT",
           BodyStyle = WebMessageBodyStyle.Wrapped,
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,
           UriTemplate = "/AddFile")]
 Response AddFile(AddFileRequest request, Stream FileStream);

我在 web.config 中使用 webHttpBinding 和 webHttp 行为。

【问题讨论】:

  • 似乎在 UriTemplate 中我遗漏了一个参数(类型)。
  • 但是我仍然可以将 json 与流对象一起使用吗?

标签: wcf rest parameters stream


【解决方案1】:

由于您使用 Stream 作为输入参数之一,因此正文中不能有其他参数。您需要将所有额外数据作为流的一部分添加到客户端,然后在服务中对其进行解析。

1. Similar problem 2. Similar problem

【讨论】:

    猜你喜欢
    • 2011-11-04
    • 2014-03-04
    • 2017-05-06
    • 2023-03-18
    • 1970-01-01
    • 2012-12-04
    • 2012-02-05
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多