2013-05-15 33 views
2

我一直在考虑实施以下使用WCF REST任务:具有不同参数的多种方法WCF REST

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

这不是每本身有问题,但问题是,所有这些功能需要不同的参数。例如,该POST方法以一个 WSDevice,而GET方法以一个 WSCollectionQuery作为参数(用于查询以及..集合)。所有4种方法都采用不同的参数,但必须通过 /设备 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传递复杂类型,但是这就是可以被加工的问题。

回答

0

大概在这种情况下,最好的解决办法是使用完全相同的同UriTemplate所有4种方法:

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

然后,您可以为您在各种情况下的必要参数。

相当为什么不抛出暧昧异常甘拜下风但是。