WCF proxy singleton in multithreaded app

2019-07-06 12:02发布

问题:

I have just inherited an asp.net mvc 3 site operating on .net 4 which makes heavy use of WCF calls to a very slow external service. The site is not yet live.

One curious piece of code I've found is around the WCF client proxy usage. There is one instance created for the entire aspnet application which is shared between all threads. So, effectively a global variable.

To me, this screams danger yet the site operates without error even under load testing. There is no guard code preventing concurrent calls on the WCF client in the site.

Can someone confirm the safety of using what is essentially a singleton WCF proxy in an asp net app? Wouldn't faulting kill the proxy for all threads?

回答1:

Can someone confirm the safety of using what is essentially a singleton WCF proxy in an asp net app? Wouldn't faulting kill the proxy for all threads?

The proxy is thread safe but as you say, it should be handled with care because it could be in a faulted state in which case the channel should be opened again. You may take a look at the following sample implementation that I have been using to reuse the same channel factory. What is expensive is the creation of the channel factory, not the channel itself.