WCF Web服务的响应大小限制

我使用basicHttpBinding在IIS中托管WCF服务。 WCF Web服务使用ADO.Net查询后端SQL Server 2008,并将DataTable返回到WCF服务的客户端。

我发现当返回的DataTable很大时,会有exception表示http连接被IIS关闭。 任何想法有什么问题以及如何设置更大的响应大小? 另一个想法是对象的序列化大小是否有任何硬性限制(我想也许DataTable实例太大而不能序列化?)

IIS返回的错误信息是:

exception消息:

收到http://labmachine1/service.svc的HTTP响应时发生错误。 这可能是由于服务端点绑定不使用HTTP协议。 这也可能是由于服务器中止HTTP请求上下文(可能是由于服务关闭)。 请参阅服务器日志以获取更多详

{“底层连接已关闭:接收时发生意外错误。”}

这是我的服务器端的完整源代码,对于我在web.config中托管的服务器,默认值没有变化。 由于我在IIS中托管,我使用basicHttpBinding。

public class StudentManagement : IStudentManagement { public DataTable Poll(int Id) { return MakeParentTable(); } private DataTable MakeParentTable() { // Create a new DataTable. System.Data.DataTable table = new DataTable("ParentTable"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = true; column.Unique = true; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "ParentItem"; column.AutoIncrement = false; column.Caption = "ParentItem"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); // Make the ID column the primary key column. DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = table.Columns["id"]; table.PrimaryKey = PrimaryKeyColumns; // Create three new DataRow objects and add // them to the DataTable for (int i = 0; i <= 1000000; i++) { row = table.NewRow(); row["id"] = i; row["ParentItem"] = "ParentItem " + i; table.Rows.Add(row); } return table; } } 

客户端代码:

 static void Main(string[] args) { StudentIdentifier identifier = new StudentIdentifier(); identifier.Id = 100; StudentManagementClient client = new StudentManagementClient(); DataTable student = client.Poll(identifier); Console.WriteLine(student.Rows.Count); } 

客户端web.config:

    

编辑2:

客户端app.config的流模式配置,

           

用于流模式的服务器端web.config,

           

您可以使用多种缓冲区大小 – 默认情况下它们保持相当小(64K)以避免拒绝服务攻击,但如果您需要,可以增加以下内容:

          

看一下绑定中的“MaxBufferSize”,“MaxBufferPoolSize”,“MaxReceivedMessageSize”设置,以及部分中的各种设置。

我会尝试首先增加“MaxBufferSize”和“MaxBufferPoolSize”,看看这是否有帮助 – 大多数其他设置应该更加适应服务接收和需要处理消息的时间。

对于序列化的对象有多大没有硬性限制。 但是,DataTable确实带来了很大的开销,如果你真的不需要客户端的DataTablefunction,你也可以在服务器上进行转换,只发回一个集合或通用的对象列表 – 而不是重型DataTable对象。

此外,如果您经常发送大量数据,您可能也需要调查WCF的流媒体function(例如,如果您返回图片,video,那种东西)。 渣

乔治,当这样的事情发生时,请务必查看事件日志。 查看WCF或任何其他组件是否已记录错误。

此外,尝试打开W​​CF跟踪并查看是否存在未以某种方式记录的内部错误。 您可能需要使跟踪详细,以便查看它,但看一看,看看有什么。

仅仅因为你从IIS中得到了一个模糊的错误,并不意味着某些地方没有更多更好的信息。 毕竟,错误消息确实说:“请参阅服务器日志以获取更多详细信息。”

请参阅以下代码段..

                   

也许对某人有帮助。

我有同样的错误。 它是通过在服务器端web.config中设置MaxItemsInObjectGraph属性(为大值)来解决的:

             

从这里得到它: http : //forums.asp.net/post/4948029.aspx