diff --git a/dotnet-stream/.gitignore b/dotnet-stream/.gitignore new file mode 100644 index 0000000..7c5f7a3 --- /dev/null +++ b/dotnet-stream/.gitignore @@ -0,0 +1,113 @@ +## Misc files +*.bak +.DS_Store +.idea +InternalTrace* +[Ll]ocal.dist +[Ll]ocal.props +*.lock.json +nunit-agent* +*.pyc +*.VisualState.xml +.vscode + +## Misc directories +.fake/ +gensrc/ +.ionide/ +NuGet/ +tmp/ +.vscode/ + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ +*.lock.json + +BenchmarkDotNet.Artifacts/* + +APIApproval.Approve.received.txt + +# Visual Studio 2015 cache/options directory +.vs/ + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# NuGet Packages Directory +packages/ +/packages + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# Unit tests +projects/Unit*/TestResult.xml + +# Development scripts +*.pcap + +# Vim +.sw? +.*.sw? diff --git a/dotnet-stream/README.md b/dotnet-stream/README.md new file mode 100644 index 0000000..900f411 --- /dev/null +++ b/dotnet-stream/README.md @@ -0,0 +1,29 @@ +# Dotnet Stream C# code for RabbitMQ tutorials + +Here you can find the C# code examples for [RabbitMQ +tutorials](https://www.rabbitmq.com/getstarted.html) using .NET 8.0. + +To successfully use the examples you will need a running RabbitMQ server with the [stream plugin enabled](https://www.rabbitmq.com/docs/stream#enabling-plugin). + +## Requirements + +### Requirements on Windows + +* [dotnet core](https://www.microsoft.com/net/core) + +We're using the command line (start->run cmd.exe) to +compile and run the code. + +### Requirements on Linux + +* [dotnet core](https://www.microsoft.com/net/core) + +### Code + +Each command is best run in a separate console/terminal instance run from the root +of the tutorial directory. + +#### [Tutorial one: "Hello World!"](https://www.rabbitmq.com/tutorials/tutorial-one-dotnet-stream.html) + + dotnet run --project Receive/Receive.csproj + dotnet run --project Send/Send.csproj diff --git a/dotnet-stream/Receive/Receive.cs b/dotnet-stream/Receive/Receive.cs new file mode 100644 index 0000000..b65a75d --- /dev/null +++ b/dotnet-stream/Receive/Receive.cs @@ -0,0 +1,30 @@ +// See https://aka.ms/new-console-template for more information + +using System.Text; +using RabbitMQ.Stream.Client; +using RabbitMQ.Stream.Client.Reliable; + +var streamSystem = await StreamSystem.Create(new StreamSystemConfig()); + +await streamSystem.CreateStream(new StreamSpec("hello-stream") +{ + MaxLengthBytes = 5_000_000_000 +}); + + +var consumer = await Consumer.Create(new ConsumerConfig(streamSystem, "hello-stream") +{ + OffsetSpec = new OffsetTypeFirst(), + MessageHandler = async (stream, _, _, message) => + { + Console.WriteLine($"Stream: {stream} - " + + $"Received message: {Encoding.UTF8.GetString(message.Data.Contents)}"); + await Task.CompletedTask; + } +}); + +Console.WriteLine(" [x] Press any key to exit"); +Console.ReadKey(); + +await consumer.Close(); +await streamSystem.Close(); \ No newline at end of file diff --git a/dotnet-stream/Receive/Receive.csproj b/dotnet-stream/Receive/Receive.csproj new file mode 100644 index 0000000..cdeff19 --- /dev/null +++ b/dotnet-stream/Receive/Receive.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + ReceiveHello + + + + + + + diff --git a/dotnet-stream/Send/Send.csproj b/dotnet-stream/Send/Send.csproj new file mode 100644 index 0000000..f1c5fe9 --- /dev/null +++ b/dotnet-stream/Send/Send.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + SendHello + + + + + + + diff --git a/dotnet-stream/Send/Sends.cs b/dotnet-stream/Send/Sends.cs new file mode 100644 index 0000000..55617e3 --- /dev/null +++ b/dotnet-stream/Send/Sends.cs @@ -0,0 +1,21 @@ +using System.Text; +using RabbitMQ.Stream.Client; +using RabbitMQ.Stream.Client.Reliable; + +var streamSystem = await StreamSystem.Create(new StreamSystemConfig()); + +await streamSystem.CreateStream(new StreamSpec("hello-stream") +{ + MaxLengthBytes = 5_000_000_000 +}); + +var producer = await Producer.Create(new ProducerConfig(streamSystem, "hello-stream")); + + +await producer.Send(new Message(Encoding.UTF8.GetBytes($"Hello, World"))); +Console.WriteLine(" [x] Sent 'Hello, World'"); + +Console.WriteLine(" [x] Press any key to exit"); +Console.ReadKey(); +await producer.Close(); +await streamSystem.Close(); \ No newline at end of file diff --git a/dotnet-stream/dotnet-stream.sln b/dotnet-stream/dotnet-stream.sln new file mode 100644 index 0000000..e1b0550 --- /dev/null +++ b/dotnet-stream/dotnet-stream.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Send", "Send\Send.csproj", "{C23444A1-BCB9-46A6-8D6D-7783C03E2A2E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Receive", "Receive\Receive.csproj", "{37F1D7F4-58B1-4A1B-BE76-178A9D7A91E8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C23444A1-BCB9-46A6-8D6D-7783C03E2A2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C23444A1-BCB9-46A6-8D6D-7783C03E2A2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C23444A1-BCB9-46A6-8D6D-7783C03E2A2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C23444A1-BCB9-46A6-8D6D-7783C03E2A2E}.Release|Any CPU.Build.0 = Release|Any CPU + {37F1D7F4-58B1-4A1B-BE76-178A9D7A91E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37F1D7F4-58B1-4A1B-BE76-178A9D7A91E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37F1D7F4-58B1-4A1B-BE76-178A9D7A91E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37F1D7F4-58B1-4A1B-BE76-178A9D7A91E8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal