From 5aeabe9637236e9b10e4433acf62a0f5c225e15e Mon Sep 17 00:00:00 2001 From: Brian Pursley Date: Wed, 22 May 2024 10:32:04 -0400 Subject: [PATCH] Add Npgmq.Example project. --- Npgmq.Example/Npgmq.Example.csproj | 20 ++++++++++++++++++ Npgmq.Example/Program.cs | 33 ++++++++++++++++++++++++++++++ Npgmq.Example/README.md | 3 +++ Npgmq.sln | 6 ++++++ README.md | 6 ++++-- 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 Npgmq.Example/Npgmq.Example.csproj create mode 100644 Npgmq.Example/Program.cs create mode 100644 Npgmq.Example/README.md diff --git a/Npgmq.Example/Npgmq.Example.csproj b/Npgmq.Example/Npgmq.Example.csproj new file mode 100644 index 0000000..e9e7233 --- /dev/null +++ b/Npgmq.Example/Npgmq.Example.csproj @@ -0,0 +1,20 @@ + + + + Exe + net8.0 + enable + enable + ffbcfb1f-57f6-4fca-96ae-4a0e2b43c970 + + + + + + + + + + + + diff --git a/Npgmq.Example/Program.cs b/Npgmq.Example/Program.cs new file mode 100644 index 0000000..72ce1dc --- /dev/null +++ b/Npgmq.Example/Program.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using Microsoft.Extensions.Configuration; +using Npgmq; + +var configuration = new ConfigurationBuilder() + .AddEnvironmentVariables() + .AddUserSecrets(Assembly.GetExecutingAssembly()) + .Build(); + +var npgmq = new NpgmqClient(configuration.GetConnectionString("ExampleDB")!); + +await npgmq.InitAsync(); +await npgmq.CreateQueueAsync("example_queue"); + +var msgId = await npgmq.SendAsync("example_queue", new MyMessageType +{ + Foo = "Test", + Bar = 123 +}); +Console.WriteLine($"Sent message with id {msgId}"); + +var msg = await npgmq.ReadAsync("example_queue"); +if (msg != null) +{ + Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}"); + await npgmq.ArchiveAsync("example_queue", msg.MsgId); +} + +internal class MyMessageType +{ + public string Foo { get; set; } = null!; + public int Bar { get; set; } +} \ No newline at end of file diff --git a/Npgmq.Example/README.md b/Npgmq.Example/README.md new file mode 100644 index 0000000..faade7a --- /dev/null +++ b/Npgmq.Example/README.md @@ -0,0 +1,3 @@ +# Npgmq.Example + +Example project for Npgmq. diff --git a/Npgmq.sln b/Npgmq.sln index e1461d6..21e8272 100644 --- a/Npgmq.sln +++ b/Npgmq.sln @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{ .github\workflows\build.yml = .github\workflows\build.yml EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Npgmq.Example", "Npgmq.Example\Npgmq.Example.csproj", "{60A266EF-6EFA-42B0-B592-C0C50BC32C7C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,6 +31,10 @@ Global {27C187EE-9298-452F-9FA7-6DF8FC381095}.Debug|Any CPU.Build.0 = Debug|Any CPU {27C187EE-9298-452F-9FA7-6DF8FC381095}.Release|Any CPU.ActiveCfg = Release|Any CPU {27C187EE-9298-452F-9FA7-6DF8FC381095}.Release|Any CPU.Build.0 = Release|Any CPU + {60A266EF-6EFA-42B0-B592-C0C50BC32C7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60A266EF-6EFA-42B0-B592-C0C50BC32C7C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60A266EF-6EFA-42B0-B592-C0C50BC32C7C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60A266EF-6EFA-42B0-B592-C0C50BC32C7C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {8C37002D-05C6-4B1F-B4FC-C2F45C5E5328} = {023319FF-914F-42F7-AE34-3BA9CF91DAEE} diff --git a/README.md b/README.md index 8724b99..8dd95d2 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ using Npgmq; var npgmq = new NpgmqClient(""); +await npgmq.InitAsync(); + await npgmq.CreateQueueAsync("my_queue"); var msgId = await npgmq.SendAsync("my_queue", new MyMessageType @@ -35,7 +37,7 @@ var msg = await npgmq.ReadAsync("my_queue"); if (msg != null) { Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}"); - await npgmq.ArchiveAsync("my_queue", msg!.MsgId); + await npgmq.ArchiveAsync("my_queue", msg.MsgId); } ``` @@ -59,7 +61,7 @@ var msg = await npgmq.ReadAsync("my_queue"); if (msg != null) { Console.WriteLine($"Read message with id {msg.MsgId}: {msg.Message}"); - await npgmq.ArchiveAsync("my_queue", msg!.MsgId); + await npgmq.ArchiveAsync("my_queue", msg.MsgId); } ```