2012-02-13 46 views
3

我有一个在VS2010中完美运行的C#Web应用程序,但是当部署到IIS7服务器时,返回“图像未找到图标”。FileNotFoundException IIS7

的一段代码在基本上问题抓住在网络共享位置处的图像的缩略图,操纵和然后推回出到网页。

Web服务器与其尝试访问的文件位于同一网络上。所有访问此网页的用户都位于同一个本地Intranet上。

的应用是在一个这样或那样的分类网络资源节省的存储列表。

当我部署应用程序,它提供了两种错误,我在我的应用程序日志中找到。我觉得这是一个文件权限错误,并且这两个错误是链接的,但我不知道在哪里更改权限以使应用程序正常工作。但是,如果我将“T:\ Published \ Generic.jpg”并将其插入到我的IE地址栏中,则可能会出现此问题。它加载图像。

的用于处理图像的一段代码是这样的:

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

我已经既没有MapPath方法试了一下。

我尝试调试应用程序,但因为它在VS2010中工作,所以它不会抛出异常,所以我不知道它为什么会在IIS服务器上抛出。

整个堆栈跟踪的要求:imagedrawer.aspx的

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: 

内容:

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(); 

回答

3

我不认为网络驱动器在服务环境下可用。您可能必须使用网络共享符号(例如\\machine-name\share)。此外,您正在默认的用户上下文(IIS APPPOOL\ASP.NET v4.0)下运行,这在网络设置中更难实现。您应该将应用程序池标识更改为网络用户,并授予该用户访问权限。

另一种方法是模拟用户访问应用程序(假设你使用Windows身份验证)。

您可以通过在应用程序池右击并选择高级设置更改应用程序池标识。过程模型下的标识是要更改的设置。

为了启用模拟,您可以转到应用程序,然后选择身份验证功能,启用ASP.NET模拟,然后单击编辑..并确保已选择身份验证的用户。通过在最后一个对话框中使用特定用户,模拟也可以使用特定的用户身份,但是当您想要在通常无法作为服务运行的用户的上下文中运行时,此模式非常有用。

编辑:

显然,IIS程序池用户机上下文,这是DOMAIN\Machine$下运行。请参阅Application Pool Identities

+1

@JKM,当您使用IE访问共享时,IE使用您的凭据。如果IE浏览器有效,它只是证明你有权访问该共享。但是,在IIS/ASP.NET上运行的应用程序正在应用程序池标识下运行(如Guvante所述),因此发生的任何错误都表明该标识不能访问该共享(必须使用UNC路径,每http:// support.microsoft.com/kb/257174)。您需要授予访问共享池身份的权限,或将访问池身份更改为可访问共享的帐户。下次请多关注用户上下文。 – 2012-02-14 02:49:04

+0

将解决方案部署到目标机器并设置正确的应用程序池服务后,它不再注册此错误。 – Sorean 2012-02-14 18:32:22

2

的IIS7工作进程在其自己的凭据运行。它将以运行网站所运行的应用程序池的身份访问该文件。这通常是ApplicationPoolIdentityNetworkService。您需要授予该用户对该文件的访问权限。

但是,如果你真的得到FileNotFoundException这可能不是你的问题,所以请发布整个堆栈跟踪。

+0

主要发布更新请求的信息。 – Sorean 2012-02-13 23:34:21

+0

@JKM我最好的猜测是驱动器字母“T”未在服务器上正确映射。 – 2012-02-13 23:35:32

+0

@JKM我建议使用UNC路径,比如'\\ servername \ folder \ file.name'而不是映射驱动器。 – 2012-02-13 23:36:57

0

我认为这是因为您正在使用映射的驱动器名称访问映像。 相反,如果在IIS虚拟目录中使用T:\ Published \ Generic.jpg尝试UNC名称\ machineName \ Published \ Generic.jpg

+0

只需输入URL即可浏览图片的形象?没有通过你的imagedrawer.aspx? – ASetty 2012-02-13 23:45:14