我们最近将 SQLite
添加到在 IIS
上运行的现有 C# WebForms
应用程序中,但现在我们在从 Visual Studio
或 Team City
。
如果我们在用户访问了一个访问了 SQLite DB
的路由后尝试发布,我们会收到以下错误:
Unable to add 'bin/x86/SQLite.Interop.dll' to the Web site. Unable to add file 'bin\x86\SQLite.Interop.dll'. The process cannot access the file because it is being used by another process.
为了尽量减少问题范围,我执行了以下步骤:
- 创建了一个新的
C# Web API 项目。
- 添加了
System.Data.SQLite.Core NuGet 包。
- 添加了如下所示的代码。
- 添加了发布配置文件以发布到本地文件系统并发布。
- 运行以下
IISExpress
命令iisexpress/path:c:\path-to-application/port:8081
- 在浏览器中导航到
http://localhost:8081/api/values
,浏览器会正确响应来自相关SQLite 表
的行
如果我们随后尝试再次发布,我们会收到上述错误,这与我们在 IIS
下的生产环境中看到的问题相呼应。
为了成功部署到生产站点,我们必须停止应用程序池
,发布然后重新启动应用程序池
。
这远非理想。关于我们如何解决这个问题有什么想法吗?
这是代码...
public IEnumerable<string> Get()
{
var dbPath = HttpContext.Current.Server.MapPath("~/MyDB.sqlite");
using (var conn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", dbPath)))
{
var rows = new List<string>();
conn.Open();
var sql = "select * from myTable";
using (var command = new SQLiteCommand(sql, conn))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
rows.Add(reader["mycolumn"].ToString());
}
}
}
return rows;
}
}
最佳答案
您必须在部署期间停止该网站。 无论如何您都应该这样做,与 DLL 锁定无关,因为在部署期间处理的请求将因混合新旧代码而产生不可预测的结果。
关于c# - 发布包含 SQLite.Interop.dll 的项目失败,因为它正被另一个进程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41632850/