在 WCF/.NET 中返回数据表 [英] Returning DataTables in WCF/.NET

查看:25
本文介绍了在 WCF/.NET 中返回数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,我想从中返回一个 DataTable.我知道这通常是一个备受争议的话题,至于返回 DataTables 是否是一个好的做法.让我们暂时把它放在一边.

I have a WCF service from which I want to return a DataTable. I know that this is often a highly-debated topic, as far as whether or not returning DataTables is a good practice. Let's put that aside for a moment.

当我从头开始创建 DataTable 时,如下所示,没有任何问题.表被创建、填充并返回给客户端,一切正常:

When I create a DataTable from scratch, as below, there are no problems whatsoever. The table is created, populated, and returned to the client, and all is well:

[DataContract]
public DataTable GetTbl()
{
    DataTable tbl = new DataTable("testTbl");
    for(int i=0;i<100;i++)
    {
        tbl.Columns.Add(i);
        tbl.Rows.Add(new string[]{"testValue"});
    }
    return tbl;
}

但是,一旦我出去点击数据库创建表,如下所示,我得到一个CommunicationException底层连接已关闭:连接意外关闭."

However, as soon as I go out and hit the database to create the table, as below, I get a CommunicationException "The underlying connection was closed: The connection was closed unexpectedly."

[DataContract]
public DataTable GetTbl()
{
    DataTable tbl = new DataTable("testTbl");
    //Populate table with SQL query

    return tbl;
}

该表正在服务器端正确填充.它比我循环并返回的测试表小得多,而且查询又小又快——这里没有超时或大数据传输的问题.正在使用完全相同的函数和 DataContracts/ServiceContracts/BehaviorContracts.

The table is being populated correctly on the server side. It is significantly smaller than the test table that I looped through and returned, and the query is small and fast - there is no issue here with timeouts or large data transfer. The same exact functions and DataContracts/ServiceContracts/BehaviorContracts are being used.

为什么表格的填充方式会影响表格的成功返回?

Why would the way that the table is being populated have any bearing on the table returning successfully?

推荐答案

对于任何有类似问题的人,我已经解决了我的问题.这是好几倍.

For anyone having similar problems, I have solved my issue. It was several-fold.

  • 正如 Darren 建议和 Paul 所支持的,配置中的 Max..Size 属性需要扩大.SvcTraceViewer 实用程序有助于确定这一点,但它仍然不总是提供最有用的错误消息.
  • 似乎在客户端更新服务引用时,配置有时不会正确更新(例如,更改服务器上的配置值并不总是在客户端上正确更新.我不得不进去更改Max..Size 属性在我的调试过程中多次在客户端和服务器端)
  • 要使 DataTable 可序列化,需要为其指定名称.默认构造函数没有给表命名,所以:

  • As Darren suggested and Paul backed up, the Max..Size properties in the configuration needed to be enlarged. The SvcTraceViewer utility helped in determining this, but it still does not always give the most helpful error messages.
  • It also appears that when the Service Reference is updated on the client side, the configuration will sometimes not update properly (e.g. Changing config values on the server will not always properly update on the client. I had to go in and change the Max..Size properties multiple times on both the client and server sides in the course of my debugging)
  • For a DataTable to be serializable, it needs to be given a name. The default constructor does not give the table a name, so:

return new DataTable();

不会被序列化,同时:

return new DataTable("someName");

将命名表作为参数传递的任何内容.

will name the table whatever is passed as the parameter.

请注意,可以通过将字符串分配给 DataTable 的 TableName 属性来随时为表命名.

Note that a table can be given a name at any time by assigning a string to the TableName property of the DataTable.

var table = new DataTable();
table.TableName = "someName";

希望这会帮助某人.

这篇关于在 WCF/.NET 中返回数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆