How to handle WCF connection when calling method in loop

半城伤御伤魂 提交于 2019-12-12 04:46:00

问题


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

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

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:


  1. Abort and re-open the connection in the catch
  2. You can close the connection outside the loop. However if you anticipate being in the loop for long, then I would prefer using a counter and closing the connection everytime the counter gets to, say 50. And use a finally block to close the connection if its not already aborted or closed.


来源:https://stackoverflow.com/questions/7895770/how-to-handle-wcf-connection-when-calling-method-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!