簡體   English   中英

隨機文件訪問問題 IIS

[英]Random File Access Issues IIS

我有兩種方法可以在 ASP.NET MVC 網站上檢索圖像。 如果我使用文件資源管理器在磁盤上搜索圖像,圖像在那里,但我的日志文件顯示許多錯誤,說每個圖像都不存在。

有時它可以正常工作,但在其他情況下,我的應用程序找不到圖像。 如果我重新啟動我的 IIS 站點或重新啟動計算機,它可以正常工作,但它有一個我必須解決的隨機行為。

圖片所在的兩條路徑是:

"C:\inetpub\wwwroot\Test_Application\Content\Images" (Inside site public dir)
"C:\Test_Application\images" (Configured on IIS as a virtual directory)

這是一個運行 IIS 的 Windows 10 Pro 設備,並且運行該站點的應用程序池有一個 Windows 帳戶,該帳戶具有管理權限作為身份,因為我需要連接到 SQL Server LocalDb。

將消息寫入日志的函數是:

private bool Func_GetImages(RecordModel record)
{
    bool bResult = false;
    List<string> imagesFound = new List<string>();
    string sImageMaskName = string.Empty;
    string sAbsoluteImgPath = string.Empty;
    string sSourceImgPath = string.Empty; //Path to img directory with year, month and day.
    string sTargetImgPath = string.Empty;
    try
    {
        Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Debug, "Func_GetImages", "Get Images: ",
        string.Format("Get images for record with GUID: {0}; Image Mask Name: {1}; Times for Images: {2}", record.GUID, record.imageMaskName,
        record.TimeBetweenColorImages));

        if (HttpContext.Current != null)
        {
            sAbsoluteImgPath = HttpContext.Current.Server.MapPath(Config.sImgDirectory);
            sTargetImgPath = HttpContext.Current.Server.MapPath("~/Content/Images/Processed");
        }
        else
        {
            sAbsoluteImgPath = string.Format("{0}/", AppDomain.CurrentDomain.BaseDirectory);
            sTargetImgPath = string.Format("{0}/", "~/Content/Images/Processed");
        }

        sSourceImgPath = Path.Combine(sAbsoluteImgPath, record.DetectionDatetime.Year.ToString(), record.DetectionDatetime.Month.ToString("D2"), record.DetectionDatetime.Day.ToString("D2"));
        Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Debug, "Func_GetImages", "Get Images: ",
        string.Format("Path to search for files: {0}", sSourceImgPath));

        sImageMaskName = record.imageMaskName;

        if (!Directory.Exists(sSourceImgPath))
        {
            Directory.CreateDirectory(sSourceImgPath);
        }

        //Try to get files with this search pattern
        imagesFound = Directory.GetFiles(sSourceImgPath, string.Format("{0}*", sImageMaskName)).ToList();

        if (imagesFound.Count > 0)
        {
            Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Debug, "Func_GetImages", "Get Images: Ok",
            string.Format("Number of images for record with GUID: {0} is {1}", record.GUID, imagesFound.Count.ToString()));

            //Create new image name
            DateTime dateCapture = record.DetectionDatetime;
            string sImageName = record.imageMaskName;

            //Check if day directory exists. If not, create it
            Func_CheckOrCreateImageDirectory(dateCapture);

            string sBaseDirectory = Path.Combine(sTargetImgPath, dateCapture.Year.ToString(), dateCapture.Month.ToString("D2"), dateCapture.Day.ToString("D2"));

            //Rename, add image header and copy blackWhiteImages
            Func_ProcessImages(sBaseDirectory, sImageMaskName, sImageName, detection.numPicturesColor, enum_ImagesType.COLOR, detection); 

        }
        else
        {
            Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Error, "Func_GetImages", "Get Images: Ko",
            string.Format("No images found for record with GUID: {0} and Image Mask Name: {1}", record.GUID, sImageMaskName));
        }

        bResult = true;
    }
    catch (Exception exception1)
    {
        Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Error, new StackTrace(exception1).GetFrame(0).GetMethod().Name, "GeneralException",
        "Source = " + exception1.Source.Replace("'", "''") + ", Message = " + exception1.Message.Replace("'", "''"));
    }

    return bResult;
}

當我檢查“imagesFound.Count > 0”時,出現問題時寫入的日志消息在else語句上。

這是有時出現的錯誤消息:

未找到用於記錄 GUID 的圖像:8689ffcd-46d2-4a25-ab57-edce11e45977 和圖像掩碼名稱:002475-20190702_070949_326

感謝您的幫助。

我做了一些測試,這是由以下代碼引起的:

if (HttpContext.Current != null)
{
    sAbsoluteImgPath = HttpContext.Current.Server.MapPath(Config.sImgDirectory);
    sTargetImgPath = HttpContext.Current.Server.MapPath("~/Content/Images/Processed");
}
else
{
    sAbsoluteImgPath = string.Format("{0}/", AppDomain.CurrentDomain.BaseDirectory);
    sTargetImgPath = string.Format("{0}/", "~/Content/Images/Processed");
}

我解決了將“Web.config”中的“sImgDirectory”的值設置為項目根目錄並將相對於該路徑的路徑存儲在靜態變量中的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM