FileStream ctor throws sharing violation in SSIS script task

I have an Integration Services package (VS 2012) that loads values from an Excel workbook and stores them in SQL Server. I'm using a 3rd party library to extract the workbook data in a C# script task, but before that, in the task, I create a Filestream:

MessageBox.Show("Step 1");

using (FileStream fsWorkbook = new FileStream(strWkbkFilePath, FileMode.Open,      

FileAccess.Read, FileShare.ReadWrite))

{    MessageBox.Show("Step 2");   

//...

}



The FileStream construction crashes with the error "The process cannot access the file ... because it is being used by another process." I know that this is where the crash occurs, because I see the "Step 1" message box before the exception message, but not "Step 2".

What's frustrating is that, if I set a breakpoint in the the script task, before the line causing the error, and start stepping through the code, I'm able to proceed and access the workbook file without error. At first I thought this might be due to a race condition, but then I added the message box shown above, which also pauses execution before the file stream is created. The pausing due to the MessageBox doesn't prevent the error when running in non-debug mode.

Note that I'm using FileShare.ReadWrite in the FileStream constructor, even though no other process should have the workbook open at the time.

So why do I get the sharing error? And why does it not happen when debugging?

April 24th, 2015 12:31pm

Odd that with SSIS you need to open it FileStram, well, but then just make a copy of the file aside and make the processing going off the copy.

Free Windows Admin Tool Kit Click here and download it now
April 24th, 2015 9:35pm

As mentioned in my question, I'm using a 3rd-party class library.  The class takes a FileStream object as input to its constru
April 26th, 2015 12:46pm

Do you use the file anywhere else in the package outside this script?  Is the third-party library an SSIS component or one used only in this script?

Free Windows Admin Tool Kit Click here and download it now
April 26th, 2015 1:28pm

Thanks, Dan.  That was it.

The I had opened the workbook in a previous script task, in order to get the list of sheets, but never closed it.  Once I fixed that, the file stream opened in the other script.

April 26th, 2015 8:33pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics