WCF C# - Getting Specific Configuration Values From App.config -
i have wcf service xyz deployed on number of hosts. each such service may have connection xyz service deployed on 1 of other hosts. it's distributed system states differ between services.
in order communicate doesn't make sense me "add service reference" in visual studio because add redundancy (the service knows it's going communicating with).
so idea specify other service endpoints in app.config files of each service. example:
<client> <endpoint name="bel" address="tcp://us.test.com:7650/ordermanagementservice" binding="tcpbinding" contract="iordermanagementservice"/> <endpoint name="bel2" address="tcp://us.test2.com:7650/ordermanagementservice" binding="tcpbinding" contract="iordermanagementservice"/> </client>
now, want way read these settings , create channelfactories , channels in code. however, it's turning out hassle this.
two questions: doing things right; , if so, what's best way extract these values config file?
creating channels directly isn't hard, , endpoint configuration read in you. try this:
var factory = new channelfactory<iordermanagementservice>("bel"); var proxy = factory.createchannel(); // call methods on proxy proxy.close();
note proxy needs closing (which means calling close
or abort
correctly) have finished it. however, can leave factory open long periods, in cache.
you can encapsulate helper methods make calling code simple:
public static channelfactory<tcontract> newchannelfactory<tcontract>(string endpointconfigurationname) tcontract : class { // todo: cache factory in here better performance. return new channelfactory<tcontract>(endpointconfigurationname); } public static void invoke<tcontract>(channelfactory<tcontract> factory, action<tcontract> action) tcontract : class { var proxy = (iclientchannel) factory.createchannel(); bool success = false; try { action((tcontract) proxy); proxy.close(); success = true; } { if(!success) { proxy.abort(); } } }
Comments
Post a Comment