具有不同参数的多个方法WCF REST

时间:2013-05-15 09:28:14

标签: wcf rest parameters uritemplate

我已经完成了使用WCF REST实现以下任务的任务:

Resource       POST                GET           PUT                  DELETE
/device        Create new device   List devices  Bulk update devices  Delete all devices

这本身不是问题,但问题是所有这些功能都需要不同的参数。例如,POST方法采用 WSDevice ,而GET方法采用 WSCollectionQuery 作为参数(用于查询井..集合)。所有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中传递复杂的类型,但这是一个可以解决的问题。

1 个答案:

答案 0 :(得分:0)

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

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

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

然而,为什么这不会引起模棱两可的异常。但

相关问题