2013-05-15 35 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}" 

然後,您可以爲您在各種情況下的必要參數。

相當爲什麼不拋出曖昧異常甘拜下風但是。