wcf method in a loop?
Taking the follwing scenario in consuming a WCF method:
开发者_运维知识库List<TList> MyList ; // contains 1000 rows
MyWCFclient svc = new MyWCFclient;
foreach ( var g in MyList){
g.field1inMyList = svc.getCalc( g.field2inMyList )
}
svc.Close();
Could be a good implementation to reuse a wcf method in this way, performance etc?
Each invocation of getMyInt would be its own separate conversation, with quite a bit of overhead on each call. So no, this would not be a good implementation if you actually had a loop quickly invoking getMyInt 1000 times in a row. However, since your example is a bit contrived (it doesn't accomplish/return anything) it's difficult to suggest the best way to improve it.
Edit: I can see from your changes that you have a list of objects containing two properties. You are using the web service to derive one of the values from the other. Therefore, the most optimal way of solving this problem, would be to pass all the values to the web service at once, like so:
List<TList> MyList ; // contains 1000 rows
MyWCFclient svc = new MyWCFclient;
var field2Values = MyList.Select(x => x.field2inMyList).ToArray();
var field1Values = svc.getCalc(field2Values);
for (int i = 0; i < field1Values.Length; i++)
{
MyList[i].field1inMyList = field1Values[i];
}
svc.Close();
Both field2Values
and field1Values
are arrays and there is only one call to the WCF service.
You might want to create a "bulk" version of this service operation to allow it to take a list as a parameter (MyList in that case). This new operation would then call the same business logic for each element and return the results as a whole.
This should reduce overhead. But do not forget to adapt the Max*MessageSize configuration parameters to cope with the potentially big messages involved in that case.
精彩评论