File automatically getting locked by IIS 8 (C#, AS

2019-07-24 15:09发布

问题:

Facing an issue with IIS 8, windows server 2012. Web app written in asp.net c# mvc razor 5 I am facing an File locking(image file) issue by IIS. I have a user profile page where I display the User profile pic. I noticed that as soon as I simply display the profile page in browser, the profile image on disk is locked by the IIS. Profile image is display with a img tag as shown below (Model.ImagePath is a string path)

string ImageUrl = "";
if (Model.ImagePath == null || Model.ImagePath == "/AppContents/1/Speaker/")
{
    ImageUrl = "http://" + HttpContext.Current.Request.Url.Authority + "/AppContents/1/avatar.png";}
else 
{
    ImageUrl = "http://" + HttpContext.Current.Request.Url.Authority + Model.ImagePath;
}
 ...

<section class="col col-5" style="padding: 0px 0px 0px 4px;">
<label class="label">Profile Picture  <span class="">(120px X 120px)</span>      </label>

  <div class="editor-label">
    <span>
     <a  onclick="return launchEditor_avatar('blah','@ImageUrl');" href="#">
       <img id="blah" style="margin:10px;width: auto !important;height: auto !important;max-height: 85px; max-width: 120px;min-height: auto;min-width: 50px;"   src="@ImageUrl" />
      </a>
   </span>

    <span style="border: 1px initial #808080">@Html.CheckBoxFor(model => model.moderator) Is a Moderator
    </span>
</section>  

As soon as page is rendered the profile image on the local server gets locked. This creates issue becuase from profile page, user can edit the image. So edited image (using aviary high res) is not getting saved as it says " "System.IO.IOException: The process cannot access the file 'C:\Users\webapp\73a.jpg' because it is being used by another process.".

On my local windows instance(windows 10, IIS 8), this issue do appear but after 3 to 4 mins the lock gets removed automatically and file gets saved after editing. This making me feel that it is some configuration issue. Please guide if some one has faced any similar issue.

I have checked the sysinternal tool "handle.exe" and I see it points to IIS worker process. Can someone please guide as how to get this issue solved

C:\Downloads\Handle>handle.exe C:\Users\webapp\73a.jpg


Nthandle v4.1 - Handle viewer
Copyright (C) 1997-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

w3wp.exe           pid: 2720   type: File           794: C:\Users\webapp\73a.jpg
w3wp.exe           pid: 2720   type: File           A34: C:\Users\webapp\73a.jpg
w3wp.exe           pid: 2720   type: File           A9C: C:\Users\webapp\73a.jpg

回答1:

I have found the issue and solution. In controller an function to identify the image dimension (to apply some css rule for mobile based app) was causing the issue.

System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~") + spe_Fil.ImagePath);

the img object was not disposed and hence a lock was applied. After adding img.dispose(), file locking issue got resolved.