循环调用方法时如何处理WCF连接 [英] How to handle WCF connection when calling method in loop

查看:114
本文介绍了循环调用方法时如何处理WCF连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am calling WCF method in for loop. I have couple of questions in this regard,

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

1) In this case if error occurs while its in the loop, where to re-open the connection?

2)在哪里关闭连接?

2) Where to close the connection?

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)
    {

    }
}


推荐答案


  1. 中止并重新打开捕获中的连接

  2. 您可以在循环外关闭连接。但是,如果您期望长时间处于循环中,那么我更喜欢使用计数器并在每次计数器到达50时关闭连接。如果尚未终止或关闭连接,请使用finally块关闭连接。

这篇关于循环调用方法时如何处理WCF连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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