我们有一个 .NET Windows 服务将 WCF 服务暴露给用户界面和我们系统的其他部分。它以 .NET Framework 4.5 为目标并使用 SQLite 1.0.92 二进制文件与底层 SQLite 数据库通信。但是,Windows 服务在运行一段时间后崩溃(自动停止),并在 SQLite.Interop.dll 中出现 AccessViolationException(通过 Windows 事件查看器发现)。我在连接关闭中看到过讨论此异常的文章,但在我们所有的案例中,我们在使用 WCF 服务公开的方法查询或写入数据库时遇到此异常。堆栈跟踪如下:
Application: OurServer.exe
Framework Version: **v4.0.30319**
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
at System.Data.SQLite.UnsafeNativeMethods.sqlite3_bind_int(IntPtr, Int32, Int32)
at System.Data.SQLite.UnsafeNativeMethods.sqlite3_bind_int(IntPtr, Int32, Int32)
at System.Data.SQLite.SQLite3.Bind_Int32(System.Data.SQLite.SQLiteStatement, System.Data.SQLite.SQLiteConnectionFlags, Int32, Int32)
at System.Data.SQLite.SQLiteStatement.BindParameter(Int32, System.Data.SQLite.SQLiteParameter)
at System.Data.SQLite.SQLiteStatement.BindParameters()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(System.Data.SQLite.SQLiteCommand, System.Data.CommandBehavior)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(System.Data.CommandBehavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(System.Data.CommandBehavior)
at DataAccess.Sqlite.ExecuteCommand(System.Collections.ObjectModel.Collection`1<System.String>, System.Collections.ObjectModel.Collection`1<System.Data.Common.DbParameter[]>)
at Data.Settings.Save(System.Collections.ObjectModel.Collection`1<Common.Operation>)
at DynamicClass.SyncInvokeSaveOperation(System.Object, System.Object[], System.Object[])
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(System.Object, System.Object[], System.Object[] ByRef)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(System.ServiceModel.Dispatcher.MessageRpc ByRef)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(System.ServiceModel.Dispatcher.MessageRpc ByRef)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(System.ServiceModel.Dispatcher.MessageRpc ByRef)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean)
at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(System.ServiceModel.Channels.RequestContext, Boolean, System.ServiceModel.OperationContext)
at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(System.ServiceModel.Channels.RequestContext, System.ServiceModel.OperationContext)
at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(System.IAsyncResult)
at System.Runtime.Fx+AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult)
at System.Runtime.AsyncResult.Complete(Boolean)
at System.Runtime.InputQueue`1+AsyncQueueReader[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Set(Item<System.__Canon>)
at System.Runtime.InputQueue`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].EnqueueAndDispatch(Item<System.__Canon>, Boolean)
at System.Runtime.InputQueue`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].EnqueueAndDispatch(System.__Canon, System.Action, Boolean)
at System.ServiceModel.Channels.SingletonChannelAcceptor`3[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Enqueue(System.__Canon, System.Action, Boolean)
at System.ServiceModel.Channels.ConnectionDemuxer+CompleteSingletonPreambleAndDispatchRequestAsyncResult.OnPreambleComplete(System.IAsyncResult)
at System.Runtime.Fx+AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult)
at System.Runtime.AsyncResult.Complete(Boolean)
at System.ServiceModel.Channels.ServerSingletonPreambleConnectionReader+CompletePreambleAsyncResult.OnWriteCompleted(System.Object)
at System.ServiceModel.Channels.SocketConnection.OnSendAsync(System.Object, System.Net.Sockets.SocketAsyncEventArgs)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(System.Net.Sockets.SocketError, Int32, System.Net.Sockets.SocketFlags)
at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
我们正在使用来自“sqlite-netFx45-binary-bundle-Win32-2012-1.0.92.0”的 SQLite 程序集(从 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 下载)。这些程序集作为 Windows 服务的一部分捆绑在一起,不在 GAC 中。此行为在 32 位和 64 位计算机中是一致的。仅供引用,我们不使用混合模式程序集。
我们的连接字符串:
Data Source=ourapp.db;Version=3;New=False;Compress=True;PRAGMA cache_size=20000; PRAGMA page_size=32768; PRAGMA synchronous=off"
SQLite 数据库文件是 Windows 的“ProgramData”文件夹。
堆栈跟踪显示框架版本为“v4.0.30319”,而我们在服务的应用程序配置中明确将目标版本设置为 4.5。但是,机器同时安装了这两个版本。
另外,我编写了一个简单的控制台应用程序,它从多个线程调用相同的 WCF 服务方法,但无法模拟相同的 AccessViolationException。因此,我认为这不是负载或并发访问相关的问题。异常似乎是随机的,除了运行服务并等待它发生之外,我们没有办法始终如一地重现问题。
非常感谢任何指出可能导致此问题的原因。
更新:
正在使用的 ExecuteCommand 的两个变体的代码:
public int ExecuteCommand(string query, params DbParameter[] parameters)
{
try
{
this.result = -1;
this.OpenConnection();
this.command = new SQLiteCommand(query, this.connnection);
this.HandleParameters(parameters);
this.result = this.command.ExecuteNonQuery();
}
catch (Exception ex)
{
this.result = -1;
}
finally
{
if (this.command != null)
{
this.command.Dispose();
}
this.CloseConnection();
}
return this.result;
}
public int ExecuteCommand(Collection<string> queries, Collection<DbParameter[]> parameters)
{
try
{
this.result = -1;
this.OpenConnection();
this.command = new SQLiteCommand();
this.command.Connection = this.connnection;
this.transaction = this.connnection.BeginTransaction();
for (int i = 0; i < queries.Count; i++)
{
this.command.Parameters.Clear();
this.command.CommandText = queries[i];
this.command.CommandTimeout = this.timeOut;
this.command.Transaction = this.transaction;
DbParameter[] cmdParams = new DbParameter[] { };
if (parameters != null)
{
cmdParams = parameters[i];
}
this.HandleParameters(cmdParams);
this.result += this.command.ExecuteNonQuery();
}
this.transaction.Commit();
}
catch (Exception ex)
{
if (this.transaction != null)
{
this.transaction.Rollback();
}
this.result = -1;
}
finally
{
if (this.command != null)
{
this.command.Dispose();
}
this.CloseConnection();
}
return this.result;
}
更新 2:保存方法的代码
Collection<DbParameter[]> dbparameters = new Collection<DbParameter[]>();
DbParameter[] dbparams;
SQLiteParameter sqlparams;
Collection<string> queries = new Collection<string>();
int icount = 0;
foreach (Operation operation in operations)
{
icount = 0;
dbparams = new DbParameter[4];
queries.Add("UPDATE table1 SET col1 = @Col1, col2 = @col2, " +
"Timestamp = @Timestamp WHERE Id = @Id");
sqlparams = new SQLiteParameter();
sqlparams.DbType = DbType.String;
sqlparams.ParameterName = "@Timestamp";
sqlparams.Value = string.Format(CultureInfo.InvariantCulture, "{0:yyyy-MM-dd HH:mm:ss}", operation.Timestamp);
dbparams[icount++] = sqlparams;
sqlparams = new SQLiteParameter();
sqlparams.DbType = DbType.String;
sqlparams.ParameterName = "@Id";
sqlparams.Value = operation.Id;
dbparams[icount++] = sqlparams;
sqlparams = new SQLiteParameter();
sqlparams.DbType = DbType.String;
sqlparams.ParameterName = "@Col1";
sqlparams.Value = operation.Col1;
dbparams[icount++] = sqlparams;
sqlparams = new SQLiteParameter();
sqlparams.DbType = DbType.String;
sqlparams.ParameterName = "@Col2";
sqlparams.Value = operation.Col2;
dbparams[icount++] = sqlparams;
dbparameters.Add(dbparams);
}
return (DataAccess.Sqlite.ExecuteCommand(queries, dbparameters) > -1);
最佳答案
错误与提供者失去对打开的连接的跟踪一致:
- 试图关闭一个不存在的连接
- 尝试使用不存在的连接
该错误看起来像是连接池的问题,但您的连接字符串不使用连接池,默认情况下它是关闭的。
在打开连接池的情况下是否还有其他访问同一个数据库的情况?
您必须做的第一件事是记录异常。
关于c# - WCF 服务中的 SQLite AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23753350/