webservices的最佳做法

webservices的最佳做法

问题描述:

我创建了一个web服务,当我想使用它的方法时,我在一个过程中实例化它,调用该方法,最后我将其部署,但是我认为它也可以实例化“private void Main_Load(object sender,EventArgs e)”事件中的webservice。webservices的最佳做法

事情是,如果我这样做,第一种方式,我必须实例化Web服务,每次我需要它的一种方法,但在另一种方式,我必须保持一个Web服务连接所有的时间,当我用它在一个形式例如。

我想知道这些做法是更好的,或者如果有一个更好的方法来做到这一点

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

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

这取决于你打电话给Web服务的频率。如果你打算几乎不断地调用它,那么使用方法#2可能会更好。但是,如果它不会经常被调用,那么最好使用方法#1,并且只在需要时进行实例化。

现在我为移动设备制定了一个解决方案,它变成在不规则的时间使用,它可以在10分钟,1小时,4小时内使用它的变量,似乎更好的方法是第一个战略。

去年我们开始使用webservices的一个项目,事实是我们在Sub New()过程中实例化了我们的webservices,并且它运行得非常好,但是,有时一些用户声称他们醒了从他们的椅子上,当他们返回并试图继续申请时,他们收到了超时错误信息,他们不得不重新登录。

我们认为这可能是好的,因为用户可能会在很长的时间外出席他们的座位,但是一旦与首席执行官一起呈现应用程序,它就会发生完全相同的情况,并且我个人没有像那样的行为,这就是问题的原因。

感谢您的回答。