asp.net mvc - Make WCF client static or use dispose method -
i developing website in mvc asp.net
uses wcf service. i'm using wcf client in controller that
public class home : controller { private serviceclient _client = new serviceclient(); public actionresult index() { //code here } }
so think 2 options have define it:
1- make client object static
private static serviceclient _client = new serviceclient();
2- use idisposable.dispose
public class home : controller, idisposable { private serviceclient _client = new serviceclient(); protected override void dispose(bool disposing) { _client.dispose(); base.dispose(disposing); } }
what best option performance ?
both options irrelevant performance point of view. creating wcf channel factories performance "expensive". serviceclient
inherits clientbase
, channel factory caching. creating clients cheap , that's correct approach: create client, use it, dispose immediately.
the second option therefore correct, if not flaw in wcf client desing. flaw causes cannot rely on client's dispose
method handle channel's fault state correctly. issue sure has been covered somewhere else in so. in short, here's how create , dispose wcf client:
var client = new serviceclient(); var success = false; try { // call client's methods client.close(); success = true; } { if (!success) { client.abort(); } }
Comments
Post a Comment