【问题标题】:Multiple methods with different parameters WCF REST具有不同参数的多种方法 WCF REST
【发布时间】:2013-05-15 09:28:14
【问题描述】:

我被赋予使用 WCF REST 实现以下任务的任务:

资源 POST GET PUT DELETE /device 创建新设备 列出设备 批量更新设备 删除所有设备

这本身不是问题,但问题是所有这些函数都需要不同的参数。例如,POST 方法采用 WSDevice,而 GET 方法采用 WSCollectionQuery 作为参数(用于查询 well.. 集合)。所有 4 种方法都采用不同的参数,但必须通过 /device Uri 访问。

这在 REST 中应该是可能的(根据 http://pages.apigee.com/web-api-design-ebook-thank-you-download.html?aliId=1911411 ,我首先从那里得到了表格。参见第 7 页)。

我目前拥有的:

[OperationContract,
WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "/v" + REST_API_VERSION + "/device/?device={device}")]
WSResult DevicePost(String sessionKey, WSDevice device);

[OperationContract,
WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "/v" + REST_API_VERSION + "/device/?collectionQuery={collectionQuery}")]
WSResult DeviceGet(String sessionKey, WSCollectionQuery collectionQuery);

[OperationContract,
WebInvoke(Method = "PUT",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "/v" + REST_API_VERSION + "/device/?devices={devices}")]
WSResult DevicePut(String sessionKey, WSDevice[] devices);

[OperationContract,
WebInvoke(Method = "DELETE",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "/v" + REST_API_VERSION + "/device/")]
WSResult DeviceDelete(String sessionKey);

所以本质上我希望拥有相同的 UriTemplate,但根据 消息正文 中传递的参数不同的结果。我知道我在 Uri 中添加了上面的参数,但这只是为了尝试区分 Uri。

我得到的错误如下:

UriTemplateTable does not support multiple templates that have equivalent path as template '/v1/device/?device={device}' but have different query strings, where the query strings cannot all be disambiguated via literal values. See the documentation for UriTemplateTable for more detail.

我知道为什么会收到此错误消息。我想知道的是如何解决这个问题?我已经研究过有一个函数采用 Method = "*" 可行,但除了在函数中传递的参数之外,我无法访问任何参数。

如果有人知道这个问题的解决方案,或者可以说如果不是的话,那是不可能的,那将非常非常感谢!

编辑:我现在也知道您不能在 GET 中传递复杂类型,但这是一个可以解决的问题。

【问题讨论】:

    标签: wcf rest parameters uritemplate


    【解决方案1】:

    在这种情况下,最好的解决方案可能是对所有 4 种方法使用完全相同的 UriTemplate:

    UriTemplate = "/v" + REST_API_VERSION + "/device/?device={device}&collectionQuery={collectionQuery}&devices={devices}"
    

    然后您可以检查每种情况下的必要参数。

    不过,为什么这不会引发模棱两可的异常却让我失望。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2017-09-06
      • 2013-02-02
      • 1970-01-01
      • 2017-10-10
      • 2011-12-10
      相关资源
      最近更新 更多