Using WCF client
In my application, I am using client.open and client.close to open a WCF proxy channel, use the wcf method and finally close it. I do this is several parts of my Windows Forms application.
However, I have noticed that client.close does not necessarily close the connection immediately. Why is this? Should I use client.Abort instead?
Am I using the WCF client the right way? i.e. clien.open -> call WCF method -> client.close.
Thanks for any help开发者_运维百科 in advance!
Subbu
The .Close()
method will try to close the connection gracefully, e.g. a pending call will still be finished by the server, and only after that's done, the connection is terminated. Use this whenever possible - it's the gentle and preferred way of closing a client connection.
.Abort()
really just tears down the communication channel - not matter whether a call is still in progress or not. It's the ultimate, last resort escape hatch, if .Close()
failed.
As a best practice, I would wrap my service calls into a try...catch
block, something like this:
try
{
myServiceClient.CallSomeMethod();
myServiceClient.Close();
}
catch(FaultException<SomeError>)
{
myServiceClient.Abort();
}
// possibly other catch clauses, too - call .Abort() inside them
catch(CommunicationException)
{
myServiceClient.Abort();
}
精彩评论