0

我们编写了一个 ClickOnce 应用程序,可在此处办公室的所有 PC 上运行。今天,.NET 运行时崩溃并在事件日志中记录了以下事件:

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吗?这可能是什么根本原因?

4

1 回答 1

1

是的,我们确实遇到了这个问题 :) 在此处查看我的答案:https ://stackoverflow.com/a/13711535/1246870 - 简而言之,这是 ClickOnce 中与频繁检查更新有关的错误。

我能想出的唯一解决方法是将计时器间隔增加到更大的值。如果你有更好的想法,我很想听听。

于 2013-10-10T14:30:47.280 回答