2011-11-28 64 views
1

我的silverlight應用程序正在連接到以下合同的WCF Web服務方法。我能夠使用wcftestclient.exe正確檢索List<>如何調用從Silverlight返回自定義對象的WCF方法

[OperationContract] 
List<CustomClass> GetCustomObjectsById(int uid); 

但是我不確定如何從我的silverlight客戶端調用此函數。

ServiceReference1.Service1Client sc = new Service1Client(); 

sc.GetCustomObjectsByIdCompleted += new EventHandler<GetCustomObjectsByIdCompletedEventArgs>(sc_GetCustomObjectsByIdCompleted); 

.. 
void sc_GetCustomObjectsByIdCompleted(object sender, GetCustomObjectsByIdCompletedEventArgs e) 
{ 
     List<CustomClass> ac = (List<CustomClass>)e.Result[0]; //How to access here 
} 

編輯: 我打電話這樣的服務:

sc.GetCustomObjectsByIdAsync(3); 

回答

0

你可以通過調用方法

sc.GetCustomObjectsByIdCompleted += new EventHandler<GetCustomObjectsByIdCompletedEventArgs>(sc_GetCustomObjectsByIdCompleted); 
//call the WCF Service method Asynchronously and once the call is complete 
// sc_GetCustomObjectsByIdCompleted method will be called with results 
sc.GetCustomObjectsByIdAsync(); 
+0

正在做的是訂閱完成事件之後的某個時間開始操作。我不確定如何在'sc_GetCustomObjectsByIdCompleted'方法中檢索它 – devnull

2

啓動WCF服務調用的Result屬性是從返回的值你的合同。在這種情況下,你應該能夠只使用:

List<CustomClass> ac = e.Result; 

這也需要您的服務引用配置爲返回集合爲List<T>而是採用陣列 - 詳細信息,請參閱Configure Service Reference Dialog help。 (WCF客戶端可以使用與服務返回的不同集合類型,因爲它們在客戶端被反序列化時被「重構」......默認情況下,返回集合的所有方法都將返回一個數組或Dictionary<T,U>除非您選擇配置它們,否則做。)

請注意,您還需要通過調用sc.GetCustomObjectsByIdAsync();

+0

不應該使用Async方法嗎? – devnull

+0

@neutrino是 - 已更正。只是一個錯字;)我添加了一些其他信息,順便說一句 - 我懷疑問題是你有服務引用配置爲返回一個數組,而不是'列表'... –

+0

我不太明白它。我已經在操作合約和'Service1.svc.cs'文件中指定了返回類型爲'List '。但是當我做'List ac = e.Result;' – devnull

相關問題