简体   繁体   中英

FileNotFoundException IIS7

I have a C# Web app that runs perfectly fine in VS2010, but when deployed to an IIS7 server, returns the "image not found icon".

The piece of code in question essentially grabs a thumbnail of an image on a network shared location, manipulated and then pushed back out to the webpage.

The web server sits on the same network as the files it is trying to access. All users that access this webpage are all on the same local intranet.

The application is a stored list of network saved resources that are categorized in one way or another.

When I deploy the application, it gives the above two errors that I find in my Application Log. I feel that this is a file permissions error and that the two errors are linked, but I do not know where to change the permissions for this to make the application work properly.

Exception information: 
Exception type: FileNotFoundException 
Exception message: T:\Published\Generic.jpg

However, if I take "T:\\Published\\Generic.jpg" and plug it into my address bar of IE. It loads the image.

The section of code that handles the image is this:

System.Drawing.Image img;
img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString()));

I've tried it with both with and without the MapPath method.

I try to debug the application, but because it is working in VS2010, it is not throwing the exception so I don't know why it is being tossed on the IIS server.

Entire Stack Trace as requested:

Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 13/02/2012 4:16:26 PM 
Event time (UTC): 13/02/2012 11:16:26 PM 
Event ID: 1f01693f71a2443790a8d83ba06a88a4 
Event sequence: 12 
Event occurrence: 1 
Event detail code: 0 

Application information: 
Application domain: /LM/W3SVC/2/ROOT-3-129736485835718008 
Trust level: Full 
Application Virtual Path: / 
Application Path: C:\inetpub\wwwroot\
Machine name: XXXXXX

Process information: 
Process ID: 10768 
Process name: w3wp.exe 
Account name: IIS APPPOOL\ASP.NET v4.0 

Exception information: 
Exception type: FileNotFoundException 
Exception message: T:\Published\Generic.jpg
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at imagedrawer.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)



Request information: 
Request URL: http://localhost/imagedrawer.aspx?File=T:\Published\Generic.jpg 
Request path: /imagedrawer.aspx 
User host address: ::1 
User:  
Is authenticated: False 
Authentication Type:  
Thread account name: IIS APPPOOL\ASP.NET v4.0 

Thread information: 
Thread ID: 64 
Thread account name: IIS APPPOOL\ASP.NET v4.0 
Is impersonating: False 
Stack trace:    at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at imagedrawer.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Custom event details: 

Contents of imagedrawer.aspx:

 System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Drawing.Image img;

        img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString()));


        if (img.Height > 80 || img.Width > 80)
        {
            System.Drawing.RectangleF RF = new System.Drawing.RectangleF();
            RF.X = 0;
            RF.Y = 0;
            RF.Height = (img.Height < 80) ? img.Height : 80;
            RF.Width = (img.Width < 80) ? img.Width : 80;
            System.Drawing.Bitmap bmthumb = (System.Drawing.Bitmap)img.Clone();
            System.Drawing.Bitmap bmCrop = bmthumb.Clone(RF, bmthumb.PixelFormat);
            img = (System.Drawing.Image)bmCrop;
        }
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);


        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"].ToString());
        Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(ms.ToArray());
        Response.End();
        img.Dispose();

I don't think that network drives are available under the context of a service. You will probably have to use a network share notation (such as \\\\machine-name\\share ). Additionally you are running under the default user context ( IIS APPPOOL\\ASP.NET v4.0 ), which is more difficult to get working in a network setup. You should change the application pool identity to a network user, and give that user access.

Another option is to impersonate the user accessing the application (assuming you are using Windows Authentication).

You can change the application pool identity by right clicking on the application pool and selecting advanced settings. Identity under the Process Model is the setting to change.

In order to enable impersonation, you can go to the application, and select Authentication feature, enable ASP.NET Impersonation, then click Edit.. and ensure that Authenticated user is selected. Impersonation can also work with a particular user identity by using Specific User in this last dialog box, but that is mainly useful when you want to run in the context of a user that normally cannot run as a service.

EDIT:

Apparently the IIS AppPool users run under the machine context, which is DOMAIN\\Machine$ . See Application Pool Identities .

The IIS7 worker process runs under its own credentials. It will access the file as the identity that runs the Application Pool under which your website runs. This is usually ApplicationPoolIdentity or NetworkService . You need to grant that user access to the file in question.

But if you are really getting a FileNotFoundException that is probably not your problem, so please post the entire stack trace.

I think it is because you are accessing the image usin the mapped drive name. Instead if using T:\\Published\\Generic.jpg in IIS virtual directory try UNC name \\machineName\\Published\\Generic.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM