views:

292

answers:

2

I have a Windows Service that hangs when opening an OLEDB-connection to an Excel-file, like this:

using (var connection = new OleDbConnection(
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
    + fileName + ";Extended Properties=\"Excel 8.0\""))
{
  connection.Open();
  // start using the connection
}

This code works fine when running as a console application. When I debug the Windows Service with Visual Studio, I can step into the code until I hit the call to connection.Open(). At that point, the thread hangs. No exception is thrown. Visual Studio remains responsive, until I hit the "Break All" or "Stop Debugging" button. At that point, Visual Studio also hangs. When I kill the process, Visual Studio becomes responsive again.

Does anyone know why this happens and how to solve it?

EDIT: fileName is an absolute path; the file was written by the service itself.

A: 

I do not know why this happens, but have you tried to narrow it down by trying to open the file and simply load it into a byte array - to determine whether the issue is with the file system / permissions / etc... rather than OLE DB? If you can open and load the file into a byte array, but OLE DB still hangs, is the CPU pegged which would indicate that there might be something about the file which OLE DB cannot handle?

If you cannot get this to work with OLE DB, have you considered a 3rd party xls / xlsx library like SpreadsheetGear for .NET? You can see live samples here and download a free trial here.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
The file was written by the service itself. Loading the content into a byte array is no problem. When the thread hangs, the CPU is idle, so it seems like a lock. There's nothing wrong with the file, because the code works if I run it as a console application.
Tommy Carlier
+1  A: 

After unsuccessfully trying to do this inside the Windows Service, I extracted the business logic into a separate Console Application, and just call that application from within Windows Service. Seems to work fine.

Tommy Carlier
You may want to consider migrating it to a dll instead of a console app. A Windows service calling a console app has a funny smell...
Walter
If I compile it to a DLL and then link to that DLL from the service, the code will once again run inside the service process, and hang. The reason I extracted it to a separate console application is so it would run in a separate process.
Tommy Carlier