尝试在Silverlight应用程序中使用WCF服务时出错

时间:2010-07-08 18:14:58

标签: silverlight wcf

我在Silverlight应用中遇到错误:

     An exception occurred during the operation, making the result invalid.  
Check InnerException for exception details.

       at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
       at SecretaryAppNav.ServiceReference1.GetChildByFirstnameCompletedEventArgs.get_Result()
       at SecretaryAppNav.Views.FindChild.client_GetChildByFirstnameCompleted(Object sender, GetChildByFirstnameCompletedEventArgs e)
       at SecretaryAppNav.ServiceReference1.Service1Client.OnGetChildByFirstnameCompleted(Object state)

按下按钮时出现。此操作会导致使用wcf服务:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            //BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            //EndpointAddress endpointAddress = new EndpointAddress("http://localhost:15371/Service1.svc");
            //IService1 personService = new ChannelFactory<IService1>(basicHttpBinding, endpointAddress).CreateChannel();
            //personService.BeginGetChildByFirstname(this.tbChildName.Text, 
            //    delegate(IAsyncResult result)
            //{
            //    IList<Dziecko> p = ((IService1)result.AsyncState).EndGetChildByFirstname(result);
            //    this.Dispatcher.BeginInvoke(
            //        delegate()
            //        {
            //            this.tbChildSurname.Text = p.ElementAtOrDefault<Dziecko>(0).Nazwisko;
            //        }
            //    );
            //}, personService);
            Service1Client client = new Service1Client();
            client.GetChildByFirstnameCompleted += new EventHandler<GetChildByFirstnameCompletedEventArgs>(client_GetChildByFirstnameCompleted);
            client.GetChildByFirstnameAsync(this.tbChildName.Text.ToString());
        }

        void client_GetChildByFirstnameCompleted(object sender, GetChildByFirstnameCompletedEventArgs e)
        {
            this.dataGridChild.DataContext = (IList<Dziecko>)e.Result;
        }

当我在控制台应用程序中使用服务时一切正常,但在SL中我有错误:/ 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

当你的查询似乎说你只有一个孩子(Dziecko =孩子,没有?)时,你确定你得到了一个List吗?

也许你应该将e.Result投射到Dziecko:

this.dataGridChild.DataContext = (Dziecko)e.Result;

但正如大卫所说,我们需要使用InnerException来查看出错的地方。