Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to close a .NET service reference client when I'm done using it

I'm trying to find out if it is neccessary to close a .net service reference client when you are done using it. Almost all of the examples that I have come across on the net don't seem to, but the client that is generated implements IDisposable and since it does open a connection to a service, my intuition tells me you need to close that connection when you are done with it.

Here is a code sample I pulled from http://msdn.microsoft.com/en-us/library/bb386386(v=VS.90).aspx :

private void button1_Click(System.Object sender, System.EventArgs e)
{
    ServiceReference1.Service1Client client = new 
        ServiceReference1.Service1Client();
    string returnString;

    returnString = client.GetData(textBox1.Text);
    label1.Text = returnString;
}

I would think that you should at least call client.Close() at the end of this method, and better yet wrap the first line in a using statement. I just wanted to get some feedback on this to find out what the best practices are.

like image 297
nitramssirc Avatar asked Jul 14 '10 13:07

nitramssirc


People also ask

How do I dispose of WCF client?

Anyone utilizing the client can wrap it into an ordinary using block. If the client is in a clean state a graceful close is done. If the client is in a faulted state a hard abort is done. If an exception is thrown by a service method the client is placed in the faulted state.

How do I delete a service reference in Visual Studio?

To remove a service reference In Solution Explorer, right-click the service reference and then click Delete. The service client will be removed from the solution, and the metadata that describes the service will be removed from the app.

What is a service reference in Visual Studio?

The WCF Web Service Reference tool is a Visual Studio connected service extension that lets you connect your . NET 5+, . NET Core, or ASP.NET Core project to a web service.

How do I update my website reference in Visual Studio 2019?

In Solution Explorer, open your project's App_WebReferences folder and click the node for the Web reference you want to update. Right-click the reference and click Update Web Reference. The new files for the XML Web service are downloaded to your project.


1 Answers

Yes, you do, but you need to be very careful when doing so. While closing anything that implements ICommunicationObject the potential exists to cause the disposal of the object to take an excessive amount of time in the event that there is an error or fault on the channel.

Because of this, it is prescribed that you call the Close method and then call the Dispose method on IDisposable, using a number of catches for certain exception types and calling Abort before you finally call Dispose.

You can wrap this logic up in an IDisposable implementation which you can use in a using statement.

The key here is to create a token that implements IDisposable and then in that implementation, call Close, catch the relevant exceptions, call Abort (if necessary) and then call Dispose.

This is implemented as an extension method which returns an IDisposable on it which in turn allows you to use it in a using statement.

like image 112
casperOne Avatar answered Sep 21 '22 01:09

casperOne