开发者

Reuse of WCF service clients

开发者 https://www.devze.com 2023-02-03 04:33 出处:网络
I have a WCF webservice that acts as a data provider for my ASP.NET web page. Throughout the web page a number of calls are made to the web service via the auto-generated ServiceClient.

I have a WCF webservice that acts as a data provider for my ASP.NET web page.

Throughout the web page a number of calls are made to the web service via the auto-generated ServiceClient.

Currently I create a new ServiceClient and open it for each request i.e. Get Users, Get Roles, Get Customer list etc.... E开发者_JS百科ach one of these would create a new ServiceClient and open a new connection.

Can I make my ServiceClient class a global or statically available class so that all functions within my ASP.NET web page can use the same client. This would seem to be far more efficient. Are there any issues with doing it this way? Any advice I should take into account when doing this?

What happens if I make multiple requests to a client? Presumably it is all synchronous so it shouldn't matter if I make 1 or 50 calls to it?

Thanks


When session (wsHttp with security context or reliable session) or connection (net.tcp, net.pipe) oriented binding is used you have to handle your proxy in the way you want to handle the session. So if you share the proxy, all calls will be handled in single WCF session (by default handled by single service instance). But you have to deal with additional complexity like: Unhandled service exception will terminate your channel and following call from client will result in exception.

When session-less HTTP binding (basicHttp, webHttp) is used you can share your proxy or even make it static. Each call is handled separately, exception on a service will not fault the channel and it transparently reuses opened HTTP persistent connections. But because of this, there should be no big overhead to creating new proxy / channel.

So my suggestion is: When you need several calls to your service in single request processing in your ASP.NET application, use the same proxy / channel. But don't share proxy / channel among different requests.


I think using a ChannelFactory could take of your problem. If I'm right the ChannelFactory has a pool of your connection and re-uses the channels. The advantage of this is that the channels don't get instatiated each time, only the first.

Read more here: ChannelFactory

To handle the disposing of the channels you need some special handling since the channel can throw exception in dispose. I wrote a mapper to handle this, you can read about it here: http://blog.tomasjansson.com/2010/12/disposible-wcf-client-wrapper/

0

精彩评论

暂无评论...
验证码 换一张
取 消