【问题标题】:IO.File.Delete works in debug mode but throws security exception in run modeIO.File.Delete 在调试模式下工作,但在运行模式下抛出安全异常
【发布时间】:2014-03-20 22:45:06
【问题描述】:

有没有人见过 IO.File 方法与附加的调试器一起工作但在正常运行时不工作的问题?

IO.File.Delete 在运行时给出此异常,但如果调试器通过 VS 附加(在管理员模式下运行)则不会。

“对路径 'C:\AppName\App_Data\BodyPart_7416da26-4b8f-4d08-9a4a-fd3a9bf02327' 的访问被拒绝。”

我已验证 IIS_IUSRS 对 \App_Data 目录具有完全权限。 BodyPart_* 是 ASP.Net 生成的文件名,不是子目录。

另一个人在 StackOverflow 上遇到了这个问题,但目前还没有修复。 (File.Delete() not working in run mode but only works in debug mode)

我的代码:

''' <summary>
'''Post file(s) and store it via VDocument WS
''' </summary>
<System.Web.Http.HttpPost>
<ActionNameAttribute("PostFiles")> _
Public Function PostFiles(<FromUri> fileGroupGuid As Guid) As HttpResponseMessage

    Dim newFileIds As New List(Of Integer)
    Dim filesToDelete As New List(Of String)

    ' Check if the request contains multipart/form-data.
    If Not Request.Content.IsMimeMultipartContent() Then
        Throw New HttpResponseException(HttpStatusCode.UnsupportedMediaType)
    End If


    Dim root As String = HttpContext.Current.Server.MapPath("~/App_Data")
    Dim provider = New MultipartFormDataStreamProvider(root)

    ' Read the form data.
    Request.Content.ReadAsMultipartAsync(provider)

    For Each file As MultipartFileData In provider.FileData

        'Store to VDoc Server
        Dim vdocService As New wsdocument.vdocument
        Dim vdocId As String
        Dim sOrigFileName As String = "/" & file.Headers.ContentDisposition.FileName.Replace("""", "")

        vdocId = vdocService.savedocument(IO.File.ReadAllBytes(file.LocalFileName), sOrigFileName, _
                              "FS Upload", "0", "0", "0", "0")

        ' Store the posted file reference in the database
        Dim fileId As Integer = New Answer().StoreAnswerFileWithVDocumentId(fileGroupGuid.ToString, sOrigFileName, 0, file.Headers.ContentType.ToString, New Byte(-1) {}, 0, _
            0, FSFileMode.RespondentAnswer, Convert.ToInt32(vdocId))

        newFileIds.Add(fileId)

        filesToDelete.Add(file.LocalFileName)

    Next

    For Each tempFile As String In filesToDelete
        'delete the temp file
        IO.File.Delete(tempFile)
    Next

    Return Request.CreateResponse(HttpStatusCode.Accepted, newFileIds)


End Function

【问题讨论】:

  • 究竟是什么不起作用? 不起作用是什么意思?你有例外吗?如果是这样,在哪里和哪个?
  • 您是否尝试过以管理员身份运行程序集可执行文件?您是否也检查了文件权限?
  • 添加有关异常的详细信息(哎呀)。是的,我已经检查了权限,这意味着我让 IIS_IUSRS 完全控制了目录。
  • 您授予了访问哪个目录的权限? C:\AppName\App_Data\BodyPart_7416da26-4b8f-4d08-9a4a-fd3a9bf02327,还是父目录?
  • BodyPart_*是asp.net生成的临时文件名。我允许访问 \App_Data

标签: asp.net vb.net asp.net-web-api


【解决方案1】:

在调试模式下,不会为异步调用“ReadAsMultipartAsync”创建新线程,因此线程被阻塞,直到该方法完成。在发布模式下,它使用新线程进行异步调用,并且由于该方法在单独的线程上运行,因此您的其余代码仍在当前线程上处理。当您开始删除文件时,文件仍被附加线程上的“ReadAsMultipartAsync”方法锁定。由于文件仍处于锁定状态,因此删除将失败。您需要等待“ReadAsMultipartAsync”,以便在继续处理之前完成。

试试这个:

await Request.Content.ReadAsMultipartAsync(provider)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多