Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop IIS 7 locking .XSLT file in C#

I have the following lines of code:

xslt.Load(XmlReader.Create(new FileStream(@"C:\website\TransList.xslt", System.IO.FileMode.Open)));

xslt.Transform(mydoc.CreateReader(),null, sw);

It works fine, if I stop the project and launch it again, I get the following error:

[System.IO.IOException] = {"The process cannot access the file 'C:\website\TransList.xslt' because it is being used by another process."}

I then have have to goto the command line and do a IISRESET to get, I can also reset the app pool, this is easiest at this time as this is just my dev box.

Now I do have the call in a try catch statement, but I cannot access the xslt object in the handler.

The xslt object doesn't seem to have a close or dispose method.

The garbage collector never gets a shot at it , it seems.

Any ideas?

like image 877
James Campbell Avatar asked Feb 06 '10 14:02

James Campbell


2 Answers

You will need to close your FileStream and Reader, either explicitly using .Close() or via a using statement:

using (FileStream fs = new FileStream(@"C:\website\TransList.xslt", System.IO.FileMode.Open))
   {
    xslt.Load(XmlReader.Create(fs));
    using (var reader = mydoc.CreateReader())
        {
         xslt.Transform(reader, null, sw);
        }
     }
like image 157
stuartd Avatar answered Sep 29 '22 10:09

stuartd


There is no need to explicitly create a FileStream and an XmlReader, if you know the file location then you can simply pass that to the Load method, using this overload:

XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(@"C:\website\Translist.xslt");

If you think you need to create a FileStream and an XmlReader then I agree with the suggestions already made, use the 'using' statement to properly close and dispose of those objects.

like image 39
Martin Honnen Avatar answered Sep 29 '22 09:09

Martin Honnen