我有一个在 firebird 数据库中存储一些数据的应用程序。我正在使用嵌入式 firebird 服务器和 EntityFramework,并且一切正常,但是当我通过表单上的 x 按钮关闭我的应用程序时,我收到一条 Windows 系统消息“应用程序已停止工作”,我无法捕捉到这个异常。我的应用程序中有一个 UnhandledExceptionHandler :
// Add handler for UI thread exceptions
Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
// Force all WinForms errors to go through handler
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//This handler is for catching non-UI thread exceptions
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
.....some other code..........
Application.Run(new MainForm());
但是这种异常从未被它捕获。所以我去了windows事件日志,在那里找到了错误事件的xml视图:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name="Application Error" />
<EventID Qualifiers="0">1000</EventID>
<Level>2</Level>
<Task>100</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2017-03-14T23:06:25.000000000Z" />
<EventRecordID>36077</EventRecordID>
<Channel>Application</Channel>
<Computer>MYPC</Computer>
<Security />
</System>
- <EventData>
<Data>MyApp.exe</Data>
<Data>1.0.0.0</Data>
<Data>58c7a3f0</Data>
<Data>fbintl.DLL</Data>
<Data>2.5.5.26952</Data>
<Data>5644432f</Data>
<Data>c0000005</Data>
<Data>00004e9c</Data>
<Data>1d64</Data>
<Data>01d29d1797fb7f0d</Data>
<Data>G:\Programming\WorkSpace\C#\MyApp\bin\x86\Debug\MyApp.exe</Data>
<Data>G:\Programming\WorkSpace\C#\MyApp\bin\x86\Debug\FireBirdEmbeddedServer\intl\fbintl.DLL</Data>
<Data>d84a6ca6-090a-11e7-8151-005056c00008</Data>
</EventData>
</Event>
如您所见,当应用程序已关闭时 fbintl.DLL 出现问题。那么我怎样才能得到关于这个问题的更详细的描述呢?
UPD 我让应用程序更短以检测我的问题的原因 - 现在只有这个 EF 代码在应用程序关闭之前运行
public async Task GetAutoAnswerTemplate()
{
try
{
using (var db = new FirebirdDbContext(embeddedConnectionString)){
//Async or sync methods doesn't affect to my problem
AutoAnswerTemplate template = await dbContext.AutoAnswerTemplate.FirstOrDefaultAsync();
return template?.AutoAnswer_body;
}
}
catch (Exception ex)
{
throw new EmbeddedFbDataBaseTools.EmbeddedDbException(
"Error while getting auto answer template" + "\r\n" + ex.Message, ex);
}
}
FirebirdDbContext 在哪里:
public class FirebirdDbContext : DbContext
{
public FirebirdDbContext(string connString)
: base(new FbConnection(connString), true)
{
//* The Entity initializer is bugged with Firebird embedded: http://stackoverflow.com/q/20959450/2504010 so I didn't use default--->
// Database.SetInitializer<FirebirdDBContext>(new CreateDatabaseIfNotExists<FirebirdDBContext>());
Database.SetInitializer<FirebirdDbContext>(new MyCreateDatabaseIfNotExists());
}
public DbSet<AutoAnswerTemplate> AutoAnswerTemplate { get; set; }
public DbSet<User> User { get; set; }
}
class MyCreateDatabaseIfNotExists : IDatabaseInitializer<FirebirdDbContext>
{
public void InitializeDatabase(FirebirdDbContext context)
{
if (!context.Database.Exists())
{
context.Database.Create();
}
}
}
连接参数是
public static string GetEmbeddeddefaultConnectionString()
{
FbConnectionStringBuilder builder = new FbConnectionStringBuilder
{
ServerType = FbServerType.Embedded,
DataSource = "localhost",
Port = 3050,
Database = EmbeddedDbPath, //Path to embedded db
ClientLibrary = EmbeddedServerDllPath,
UserID = "SYSDBA",
Password = "masterkey",
Charset = "WIN1251",
Dialect = 3,
ConnectionLifeTime = 15,
Pooling = true,
MinPoolSize = 0,
MaxPoolSize = 50
};
return builder.ToString();
}
新更新 25.04.2017
我用 firebird 嵌入式数据库制作了一个简单的应用程序来演示错误。你可以在这里找到它
该应用程序创建一个firebird嵌入式数据库并在后台线程(任务TPL)中连接到它,工作完成后(_bgTask.Status == TaskStatus.RanToCompletion)您关闭应用程序并收到错误消息。