Question

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.

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.

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

Strategy 1

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

Strategy 2

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();
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

Thanks for the answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top