在页面加载WPF时从WCF填充Datagrid

时间:2015-11-13 05:42:52

标签: c# wpf wcf datagrid

我想在加载窗口或用户单击按钮时通过WCF服务从数据库中填充一些Datagrid,但是我收到以下消息:

enter image description here

enter image description here

有什么问题?

这是来自WCF的代码:

 public IEnumerable<sesizari> getSesizari()
      {
          try
          {
           IEnumerable<sesizari> query = from sel in dataP.sesizaris select sel;
           return query;
           
          }
          catch(FaultException ex)
              {
              throw ex;
              }
      }

客户代码:

  private void Service_Window_Loaded(object sender, RoutedEventArgs e)
        {
          try  {

            GridView gridView = new GridView();
           SesizariList.View = gridView;
            gridView.Columns.Add(new GridViewColumn
            {
                Header = "Id Sesizare",
                DisplayMemberBinding = new Binding("id_sesizare")
            });
            gridView.Columns.Add(new GridViewColumn
            {
                Header = "Titlu",
                DisplayMemberBinding = new Binding("titlu"),

            });
            gridView.Columns.Add(new GridViewColumn
            {
                Header = "Client",
                DisplayMemberBinding = new Binding("client"),

            });
           
            SesizariList.ItemsSource = client.getSesizari();
            SesizariList.Items.Refresh();
      }
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
        }

我启用了跟踪日志记录,问题出在getSesizari()方法,但我不知道出了什么问题......

There was an error while trying to serialize parameter http://tempuri.org/:getSesizariResult.
 The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.sesizari_3A8E1D8187ED0306632025D5E2C490F13F4A3E7EF93AFE8F6B9C7DE55AFA8511'
 with data contract name sesizari_3A8E1D8187ED0306632025D5E2C490F13F4A3E7EF93AFE8F6B9C7DE55AFA8511:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 
is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example,
 by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. 
 Please see InnerException for more details.

1 个答案:

答案 0 :(得分:0)

问题解决了...问题出在getSesizari()方法中,因为EntityFramework创建了一个&#39;代理&#39;班上的。 解决方案是在Context构造函数中设置ContextOptions.ProxyCreationEnabled = false;

DataContractSerializer Error using Entity Framework 4.0