Skip to content

Commit caba7b3

Browse files
committed
Add dotnet-amqp AMQP 1.0 tutorial port
Introduce C# examples using RabbitMQ.AMQP.Client (solution, README, and test-tutorials.sh smoke script) aligned with other AMQP 1.0 tutorial ports.
1 parent b3fc942 commit caba7b3

33 files changed

Lines changed: 1789 additions & 0 deletions

dotnet-amqp/.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Set the default behavior, in case people don't have core.autocrlf set.
2+
* text=auto
3+
4+
# Auto detect text files and perform LF normalization
5+
*.cs text=auto eol=lf
6+
*.txt text=auto
7+
8+
# Declare files that will always have CRLF line endings on checkout.
9+
*.sln text eol=crlf
10+
*.csproj text eol=crlf
11+
12+
# Custom for Visual Studio
13+
*.cs diff=csharp

dotnet-amqp/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.dll
2+
*.exe
3+
*.lock.json
4+
packages/
5+
bin/
6+
obj/

dotnet-amqp/EmitLog/EmitLog.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" />
12+
</ItemGroup>
13+
14+
</Project>

dotnet-amqp/EmitLog/Program.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string exchangeName = "logs";
7+
8+
string message = args.Length < 1 ? "info: Hello World!" : string.Join(" ", args);
9+
10+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
11+
.Uri(new Uri(brokerUri))
12+
.ContainerId("tutorial-emitlog")
13+
.Build();
14+
15+
IEnvironment environment = AmqpEnvironment.Create(settings);
16+
IConnection connection = await environment.CreateConnectionAsync();
17+
18+
try
19+
{
20+
IManagement management = connection.Management();
21+
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("fanout");
22+
await exchangeSpec.DeclareAsync();
23+
24+
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).BuildAsync();
25+
try
26+
{
27+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
28+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
29+
if (pr.Outcome.State != OutcomeState.Accepted)
30+
{
31+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
32+
Environment.Exit(1);
33+
}
34+
35+
Console.WriteLine($" [x] Sent '{message}'");
36+
}
37+
finally
38+
{
39+
await publisher.CloseAsync();
40+
}
41+
}
42+
finally
43+
{
44+
await connection.CloseAsync();
45+
await environment.CloseAsync();
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string exchangeName = "logs_direct";
7+
8+
string severity = GetSeverity(args);
9+
string message = GetMessage(args);
10+
11+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
12+
.Uri(new Uri(brokerUri))
13+
.ContainerId("tutorial-emitlogdirect")
14+
.Build();
15+
16+
IEnvironment environment = AmqpEnvironment.Create(settings);
17+
IConnection connection = await environment.CreateConnectionAsync();
18+
19+
try
20+
{
21+
IManagement management = connection.Management();
22+
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("direct");
23+
await exchangeSpec.DeclareAsync();
24+
25+
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(severity).BuildAsync();
26+
try
27+
{
28+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
29+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
30+
if (pr.Outcome.State != OutcomeState.Accepted)
31+
{
32+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
33+
Environment.Exit(1);
34+
}
35+
36+
Console.WriteLine($" [x] Sent '{severity}':'{message}'");
37+
}
38+
finally
39+
{
40+
await publisher.CloseAsync();
41+
}
42+
}
43+
finally
44+
{
45+
await connection.CloseAsync();
46+
await environment.CloseAsync();
47+
}
48+
49+
static string GetSeverity(string[] strings) => strings.Length < 1 ? "info" : strings[0];
50+
51+
static string GetMessage(string[] strings)
52+
{
53+
if (strings.Length < 2)
54+
{
55+
return "Hello World!";
56+
}
57+
58+
return string.Join(" ", strings.Skip(1));
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string exchangeName = "logs_topic";
7+
8+
string routingKey = GetRouting(args);
9+
string message = GetMessage(args);
10+
11+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
12+
.Uri(new Uri(brokerUri))
13+
.ContainerId("tutorial-emitlogtopic")
14+
.Build();
15+
16+
IEnvironment environment = AmqpEnvironment.Create(settings);
17+
IConnection connection = await environment.CreateConnectionAsync();
18+
19+
try
20+
{
21+
IManagement management = connection.Management();
22+
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("topic");
23+
await exchangeSpec.DeclareAsync();
24+
25+
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(routingKey).BuildAsync();
26+
try
27+
{
28+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
29+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
30+
if (pr.Outcome.State != OutcomeState.Accepted)
31+
{
32+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
33+
Environment.Exit(1);
34+
}
35+
36+
Console.WriteLine($" [x] Sent '{routingKey}':'{message}'");
37+
}
38+
finally
39+
{
40+
await publisher.CloseAsync();
41+
}
42+
}
43+
finally
44+
{
45+
await connection.CloseAsync();
46+
await environment.CloseAsync();
47+
}
48+
49+
static string GetRouting(string[] strings) => strings.Length < 1 ? "anonymous.info" : strings[0];
50+
51+
static string GetMessage(string[] strings)
52+
{
53+
if (strings.Length < 2)
54+
{
55+
return "Hello World!";
56+
}
57+
58+
return string.Join(" ", strings.Skip(1));
59+
}

dotnet-amqp/NewTask/NewTask.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" />
12+
</ItemGroup>
13+
14+
</Project>

dotnet-amqp/NewTask/Program.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string taskQueueName = "task_queue";
7+
8+
string message = args.Length > 0 ? string.Join(" ", args) : "Hello World!";
9+
10+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
11+
.Uri(new Uri(brokerUri))
12+
.ContainerId("tutorial-newtask")
13+
.Build();
14+
15+
IEnvironment environment = AmqpEnvironment.Create(settings);
16+
IConnection connection = await environment.CreateConnectionAsync();
17+
18+
try
19+
{
20+
IManagement management = connection.Management();
21+
IQueueSpecification queueSpec = management.Queue(taskQueueName).Type(QueueType.QUORUM);
22+
await queueSpec.DeclareAsync();
23+
24+
IPublisher publisher = await connection.PublisherBuilder().Queue(taskQueueName).BuildAsync();
25+
try
26+
{
27+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
28+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
29+
if (pr.Outcome.State != OutcomeState.Accepted)
30+
{
31+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
32+
Environment.Exit(1);
33+
}
34+
35+
Console.WriteLine($" [x] Sent '{message}'");
36+
}
37+
finally
38+
{
39+
await publisher.CloseAsync();
40+
}
41+
}
42+
finally
43+
{
44+
await connection.CloseAsync();
45+
await environment.CloseAsync();
46+
}

0 commit comments

Comments
 (0)