Skip to content

Commit

Permalink
Fixed serialization problem in PublishModel/PublishHeaders examples (#…
Browse files Browse the repository at this point in the history
…296)

* Fixed a serialization problem in the sandbox/Example.Core.PublishModel/Header examples.
- Added the reflection based JsonSerializer as default serializer. This way the Bar object can be (de)serialized and the examples work.

* - Switched to using NatsJsonSerializerRegistry.Default instead of the default constructor.

---------

Co-authored-by: Tobias Hoefer <tobias.hoefer@krones.com>
  • Loading branch information
robibobi and tobiashoefer-kro authored Dec 21, 2023
1 parent 944caab commit 31dc087
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Client.Core\NATS.Client.Core.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.Serializers.Json\NATS.Client.Serializers.Json.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 7 additions & 2 deletions sandbox/Example.Core.PublishHeaders/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// > nats sub bar.*
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.Serializers.Json;

var subject = "bar.xyz";
var options = NatsOpts.Default with { LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()) };
var options = NatsOpts.Default with
{
LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()),
SerializerRegistry = NatsJsonSerializerRegistry.Default,
};

Print("[CON] Connecting...\n");

Expand All @@ -12,7 +17,7 @@
for (var i = 0; i < 10; i++)
{
Print($"[PUB] Publishing to subject ({i}) '{subject}'...\n");
await connection.PublishAsync<Bar>(
await connection.PublishAsync(
subject,
new Bar { Id = i, Name = "Baz" },
headers: new NatsHeaders { ["XFoo"] = $"bar{i}" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Client.Core\NATS.Client.Core.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.Serializers.Json\NATS.Client.Serializers.Json.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 6 additions & 2 deletions sandbox/Example.Core.PublishModel/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// > nats sub bar.*
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.Serializers.Json;

var subject = "bar.xyz";
var options = NatsOpts.Default with { LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()) };
var options = NatsOpts.Default with {
LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()),
SerializerRegistry = NatsJsonSerializerRegistry.Default,
};

Print("[CON] Connecting...\n");

Expand All @@ -12,7 +16,7 @@
for (var i = 0; i < 10; i++)
{
Print($"[PUB] Publishing to subject ({i}) '{subject}'...\n");
await connection.PublishAsync<Bar>(subject, new Bar { Id = i, Name = "Baz" });
await connection.PublishAsync(subject, new Bar { Id = i, Name = "Baz" });
}

void Print(string message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Client.Core\NATS.Client.Core.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.Serializers.Json\NATS.Client.Serializers.Json.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 8 additions & 4 deletions sandbox/Example.Core.SubscribeHeaders/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
// > nats pub bar.xyz --count=10 "my_message_{{ Count }}" -H X-Foo:Baz

using System.Text;
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.Serializers.Json;

var subject = "bar.*";
var options = NatsOpts.Default with { LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()) };
var options = NatsOpts.Default with
{
LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()),
SerializerRegistry = NatsJsonSerializerRegistry.Default,
};

Print("[CON] Connecting...\n");

await using var connection = new NatsConnection(options);

Print($"[SUB] Subscribing to subject '{subject}'...\n");

await foreach (var msg in connection.SubscribeAsync<byte[]>(subject))
await foreach (var msg in connection.SubscribeAsync<Bar>(subject))
{
Print($"[RCV] {msg.Subject}: {Encoding.UTF8.GetString(msg.Data!)}\n");
Print($"[RCV] {msg.Subject}: {msg.Data!}\n");
if (msg.Headers != null)
{
foreach (var (key, values) in msg.Headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Client.Core\NATS.Client.Core.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.Serializers.Json\NATS.Client.Serializers.Json.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion sandbox/Example.Core.SubscribeModel/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.Serializers.Json;

var subject = "bar.*";
var options = NatsOpts.Default with { LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()) };
var options = NatsOpts.Default with
{
LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()),
SerializerRegistry = NatsJsonSerializerRegistry.Default,
};

Print("[CON] Connecting...\n");

Expand Down

0 comments on commit 31dc087

Please sign in to comment.