Skip to content

Commit

Permalink
Add Npgmq.Example project.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpursley committed May 22, 2024
1 parent 0a3ab2b commit 5aeabe9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Npgmq.Example/Npgmq.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>ffbcfb1f-57f6-4fca-96ae-4a0e2b43c970</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Npgmq\Npgmq.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions Npgmq.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -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<MyMessageType>("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; }
}
3 changes: 3 additions & 0 deletions Npgmq.Example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Npgmq.Example

Example project for Npgmq.
6 changes: 6 additions & 0 deletions Npgmq.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ using Npgmq;

var npgmq = new NpgmqClient("<YOUR CONNECTION STRING HERE>");

await npgmq.InitAsync();

await npgmq.CreateQueueAsync("my_queue");

var msgId = await npgmq.SendAsync("my_queue", new MyMessageType
Expand All @@ -35,7 +37,7 @@ var msg = await npgmq.ReadAsync<MyMessageType>("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);
}
```

Expand All @@ -59,7 +61,7 @@ var msg = await npgmq.ReadAsync<string>("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);
}
```

Expand Down

0 comments on commit 5aeabe9

Please sign in to comment.