我们编写了一个 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
吗?这可能是什么根本原因?