Do I really need to call CloseAsync with WCF
If I have a function in silverlight that is like this:
authenticationServiceClient.testCompleted += callback;
authenticationServiceClient.testAsync();
Do I really need to call closeasync or will it be called for me when the program is done?
authenticationServiceClient is a local var to the 开发者_如何学Cfunction/
When the program is done, all the open connections will be closed.
If you want to dispose of the resource while the program is running, then it depends on the binding the client uses. If you're dealing with an HTTP binding, then calling Close[Async] isn't really necessary; if you're dealing with a TCP binding, then Close will actually close the socket, which remains open (for some time).
I always follow this pattern with Silverlight:
// Call
serviceClient.CallMethodCompleted += (s, a) =>
{
// Error?
if (a.Error == null)
{
// Do something
}
else
{
// Handle error
}
// Close
serviceClient.CloseAsync();
};
// Call
serviceClient.CallMethodAsync();
It's not necessary, but it is good to do this since the browser does have HTTP keep-alives and a little housekeeping never hurt anyone.
There are some patterns (which I follow as well) to close proxyclient right after method call, not in callback. In this case code looks cleaner and you can use not only lambda in callbacks, but delegate functions. something like this:
....
serviceClient.CallMethodCompleted += (s, a) =>.....
// Call
serviceClient.CallMethodAsync();
// Close
serviceClient.CloseAsync();
精彩评论