我在C#上的项目使用Npgsql库。在NuGet PM从beta版更新到2.0.14.3后,应用程序已经崩溃:它等待连接状态更改事件以启动整个工作,但DB连接没有调用事件。
我将代码最小化到下一个块:
using System;
using Npgsql;
using System.Data;
namespace TestPostgresStateEvent
{
public static class Program
{
private static NpgsqlConnection connection = null;
private static System.Timers.Timer checkerTimer = new System.Timers.Timer(1000);
public static void Main(string[] args)
{
connection = new NpgsqlConnection();
connection.StateChange += OnConnectionStateChanged;
checkerTimer.Elapsed += (sender, arg) => CheckConnectionState();
connection.ConnectionString = "Server=127.0.0.1;Port=5432;User Id=user;Password=qwerty;Database=mydb;";
connection.Open();
checkerTimer.Start();
Console.WriteLine("Waiting for connection...");
Console.ReadLine();
checkerTimer.Stop();
connection.Close();
}
private static void CheckConnectionState()
{
if (connection.FullState == ConnectionState.Open)
{
Console.WriteLine("Checker: Connection established!");
}
}
private static void OnConnectionStateChanged(object sender, StateChangeEventArgs args)
{
if (sender == connection)
{
if (args.CurrentState == ConnectionState.Open)
{
Console.WriteLine("Event: Connection established!");
}
}
}
}
}
并获得了下一个输出:
等待连接...
Checker:已建立连接!
Checker:已建立连接!
Checker:已建立连接!
...
那么,这是一个错误吗?这是我的错吗?我在C#方面没有太多经验。