<p>I've created a webservice and when I want to use its methods I instantiate it in the a procedure, call the method, and I finally I dispose it, however I think also it could be okay to instantiate the webservice in the "private void Main_Load(object sender, EventArgs e)" event.</p>

<p>The thing is that if I do it the first way I have to instantiate the webservice every time I need one of its methods but in the other way I have to keep a webservice connected all the time when I use it in a form for example. </p>

<p>I would like to know which of these practices are better or if there's a much better way to do it</p>

<p><strong>Strategy 1</strong></p>

<pre><code>private void btnRead_Click(object sender, EventArgs e)
{
    try
    {
        //Show clock
        this.picResult.Image = new Bitmap(pathWait);

        Application.DoEvents();

        //Connect to webservice
        svc = new ForPocketPC.ServiceForPocketPC();
        svc.Credentials = new System.Net.NetworkCredential(Settings.UserName, Settings.Password);
        svc.AllowAutoRedirect = false;
        svc.UserAgent = Settings.UserAgent;
        svc.PreAuthenticate = true;
        svc.Url = Settings.Url;
        svc.Timeout = System.Threading.Timeout.Infinite;

        svc.CallMethod();
         ...
    }
    catch (Exception ex)
    {
        ShowError(ex);
    }
    finally
    {
        if (svc != null)
            svc.Dispose();
    }
}
</code></pre>

<p><strong>Strategy 2</strong></p>

<pre><code>private myWebservice svc;

private void Main_Load(object sender, EventArgs e)
{
    //Connect to webservice
    svc = new ForPocketPC.ServiceForPocketPC();
    svc.Credentials = new System.Net.NetworkCredential(Settings.UserName, Settings.Password);
    svc.AllowAutoRedirect = false;
    svc.UserAgent = Settings.UserAgent;
    svc.PreAuthenticate = true;
    svc.Url = Settings.Url;
    svc.Timeout = System.Threading.Timeout.Infinite;
}

private void btnRead_Click(object sender, EventArgs e)
{
    try
    {
        //Show clock
        this.picResult.Image = new Bitmap(pathWait);

        Application.DoEvents();
        svc.CallMethod();
         ...
    }
    catch (Exception ex)
    {
        ShowError(ex);
    }
}

private void Main_Closing(object sender, CancelEventArgs e)
{
    svc.Dispose();
}
</code></pre>

[{'answer_id': 13164, 'author': 'Brandon Wood', 'author_id': 423, 'author_profile': 'https://Stackoverflow.com/users/423', 'pm_score': 3, 'selected': True, 'text': "<p>It depends on how often you are going to be calling the web service.  If you're going to be calling it almost constantly, it would probably be better to use method #2.  However, if it's not going to be getting called quite so often, you are better off using method #1, and only instantiating it when you need it.</p>\n"}, {'answer_id': 13171, 'author': 'Nelson Miranda', 'author_id': 1130097, 'author_profile': 'https://Stackoverflow.com/users/1130097', 'pm_score': 0, 'selected': False, 'text': "<p>Right now I made a solution for a mobile device and it turns to be used on irregular times, it could be used in 10 minutes, 1 hour, 4 hours its very variable, it seems that the better aproach is the first strategy.</p>\n\n<p>Last year we went on a project where we used webservices, the fact is that we instantiated our webservices at the Sub New() procedure and it run it very well, however, sometimes some users claimed at us that they woke up from their chairs and when they returned and tried to continue on the application they received a timeout error message and they had to re-login again.</p>\n\n<p>We thougth that maybe that was Ok because maybe the users went out for a very long time out of their seats, but once in a presentation of the application with the CEOs it happened exactly the same scenario and personally I didn't like that behaviour and that's why the question.</p>\n\n<p>Thanks for the answer.</p>\n"}]
['https://Stackoverflow.com/questions/13160', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1130097/']