System.IO.IOException: The process cannot access the file because it is being used by another process.

Mar 9, 2010 at 4:22 PM

Calling the following lines of code in the ASP.Net MVC 2 application generates the error below:

 

Fonet.FonetDriver driver = Fonet.FonetDriver.Make();
driver.Render(FoPath, OutputPdfPath);

The file referenced by FoPath variable (“Intake_BD10-10001_20103308123353.fo”) is created by an XSLT transformation.

Using the ProcessExplorer from sysinternals.com we discovered that the process holding a file handle to the *.fo file is:

  • WebDev.WebServer.exe when running from Visual Studio 2008
  • w3wp.exe when running from IIS.

Why is the FonetDriver.Render() method failing in this case?
What could be done?
Is it a bug?
The Render() method should access this *.fo file in readonly mode. What’s the problem if another process has a handle on it?

 

Server Error in '/' Application.


System.IO.IOException: The process cannot access the file 'C:\tst\Temp\PdfTest\PdfTest\App_Data\Intake_BD10-10001_20103308123353.fo' because it is being used by another process.
  at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
  at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
  at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
  at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
  at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
  at System.Threading.CompressedStack.runTryCode(Object userData)
  at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
  at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
  at System.Xml.XmlTextReaderImpl.OpenUrl()
  at System.Xml.XmlTextReaderImpl.Read()
  at System.Xml.XmlTextReader.Read()
  at Fonet.Fo.FOTreeBuilder.Parse(XmlReader reader)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.SystemException: System.IO.IOException: The process cannot access the file 'C:\tst\Temp\PdfTest\PdfTest\App_Data\Intake_BD10-10001_20103308123353.fo' because it is being used by another process.
  at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
  at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
  at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
  at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
  at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
  at System.Threading.CompressedStack.runTryCode(Object userData)
  at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
  at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
  at System.Xml.XmlTextReaderImpl.OpenUrl()
  at System.Xml.XmlTextReaderImpl.Read()
  at System.Xml.XmlTextReader.Read()
  at Fonet.Fo.FOTreeBuilder.Parse(XmlReader reader)

Source Error:

 

Line 134:        {

Line 135:            Fonet.FonetDriver driver = Fonet.FonetDriver.Make();

Line 136:            driver.Render(FOPath, PDFPath);

Line 137:            driver = null;

Line 138:        }


Source File: C:\tst\Temp\PdfTest\PdfTest\Controllers\HomeController.cs    Line: 136

Stack Trace:

 

[SystemException: System.IO.IOException: The process cannot access the file 'C:\tst\Temp\PdfTest\PdfTest\App_Data\Intake_BD10-10001_20103308123353.fo' because it is being used by another process.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)

   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)

   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)

   at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)

   at System.Threading.CompressedStack.runTryCode(Object userData)

   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)

   at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)

   at System.Xml.XmlTextReaderImpl.OpenUrl()

   at System.Xml.XmlTextReaderImpl.Read()

   at System.Xml.XmlTextReader.Read()

   at Fonet.Fo.FOTreeBuilder.Parse(XmlReader reader)]

   Fonet.FonetDriver.FireFonetError(String message) +112

   Fonet.Fo.FOTreeBuilder.Parse(XmlReader reader) +1088

   Fonet.FonetDriver.Render(XmlReader inputReader, Stream outputStream) +261

   Fonet.FonetDriver.Render(String inputFile, String outputFile) +101

   PdfTest.Controllers.HomeController.PDFGeneration(String FOPath, String PDFPath) in C:\tst\Temp\PdfTest\PdfTest\Controllers\HomeController.cs:136

   PdfTest.Controllers.HomeController.IntakePDF(String id) in C:\tst\Temp\PdfTest\PdfTest\Controllers\HomeController.cs:97

   lambda_method(ExecutionScope , ControllerBase , Object[] ) +140

   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17

   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178

   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24

   System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +52

   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254

   System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +19

   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192

   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314

   System.Web.Mvc.Controller.ExecuteCore() +105

   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39

   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7

   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34

   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21

   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12

   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59

   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44

   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7

   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150

   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

 


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927