Skip to content

Commit 7cbf10d

Browse files
committed
dotnet-amqp: decode message bodies with BodyAsString()
Made-with: Cursor
1 parent 7181ef4 commit 7cbf10d

9 files changed

Lines changed: 25 additions & 27 deletions

File tree

dotnet-amqp/PublisherConfirms/Program.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
.Queue(queueName)
2424
.MessageHandler((ctx, message) =>
2525
{
26-
Console.WriteLine($"Received a message: {Encoding.UTF8.GetString(message.Body()!)}");
26+
Console.WriteLine($"Received a message: {message.BodyAsString()}");
2727
ctx.Accept();
2828
return Task.CompletedTask;
2929
})
@@ -38,13 +38,16 @@
3838
{
3939
const string text = "hello";
4040
PublishResult pr = await publisher.PublishAsync(new AmqpMessage(Encoding.UTF8.GetBytes(text)));
41-
if (pr.Outcome.State == OutcomeState.Accepted)
42-
{
43-
Console.WriteLine("Confirmed");
44-
}
45-
else
46-
{
47-
Console.WriteLine($"Unexpected outcome: {pr.Outcome.State}");
41+
switch (pr.Outcome.State) {
42+
case OutcomeState.Accepted:
43+
Console.WriteLine($" Accepted Message: {pr.Message.BodyAsString()} confirmed");
44+
break;
45+
case OutcomeState.Released: // here the message is not routed
46+
Console.WriteLine($" Released Message: {pr.Message.BodyAsString()} Released");
47+
break;
48+
case OutcomeState.Rejected: // here there is also the error: `pr.Outcome.Error`
49+
Console.WriteLine($"[Publisher] Message: {pr.Message.BodyAsString()} Rejected with error: {pr.Outcome.Error}");
50+
break;
4851
}
4952

5053
await Task.Delay(500);

dotnet-amqp/RPCClient/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
Console.WriteLine($" [x] Requesting fib({iStr})");
2929
IMessage request = new AmqpMessage(Encoding.UTF8.GetBytes(iStr));
3030
IMessage reply = await requester.PublishAsync(request);
31-
string response = Encoding.UTF8.GetString(reply.Body()!);
31+
string response = reply.BodyAsString();
3232
Console.WriteLine($" [.] Got '{response}'");
3333
}
3434
}

dotnet-amqp/RPCServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
string response = "";
3030
try
3131
{
32-
string message = Encoding.UTF8.GetString(request.Body()!);
32+
string message = request.BodyAsString();
3333
int n = int.Parse(message);
3434
Console.WriteLine($" [.] fib({message})");
3535
response += Fib(n);

dotnet-amqp/Receive/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using RabbitMQ.AMQP.Client;
1+
using RabbitMQ.AMQP.Client;
32
using RabbitMQ.AMQP.Client.Impl;
43

54
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
@@ -22,7 +21,7 @@
2221
.Queue("hello")
2322
.MessageHandler((ctx, message) =>
2423
{
25-
Console.WriteLine($"Received a message: {Encoding.UTF8.GetString(message.Body()!)}");
24+
Console.WriteLine($"Received a message: {message.BodyAsString()}");
2625
ctx.Accept();
2726
return Task.CompletedTask;
2827
})

dotnet-amqp/ReceiveLogs/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using RabbitMQ.AMQP.Client;
1+
using RabbitMQ.AMQP.Client;
32
using RabbitMQ.AMQP.Client.Impl;
43

54
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
@@ -33,7 +32,7 @@
3332
.Queue(queueName)
3433
.MessageHandler((ctx, message) =>
3534
{
36-
string body = Encoding.UTF8.GetString(message.Body()!);
35+
string body = message.BodyAsString();
3736
Console.WriteLine($" [x] Received '{body}'");
3837
ctx.Accept();
3938
return Task.CompletedTask;

dotnet-amqp/ReceiveLogsDirect/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using RabbitMQ.AMQP.Client;
1+
using RabbitMQ.AMQP.Client;
32
using RabbitMQ.AMQP.Client.Impl;
43

54
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
@@ -42,7 +41,7 @@
4241
.Queue(queueName)
4342
.MessageHandler((ctx, message) =>
4443
{
45-
string body = Encoding.UTF8.GetString(message.Body()!);
44+
string body = message.BodyAsString();
4645
string routingKey = RoutingKey(message);
4746
Console.WriteLine($" [x] Received '{routingKey}':'{body}'");
4847
ctx.Accept();

dotnet-amqp/ReceiveLogsTopic/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using RabbitMQ.AMQP.Client;
1+
using RabbitMQ.AMQP.Client;
32
using RabbitMQ.AMQP.Client.Impl;
43

54
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
@@ -42,7 +41,7 @@
4241
.Queue(queueName)
4342
.MessageHandler((ctx, message) =>
4443
{
45-
string body = Encoding.UTF8.GetString(message.Body()!);
44+
string body = message.BodyAsString();
4645
string routingKey = RoutingKey(message);
4746
Console.WriteLine($" [x] Received '{routingKey}':'{body}'");
4847
ctx.Accept();

dotnet-amqp/RpcAmqp10/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async Task RunServerAsync(CancellationToken cancellationToken)
6464
.RequestQueue(requestQueue)
6565
.Handler((ctx, request) =>
6666
{
67-
string payload = Encoding.UTF8.GetString(request.Body()!);
67+
string payload = request.BodyAsString();
6868
object? mid = request.MessageId();
6969
if (mid != null)
7070
{
@@ -140,7 +140,7 @@ async Task RunClientAsync(CancellationToken cancellationToken)
140140
continue;
141141
}
142142

143-
string payload = Encoding.UTF8.GetString(reply.Body()!);
143+
string payload = reply.BodyAsString();
144144
Console.WriteLine($"RPC Client: Sent ping request ({requestId})");
145145
Console.WriteLine($"RPC Client: Received reply - {payload}");
146146
}

dotnet-amqp/Worker/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using RabbitMQ.AMQP.Client;
1+
using RabbitMQ.AMQP.Client;
32
using RabbitMQ.AMQP.Client.Impl;
43

54
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
@@ -24,7 +23,7 @@
2423
.InitialCredits(1)
2524
.MessageHandler((ctx, message) =>
2625
{
27-
string body = Encoding.UTF8.GetString(message.Body()!);
26+
string body = message.BodyAsString();
2827
Console.WriteLine($" [x] Received '{body}'");
2928
try
3029
{

0 commit comments

Comments
 (0)