【问题标题】:Populate Datagrid from WCF when page is loaded WPF加载页面时从 WCF 填充 Datagrid WPF
【发布时间】:2015-11-13 05:42:52
【问题描述】:

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

怎么了?

这是我的 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.

【问题讨论】:

  • 您的 wcf 服务托管在哪里?可以调试吗?如果是这样,您的请求是否会到达服务器?好像没有。发生这种情况的原因有很多,例如 Web 服务配置问题、网络问题等。附带说明,您需要关闭/处置您的客户端。最好在 using(var client = new MyClient()){//调用服务} 中初始化客户端
  • WCF 服务是本地托管的。我在调试时也看到了这个异常:Inner Exception: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
  • 你调试了什么?您是否按照我的建议调试了 WCF 服务?
  • 我已经调试了客户端和 WCF 服务,我注意到我的查询有问题,因为它显示以下消息:Results View Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation. 如果我从该表中删除所有数据,则会出现故障异常消失
  • 好的。所以现在很明显问题出在您的服务器端。由于服务器端错误,您在客户端看到了之前的异常。希望能帮助您找到根本原因并纠正问题。

标签: c# wpf wcf datagrid


【解决方案1】:

问题已解决...问题出在 getSesizari() 方法中,因为 EntityFramework 创建了类的“代理”。 解决方案是在 Context 构造函数中设置ContextOptions.ProxyCreationEnabled = false;

DataContractSerializer Error using Entity Framework 4.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多