Xander
Xander

Reputation: 9171

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 function/

Upvotes: 1

Views: 1881

Answers (3)

Vitaliy Markitanov
Vitaliy Markitanov

Reputation: 2448

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();

Upvotes: 0

Keith Adler
Keith Adler

Reputation: 21178

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.

Upvotes: 0

carlosfigueira
carlosfigueira

Reputation: 87238

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).

Upvotes: 2

Related Questions