2013-10-08 19 views
0

我們編寫了一個ClickOnce應用程序,可在辦公室的所有PC上運行。今天,.NET運行時墜毀,並記錄在事件日誌中的下列事件:.NET ClickOnce應用程序由於UnauthorizedAccessException而終止

Framework Version: v4.0.30319 
Description: The process was terminated due to an unhandled exception. 
Exception Info: System.UnauthorizedAccessException 
Stack: 
    at System.IO.Directory.DeleteHelper(System.String, System.String, Boolean) 
    at System.IO.Directory.Delete(System.String, System.String, Boolean) 
    at System.IO.Directory.Delete(System.String, Boolean) 
    at System.Deployment.Application.TempFile.DisposeUnmanagedResources() 
    at System.Deployment.Application.DisposableBase.Finalize() 

該應用程序使用System.Deployment定期檢查新版本並下載他們的背景。這樣,新版本在下一次啓動應用程序時就準備好了。

我BackgroundUpdater類看起來像這樣(的情況下,它的相關):

class BackgroundUpdaterService : IBackgroundUpdaterService 
{ 
    private readonly BackgroundWorker backgroundWorker = new BackgroundWorker(); 
    private readonly DispatcherTimer checkForUpdatesTimer = new DispatcherTimer(); 

    public BackgroundUpdaterService() 
    { 
     this.backgroundWorker.WorkerSupportsCancellation = true; 
     this.backgroundWorker.DoWork += (s, e) => 
      { 
       // Check if the application was deployed via ClickOnce. 
       if (!ApplicationDeployment.IsNetworkDeployed) 
       { 
        e.Result = UpdateStatus.NotDeployedViaClickOnce; 
        return; 
       } 

       if (this.backgroundWorker.CancellationPending) return; 

       var updateCheck = ApplicationDeployment.CurrentDeployment; 

       UpdateCheckInfo info; 
       try 
       { 
        info = updateCheck.CheckForDetailedUpdate(); 
       } 
       catch (DeploymentDownloadException) 
       { 
        e.Result = UpdateStatus.DeploymentDownloadException; 
        return; 
       } 
       catch (InvalidDeploymentException) 
       { 
        e.Result = UpdateStatus.InvalidDeploymentException; 
        return; 
       } 
       catch (InvalidOperationException) 
       { 
        e.Result = UpdateStatus.InvalidOperationException; 
        return; 
       } 

       if (this.backgroundWorker.CancellationPending) return; 

       if (info.UpdateAvailable) 
       { 
        e.Result = info.IsUpdateRequired 
         ? UpdateStatus.UpdateRequired 
         : UpdateStatus.UpdateAvailable; 
       } 
       else 
       { 
        e.Result = UpdateStatus.NoUpdateAvailable; 
       } 
      }; 
     this.backgroundWorker.RunWorkerCompleted += (s, e) => 
      { 
       var result = (UpdateStatus) (e.Result); 
       this.UpdateRequired = 
        result == UpdateStatus.UpdateAvailable 
        || result == UpdateStatus.UpdateRequired; 
       if(!this.UpdateRequired) // stop looking if there is one 
       { 
        this.checkForUpdatesTimer.Start(); 
       } 
      }; 

     this.checkForUpdatesTimer.Interval = new TimeSpan(hours: 0, minutes: 15, seconds: 0); 
     this.checkForUpdatesTimer.Tick += (s, e) => 
      { 
       this.checkForUpdatesTimer.Stop(); 
       this.backgroundWorker.RunWorkerAsync(); 
      }; 
     this.checkForUpdatesTimer.Start(); 
    } 

    private readonly object updateRequiredLock = new object(); 
    private bool updateRequired; 
    public bool UpdateRequired 
    { 
     get 
     { 
      lock(this.updateRequiredLock) 
      { 
       return this.updateRequired; 
      } 
     } 
     private set 
     { 
      lock(this.updateRequiredLock) 
      { 
       this.updateRequired = value; 
      } 
     } 
    } 

    public bool Update() 
    { 
     if(!this.UpdateRequired) 
     { 
      return false; 
     } 
     bool result; 
     try 
     { 
      var updateCheck = ApplicationDeployment.CurrentDeployment; 
      updateCheck.Update(); 
      result = true; 
     } 
     catch (DeploymentDownloadException) 
     { 
      result = false; 
     } 
     return result; 
    } 

    public void Dispose() 
    { 
     this.checkForUpdatesTimer.Stop(); 
     this.backgroundWorker.CancelAsync(); 
     Thread.Sleep(100); 
     this.backgroundWorker.Dispose(); 
    } 
} 

其他人遇到從System.Deployment命名此異常?這可能是什麼原因?

回答

1

是的,我們遇到了這個問題:)看到我的答案在這裏:https://stackoverflow.com/a/13711535/1246870 - 用2個詞,這是一個ClickOnce與頻繁檢查更新相關的錯誤。

我能夠想出的唯一解決方法是將計時器間隔增加到更大的值。如果你有更好的想法,我很樂意聽到他們的意見。

相關問題