Dotnet stream client tutorial getting started (#382)

* dotnet stream tutorial hello world

---------

Signed-off-by: Gabriele Santomaggio <g.santomaggio@gmail.com>
This commit is contained in:
Gabriele Santomaggio 2024-05-09 09:22:21 +02:00 committed by GitHub
parent c3170b0f7e
commit 0f18bb786e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 245 additions and 0 deletions

113
dotnet-stream/.gitignore vendored Normal file
View File

@ -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?

29
dotnet-stream/README.md Normal file
View File

@ -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

View File

@ -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();

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ReceiveHello</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RabbitMQ.Stream.Client" Version="1.8.3" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SendHello</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RabbitMQ.Stream.Client" Version="1.8.3" />
</ItemGroup>
</Project>

View File

@ -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();

View File

@ -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