いくつかの背景
多数の写真へのアクセスを提供する ASP.net MVC 4 Web アプリケーションがあります。各写真には、MySQL テーブルから一意の ID が与えられ、16 進形式のファイル名が作成され、ファイル システムのフォルダーに保存されます。
例えば:
D:\写真\69F.jpg
アプリケーション全体で、元の写真を使用して作成されたさまざまなサイズのサムネイルが表示されます。サムネイルが変更されることはほとんどないため、たとえあったとしても、サムネイルは次の形式で作成され、同じファイル システム フォルダーに保存されます。(これは、90% の確率で、画像処理なしで写真をすぐに返すことができることを意味します)。
- サムネイル通常- D:\Photos\69F_t.jpg
- サムネイルマップ- D:\Photos\69F_tm.jpg
すべての写真は、特別なコントローラー ( ImageController ) を介してアクセスされます。このコントローラーには、必要なサムネイルの種類に応じていくつかのアクションがあります。たとえば、通常のサムネイルが必要な場合は、Thumbアクションを呼び出す必要があります。このアクションは、サムネイルが存在するかどうかを判断し、存在しない場合は作成して保存してからブラウザーに返します。
\Image\Thumb\1695
アプリケーションは、上記のフォルダーへの完全なアクセス権を持つアカウントで実行されています。ほとんどの場合、これは機能するため、これはアクセス許可の問題ではありません!
問題
私が抱えている問題は、サムネイルを取得するための呼び出しが行われているときに、アプリケーションから散発的なエラーが報告されることです。エラーはすべて以下の形式に従いますが、もちろん写真 ID は変更されます (つまり、複数の写真で発生します)。
別のプロセスで使用されているため、プロセスはファイル 'D:\Photos\69F_t.jpg' にアクセスできません。
また...
パス「D:\Photos\69F_t.jpg」へのアクセスが拒否されました。
上記のエラーは両方とも、最後のTry...CatchのReturn File(...)行から発生しています。
Function Thumb(Optional ByVal id As Integer = Nothing)
Dim thumbImage As WebImage
Dim dimension As Integer = 200
' Create the image paths
Dim imageOriginal As String = System.Configuration.ConfigurationManager.AppSettings("imageStore") + Hex(id) + ".jpg"
Dim imageThumb As String = System.Configuration.ConfigurationManager.AppSettings("imageStore") + Hex(id) + "_t.jpg"
' If no image is found, return not found
If FileIO.FileSystem.FileExists(imageOriginal) = False Then
Return New HttpNotFoundResult
End If
' If a thumbnail is present, check its validity and return it
If FileIO.FileSystem.FileExists(imageThumb) = True Then
thumbImage = New WebImage(imageThumb)
' If the dimensions are correct, simply return the image
If thumbImage.Width = dimension Then
thumbImage = Nothing
Return File(imageThumb, System.Net.Mime.MediaTypeNames.Image.Jpeg)
End If
End If
' If we get this far, either the thumbnail is not the right size or does not exist!
thumbImage = New WebImage(imageOriginal)
' First we must make the image a square
If thumbImage.Height > thumbImage.Width Then
' Portrait
Dim intPixelRemove As Integer
' Determine the amount to crop off the top and bottom
intPixelRemove = (thumbImage.Height - thumbImage.Width) / 2
thumbImage.Crop(intPixelRemove, 0, intPixelRemove, 0)
Else
' Landscape
Dim intPixelRemove As Integer
' Determine the amount to crop off the top and bottom
intPixelRemove = (thumbImage.Width - thumbImage.Height) / 2
thumbImage.Crop(0, intPixelRemove, 0, intPixelRemove)
End If
thumbImage.Resize(dimension + 2, dimension + 2, True, True)
thumbImage.Crop(1, 1, 1, 1)
thumbImage.Save(imageThumb)
thumbImage = Nothing
Try
Return File(imageThumb, System.Net.Mime.MediaTypeNames.Image.Jpeg)
Catch ex As Exception
Return New HttpNotFoundResult
End Try
End Function
質問
何かがファイルを開いたままにして、アプリケーションがファイルをブラウザに返さないようにしていると確信していますが、それが何であれ、これは呼び出しの 90-95% で正常に動作するため、非常に散発的です。
それまたは問題は、複数の人が同じファイルにアクセスしようとするため、同時実行が原因です。
- これを引き起こしている可能性のあるものを誰でも見つけることができますか?
- 複数の人が同時に同じ写真のサムネイルにアクセスしようとすると、さらに多くの問題が発生する可能性がありますか?