在处理所有数据之前完成SOAP Web服务

时间:2012-03-06 10:06:27

标签: c# asp.net .net web-services enterprise-library

我有一个SOAP Web服务,我在开发环境中进行本地测试。我有一个测试Winforms应用程序,它在localhost上使用这个Web服务。

当调用Web服务时,它意味着从MSSQL 2008 DB中的表读取并返回数据集(此部分正在工作)。

My Winforms应用程序遍历数据集的行,并将数据集的行插入本地数据库。基本上,我通过网络服务将数据从一个数据库中的一个表复制到另一个数据库表(1:1)。

问题..

..是我通过Web服务获得X千行,当我在Winforms应用程序上遍历它们时,代码只停止在200-300行左右循环。我已经使用了Visual Studio的“立即窗口”,并且使用了Visual Studio的“立即窗口”,我已经验证了存在数千行的数据。

该项目是一个旧的.NET 2.0网站,它使用旧版Microsoft Enterprise Library v2.0.50727进行数据访问。

以下是Winforms应用程序中存在问题的代码:

public static int PopulateLocalTable(DataSet ds, string DBInstance, string DBServer)
{
    if (ds.Tables.Count == 0)
    {
        return 0;
    }
    else if (ds.Tables[0].Rows.Count == 0)
    {
        return 0;
    }
    else
    {
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            using (var conn = new SqlConnection(string.Format("Data Source={0}; Initial Catalog={1}; Integrated Security=True;", DBInstance, DBServer)))
            {
                conn.Open();

                using (var command = new SqlCommand("dbo.myStoredProc", conn))
                {
                    command.CommandType = CommandType.StoredProcedure;

                        //Added for debugging purposes.
                    int id = Convert.ToInt32(dr["ID"]);
                    int teamid = Convert.ToInt32(dr["TeamID"]);
                    int companyid = Convert.ToInt32(dr["CompanyID"]);
                    string team_name = Convert.ToString(dr["Team_Name"]);
                    DateTime date = Convert.ToDateTime(dr["Date"]);
                    string aaa = Convert.ToString(dr["aaa"]);
                    int bbb = Convert.ToInt32(dr["bbb"]);
                    decimal ccc = Convert.ToDecimal(dr["ccc"]);
                    DateTime ddd = Convert.ToDateTime(dr["ddd"]);

                    command.Parameters.Add(new SqlParameter("@ID", id));
                    command.Parameters.Add(new SqlParameter("@TeamID", teamid));
                    command.Parameters.Add(new SqlParameter("@CompanyID", companyid));
                    command.Parameters.Add(new SqlParameter("@Team_Name", team_name));
                    command.Parameters.Add(new SqlParameter("@Date", date));
                    command.Parameters.Add(new SqlParameter("@aaa", aaa));
                    command.Parameters.Add(new SqlParameter("@bbb", bbb));
                    command.Parameters.Add(new SqlParameter("@ccc", ccc));
                    command.Parameters.Add(new SqlParameter("@ddd", ddd));

                    command.ExecuteNonQuery();
                }
            }
        }

        return ds.Tables[0].Rows.Count;
    }
}

这是我在Winforms应用程序上的app.config设置:

 <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="Service1Soap" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="6553600" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                        <message clientCredentialType="UserName" algorithmSuite="Default"/>
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:11288/Service1.asmx" binding="basicHttpBinding" bindingConfiguration="Service1Soap" contract="ServiceReference1.Service1Soap" name="Service1Soap"/>
        </client>
    </system.serviceModel>

如果我尝试修改连接/命令超时,我无法使其正常工作。这就像连接超时或流被限制一样。我希望它只是一个配置设置..在此先感谢。

2 个答案:

答案 0 :(得分:1)

您的问题似乎是您在循环外部创建了command,因此最终会添加越来越多的参数。

在循环内创建一个新的command对象。

您可以使用using语句确保清除连接和命令:

using(var connection = new SqlConnection("..."))
{
    connection.Open();

    using(var command = connection.CreateCommand())
    {
        // do stuf
    }
}

更新:我现在注意到参数已被清除。尝试在循环中创建命令:)

答案 1 :(得分:1)

检查Web方法的BufferResponse属性。默认值为TRUE(缓冲响应),但对于大量数据,建议不要缓冲:尝试将其设置为FALSE。

请参阅msdn