Npgsql-手册 联系客服

发布时间 : 星期四 文章Npgsql-手册更新完毕开始阅读3099c250a76e58fafab003aa

Console.WriteLine(\ t.Start(); Console.WriteLine(command.ExecuteScalar()); conn.Close(); } catch(NpgsqlException e) { if (e.Code == \ Console.WriteLine(\cancelled\ } } public static void CancelRequest() { command.Cancel(); Console.WriteLine(\ } } Working with Notifications

Npgsql allows user to receive events based on notifications sent by a server. There are two ways to receive notications with Npgsql: asynchronously or synchronously. Synchronous notification is only supported by Npgsql 1.0 and above.

苏州纳米科技发展有限公司 Nanopolis Suzhou Co., Ltd 苏州工业园区金鸡湖大道99号苏州纳米城中北区23幢综合楼

Bldg CN-23, Nanopolis Suzhou, 99 Jinji Lake Avenue, Suzhou Industrial Park, Jiangsu Province, P.R. China 215123

电话:+86 0512 69993000 传真: +86 0512 69993999 网站:www.nanopolis.cn

49

Asynchronous notifications

This is the default notification mechanism used in Npgsql. It is called asynchronous because Npgsql doesn't receive a notification upon execution of the event which generated it on the server. Npgsql receives the notification on the next instance of client interaction with the server. This interaction actually occurs when Npgsql sends a subsequent command to the server- which might consist of a few seconds to many hours later. With this in mind, most users will need to actively poll the server in order to recieve notifications in a timely matter. One approach involves polling via empty commands such as \

Synchronous notifications

Starting with Npgsql 1.0, there is support for synchronous notifications. When working in this mode, Npgsql is able to receive a notificaton upon its instantiation and deliver it to client. All this is done without any additional interaction between the client and server (as described above).

Important notice: When using Synchronous notification, you can't execute commands inside your notification handler function. If you do so, you will hang Npgsql as the thread which handles the notification is the same which handles Npgsql communication with backend. If you want to use any commands, please, create another connection and use it instead. This is not the best solution but we are studying better ways to do that instead of needing another connection.

The code to receive the notification is the same for both modes:

using System; using System.Data; using Npgsql; using NpgsqlTypes; using System.Threading; 苏州纳米科技发展有限公司 Nanopolis Suzhou Co., Ltd 苏州工业园区金鸡湖大道99号苏州纳米城中北区23幢综合楼

Bldg CN-23, Nanopolis Suzhou, 99 Jinji Lake Avenue, Suzhou Industrial Park, Jiangsu Province, P.R. China 215123

电话:+86 0512 69993000 传真: +86 0512 69993999 网站:www.nanopolis.cn

50