JsonServiceClient
implements IDisposable
so best practise would be to use it with a using
statement.
However there are scenarios whereby you need to the share an instance of the JsonServiceClient
across multiple requests (Such as when you use cookie based sessions, as the cookies are contained in the instances cookie container), in which case you would use the client without a using
statement, but ensure that your application calls the Dispose
method of the client, when it no longer requires the client.
This answer by gdoron further explains the best practise regarding classes that implement IDisposable
such as the JsonServiceClient
and the reasoning behind it.
As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):
I hope that helps.