2017-02-15 65 views
0

我試圖消耗WCF PUT服務爲:WCF PUT服務與多個參數

http://dummyurl/EmployeeUpdate?id=99999&item={"var1":true,"var2":1,"var3":1} 

下面是這已經是可用的服務(應該是工作的WCF服務)

[WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{id}")] 
string UpdateEmp(string id, Employee emp); 


public string UpdateEmp(string id, Employee emp) 
    { 

     try 
     { 
    // process data 
     } 
     catch (Exception ex) 
     { 
    // handle exception 
     } 
     return IsSuccess; 
    }  

當我運行該服務時,獲取錯誤消息爲: 異常消息是'System.FormatException:輸入字符串格式不正確。

我試圖弄清楚,但無法修復。發現PUT方法只接受一個參數,服務也定義爲只接收一個參數,但函數由兩個參數定義。

1:我不理解如何通過我的數據作爲一個參數,它是如何在功能解決

請提供此

回答

0

好一些指導,這個異常可以通過2的方式解決 - 你必須只使用一個參數,這是由於UriTemplate:

[WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{id}")] 
string UpdateEmp(string id, Employee emp); 

必須使用這樣的:

WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{id}/{item}")] 
string UpdateEmp(string id, Employee item); 

我已經添加了/ {item},因爲在uri中您有item而不是emp。

2 - 你也可以創建一個新的對象,並把你的參數,它

public class ParamClass 
{ 
    public string id; 
    public Employee emp; 
} 

如果你選擇了這個解決方案,您必須將您的UpdateEmp參數更改相應的這種變化,如:

[WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{emp}")] 
string UpdateEmp(ParamClass emp); 

也不要忘記更改「EmployeeUpdate」參數並在archi的其他部分中使用。

+0

謝謝。 。當我嘗試了選項1以查看它是否有效時,我得到了服務器錯誤,並且不確定此更改是否還有其他服務無法正常工作,因此我已回滾此更改。選項2,真的,我不應該改變接口,因爲它也被其他應用程序佔用。 。強烈地感覺到界面是正確的,我沒有以正確的方式(使用一個參數) –

+0

在Uri'id = 99999&item = {「var1」:true,「var2」:1,「var3」:1}很明顯,需要的是兩個參數:id和項目 – yyg

+0

我將檢查併發布更新。 。謝謝 –