问题
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:
- Abort and re-open the connection in the catch
- 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