Windows 服务自发失去对临时文件夹的访问权限



我有一个Windows服务应用程序,作为处理MSMQ消息的一部分,它写出到运行它的帐户的TEMP目录。因此,如果服务在 MYDOMAIN\foo 下运行,则 TEMP 目录将C:UsersfooAppDataLocalTemp。相关代码为:

Guid key = Guid.NewGuid();
string tempPdf = Path.Combine(Path.GetTempPath(), string.Format("{0:D}.pdf", key));
byte[] output = GetSomeData();  // Gets in-memory PDF data, in this case the output from an SSRS report
File.WriteAllBytes(tempPdf, output);

这通常没有任何问题。以看似随机的时间间隔(有时一天几次,有时间隔几天(,该过程将在File.WriteAllBytes调用时开始失败。例外情况是:

系统。未经授权访问异常:访问路径 'C:\Users\foo\AppData\Local\Temp\a2b5b6b0-7c25-42a4-a475-771b8f4c525e.pdf' 被拒绝。

重新启动服务可以修复所有问题,至少是暂时的。

磁盘空间很好。对于 TEMP 文件夹,权限似乎是正常的。除了上面的应用程序错误外,事件日志中没有任何内容。这在Windows Server 2012 R2上运行。

堆栈跟踪:

System.UnauthorizedAccessException: Access to the path 'C:UsersfooAppDataLocalTempeb29dd49-d3c5-486f-8a9b-fface4857448.pdf' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost)

任何想法可能导致这种情况,或追踪问题可能在哪里的提示?

这最终是一个模拟问题。有一个完全独立的进程,在同一个 Windows 服务下运行,它将一些代码封装在模拟调用中。该代码未干净地实现,因此未经处理的异常将无法终止模拟,因此其他(不相关的(进程仍在模拟帐户下运行,该帐户显然无法访问服务用户的临时文件夹。

在运行进程监视器并查看"拒绝访问"事件的详细信息后,我能够看到这一点。

最新更新