打开 OLEDB 连接到 Excel 文件时 Windows 服务挂起
我有一个 Windows 服务,在打开与 Excel 文件的 OLEDB 连接时挂起,如下所示:
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
}
此代码在作为控制台应用程序运行时工作正常。当我使用 Visual Studio 调试 Windows 服务时,我可以单步执行代码,直到调用 connection.Open()。此时,线程挂起。没有抛出异常。 Visual Studio 保持响应,直到我点击“全部打破”或“停止调试”按钮。此时,Visual Studio 也会挂起。当我终止该进程时,Visual Studio 会再次响应。
有谁知道为什么会发生这种情况以及如何解决它?
编辑:文件名是绝对路径;该文件是由服务本身编写的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试在 Windows 服务内执行此操作失败后,我将业务逻辑提取到一个单独的控制台应用程序中,然后从 Windows 服务内调用该应用程序。似乎工作正常。
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.
我不知道为什么会发生这种情况,但是您是否尝试通过尝试打开文件并将其加载到字节数组中来缩小范围 - 以确定问题是否与文件系统/权限/等有关......而不是OLE数据库?如果您可以打开文件并将其加载到字节数组中,但 OLE DB 仍然挂起,CPU 是否已锁定,这表明文件中可能存在 OLE DB 无法处理的内容?
如果您无法使其与 OLE DB 一起使用,您是否考虑过使用第三方 xls / xlsx 库,例如 SpreadsheetGear for .NET< /a>?您可以此处查看实时示例并下载免费试用版此处。
免责声明:我拥有 SpreadsheetGear LLC
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