在WCF / .NET返回数据表数据表、WCF、NET

由网友(活着便精采)分享简介:我有,我想返回一个DataTable的WCF服务。我知道,这往往是一个高度争论的话题,至于是否返回数据表是一个很好的做法。让我们把那个放在一边了一会儿。I have a WCF service from which I want to return a DataTable. I know that this is o...

我有,我想返回一个DataTable的WCF服务。我知道,这往往是一个高度争论的话题,至于是否返回数据表是一个很好的做法。让我们把那个放在一边了一会儿。

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.

当我从头开始创建数据表,如下,没有出现任何问题。表被创建,填充并返回到客户端,并且一切都很好:

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.

由于达伦建议和保罗备份,在Max..Size性质所需的配置被放大。该SvcTraceViewer工具有助于确定这一点,但它仍然并不总是提供最有用的错误消息。 似乎也当服务参考更新客户端,配置有时不能正确更新(如在服务器上修改配置值不会总是在客户端上适当更新,我不得不去和改变Max..Size性能多次在客户端和服务器端无论是在我的调试过程中)

有关数据表可序列化,它需要被赋予一个名称。默认的构造不给表的名称,所以: 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();

不会被序列化,而:

will not be serializable, while:

return new DataTable("someName");

将其命名无论是作为参数传递的表。

will name the table whatever is passed as the parameter.

请注意通过指定一个字符串数据表的表名属性表可以在任何时候给一个名字。

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";

希望这将帮助别人。

阅读全文

相关推荐

最新文章