In a typical client-se开发者_开发问答rvice application, where service is WCF, how to you use the service reference?
For example, do you instantiate service for each call separately, or do you open one and keep it through the lifetime of the client app? (when I'm refering to 'instatiating service', I mean instantiate service object created by svcutil)
For now we used one service instance on client, because that seemed faster than always creating, opening and closing the service for each and every call, but now I wander if that was OK? The main problem we have with this approach is that timeouts occur (or we have to have sessions opened for 10hrs), connection breaks etc, and we always have to do 'IsAlive' through the channel to make sure everything is fine...
If it's of any relevance the app is single threaded, and the authentication is custom built (by inserting auth tokens into header). The binding is basicHttp (or ws, doesn't really matter for this case).
Personally i prefer to keep the channel open just until the method call returns and then close it to free resources. It is the cleanest approach of all. Initializing the proxy instance each time is matter of milliseconds and never affected my performance goals.
Having some kind of ServiceClient singleton is an interesting idea, but as you stated, it comes with a lot of side effects, so i am not sure that's a smart approach, but since your app is single threaded, then it should not be a big issue and you will probably have some slight performance benefits.
If saving milliseconds is mission-critical, then you could also have a pool of ServiceClients that you could get instances from.
However, the first approach is the cleanest. Complexity is bad.
精彩评论