Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t立即在Npgsql中接收多个通知_C#_Postgresql_Npgsql - Fatal编程技术网

C# Can';t立即在Npgsql中接收多个通知

C# Can';t立即在Npgsql中接收多个通知,c#,postgresql,npgsql,C#,Postgresql,Npgsql,今天,我编写了以下代码,用于名为Npgsql的PostgreSQL C#库: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Npgsql; namespace PostgreSQLNotificationsTest { class Program { static v

今天,我编写了以下代码,用于名为Npgsql的PostgreSQL C#库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;

namespace PostgreSQLNotificationsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=my_password;Database=helper_db.psql;SyncNotification=true"))
            {
                conn.Notification += OnNotification;
                conn.Open();
                using (var command = new NpgsqlCommand("listen notifytest;", conn))
                {
                    command.ExecuteNonQuery();
                }
                Console.ReadLine();
            };
        }

        private static void OnNotification(object sender, NpgsqlNotificationEventArgs e)
        {
            Console.WriteLine("event handled: " + e.AdditionalInformation);
        }
    }
}
然后我做下面的事情

createdb.exe-h 127.0.0.1-p 5432-U postgres-W helper_db.psql

psql.exe-h 127.0.0.1-p 5432-U postgres-W helper_db.psql

创建表帮助器(第一个文本主键,第二个整数)

在助手值中插入('first',10),('second',20)

在C#项目中,我得到以下输出:

已处理事件:插入前10个

没有“已处理事件:插入第二个20”。只有当我执行下一个操作(如“从助手删除”)时,我才会收到它


为什么?

除非您的客户端库支持检查网络套接字中的缓冲数据,否则接收通知的唯一方法是触发套接字上的其他活动

许多应用程序会定期发送一个空查询字符串(
“”
)来执行此操作

如果客户端库支持它,而您没有使用SSL,则可以定期在连接上调用某种checkForNotifications()函数。这在PgJDBC中是可能的,但我不知道nPgSQL,因此我只能建议您查看相关文档。

更新(2015-02-27):此错误已在github的主分支中修复。请注意,master branch是我们即将发布的3.0版本,可能有一些我们正在处理的bug。请试一试,让我知道它是否适合你

这是Npgsql中的一个bug。我们正在努力修复它。检查此拉取请求以了解有关它的更多信息:在特殊情况下,提交:

但我们正在尝试另一种方法来解决这个问题。请注意,只有在非常短的时间内发送多个通知时,才会出现此问题。如果在通知之间保持1秒或2秒的间隔,则可以正确传递通知

一旦我们把它修好,我会在这里更新我的答案


很抱歉,此问题可能会给您带来不便。

您应该会收到通知。我认为收到通知的部分存在一些问题。我查一下。
CREATE OR REPLACE FUNCTION nf() RETURNS TRIGGER AS $$
BEGIN
    PERFORM pg_notify('notifytest', format('INSERT %s %s', NEW.first, NEW.second));
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER any_after AFTER INSERT OR UPDATE OR DELETE ON helper FOR EACH ROW EXECUTE PROCEDURE nf();