You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To install the package via Nuget, run the following command:
dotnet add package Npgmq
Usage
Here is an example that uses Npgmq to create a queue and then send/read/archive a message:
usingNpgmq;varnpgmq=newNpgmqClient("<YOUR CONNECTION STRING HERE>");awaitnpgmq.InitAsync();awaitnpgmq.CreateQueueAsync("my_queue");varmsgId=awaitnpgmq.SendAsync("my_queue",newMyMessageType{Foo="Test",Bar=123});Console.WriteLine($"Sent message with id {msgId}");varmsg=awaitnpgmq.ReadAsync<MyMessageType>("my_queue");if(msg!=null){Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}");awaitnpgmq.ArchiveAsync("my_queue",msg.MsgId);}
This example assumes you have defined a class called MyMessageType with the structure something like:
You can send and read messages as JSON strings, like this:
varmsgId=awaitnpgmq.SendAsync("my_queue","{\"foo\":\"Test\",\"bar\":123}");Console.WriteLine($"Sent message with id {msgId}");varmsg=awaitnpgmq.ReadAsync<string>("my_queue");if(msg!=null){Console.WriteLine($"Read message with id {msg.MsgId}: {msg.Message}");awaitnpgmq.ArchiveAsync("my_queue",msg.MsgId);}
You can pass your own NpgsqlConnection to the NpgmqClient constructor, like this:
Npgmq uses Npgsql internally to connect to the database.
Using a Connection String
If you pass an Npgsql connection string to the NpgmqClient constructor, it will use this connection string to create an NpgsqlConnection object internally, and the connection lifetime will be managed by NpgmqClient.
Using a Connection Object
If you pass an NpgsqlConnection object to the NpgmqClient constructor, it will use this connection instead of creating its own.