如何调用从Silverlight返回自定义对象的WCF方法

时间:2011-11-28 06:34:20

标签: c# silverlight

我的silverlight应用程序正在连接到以下合同的WCF Web服务方法。我可以使用wcftestclient.exe正确检索List<>

[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);

2 个答案:

答案 0 :(得分:2)

Result属性是合同中返回的值。在这种情况下,您应该只能使用:

  List<CustomClass> ac = e.Result;

这还要求您的服务引用配置为将集合返回为List<T>而不是使用Array - 有关详细信息,请参阅Configure Service Reference Dialog help。 (WCF客户端可以使用与服务返回的集合类型不同的集合类型,因为它们在客户端进行反序列化时“重构”...默认情况下,返回集合的所有方法都将返回一个数组或{ {1}}除非您选择将其配置为其他方式。)

请注意,您还需要在订阅完成事件后的某个时间通过调用Dictionary<T,U>启动操作

答案 1 :(得分:0)

您可以通过调用方法

来启动WCF服务调用
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();