【问题标题】:How to handle WCF connection when calling method in loop循环调用方法时如何处理WCF连接
【发布时间】:2012-04-19 04:40:31
【问题描述】:

我在 for 循环中调用 WCF 方法。在这方面我有几个问题,

1) 这种情况下,如果在循环中发生错误,在哪里重新打开连接?

2) 在哪里关闭连接?

MyProxy.DemoServiceClient wsDemo = new MyProxy.DemoServiceClient();

foreach (DataRow dataRow in dataTABLE.Rows)
{
    Product product = new Product();

    //Populate product using DataRow.

    try
    {
        wsDemo.CreateProduct(product);
    }
    catch (Exception exc)
    {

    }
}

【问题讨论】:

    标签: wcf-client connection


    【解决方案1】:
    1. 在 catch 中中止并重新打开连接
    2. 您可以关闭循环外的连接。但是,如果您预计长时间处于循环中,那么我更喜欢使用计数器并在每次计数器到达时关闭连接,比如 50。如果连接尚未中止或关闭,请使用 finally 块关闭连接。李>

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多