Skip to content

Commit 145a76f

Browse files
authored
Update databus/redis sample to NServiceBus 10 (#7513)
1 parent 5b3656c commit 145a76f

File tree

12 files changed

+279
-0
lines changed

12 files changed

+279
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.35201.131
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sender", "Sender\Sender.csproj", "{A2C85229-1A52-4D3B-90FF-EF9D93EE6216}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Receiver", "Receiver\Receiver.csproj", "{D295B34A-6424-4204-B007-971EE333D991}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{3282C6A4-9A1E-401D-BC87-71ECE1241A8E}"
11+
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DCDF2CA8-92D1-46E2-AFE2-4A39D6605557}"
13+
ProjectSection(SolutionItems) = preProject
14+
docker-compose.yml = docker-compose.yml
15+
EndProjectSection
16+
EndProject
17+
Global
18+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
19+
Debug|Any CPU = Debug|Any CPU
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{A2C85229-1A52-4D3B-90FF-EF9D93EE6216}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{A2C85229-1A52-4D3B-90FF-EF9D93EE6216}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{D295B34A-6424-4204-B007-971EE333D991}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{D295B34A-6424-4204-B007-971EE333D991}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{3282C6A4-9A1E-401D-BC87-71ECE1241A8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{3282C6A4-9A1E-401D-BC87-71ECE1241A8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
EndGlobalSection
29+
GlobalSection(SolutionProperties) = preSolution
30+
HideSolutionNode = FALSE
31+
EndGlobalSection
32+
GlobalSection(ExtensibilityGlobals) = postSolution
33+
SolutionGuid = {7B1C3D38-9A43-477C-8298-A9EAA9FB17CE}
34+
EndGlobalSection
35+
EndGlobal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Logging;
5+
using NServiceBus;
6+
using Shared.Messages;
7+
8+
class ProcessTextHandler(ILogger<ProcessTextHandler> log) : IHandleMessages<ProcessText>
9+
{
10+
#region process-message
11+
public Task Handle(ProcessText message, IMessageHandlerContext context)
12+
{
13+
log.LogInformation(
14+
"Most common letter in sample: {mostCommonLetter}",
15+
MostCommonLetter(message.LargeText)
16+
);
17+
18+
return Task.CompletedTask;
19+
}
20+
#endregion
21+
22+
private static char MostCommonLetter(string text) => text.GroupBy(c => c).OrderByDescending(c => c.Count()).Select(c => c.Key).First();
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
using NServiceBus.ClaimCheck;
6+
using Shared;
7+
8+
class Program
9+
{
10+
static async Task Main(string[] args)
11+
{
12+
Console.Title = "Receiver";
13+
14+
var builder = Host.CreateApplicationBuilder();
15+
16+
var endpointConfig = new EndpointConfiguration("Receiver");
17+
endpointConfig.UseTransport(new LearningTransport());
18+
endpointConfig.UseSerialization<SystemJsonSerializer>();
19+
endpointConfig.UseClaimCheck(_ => new RedisClaimCheck("localhost"), new SystemJsonClaimCheckSerializer());
20+
endpointConfig.Conventions().DefiningClaimCheckPropertiesAs(prop => prop.Name.StartsWith("Large"));
21+
22+
builder.UseNServiceBus(endpointConfig);
23+
24+
var host = builder.Build();
25+
26+
await host.RunAsync();
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<LangVersion>preview</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
12+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Shared\Shared.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using NServiceBus;
6+
using NServiceBus.ClaimCheck;
7+
using Shared;
8+
using Shared.Messages;
9+
10+
class Program
11+
{
12+
static async Task Main(string[] args)
13+
{
14+
Console.Title = "Sender";
15+
16+
var builder = Host.CreateApplicationBuilder();
17+
18+
var endpointConfig = new EndpointConfiguration("Sender");
19+
var transport = endpointConfig.UseTransport(new LearningTransport());
20+
transport.RouteToEndpoint(typeof(ProcessText), "Receiver");
21+
endpointConfig.UseSerialization<SystemJsonSerializer>();
22+
23+
#region configure-claim-check
24+
endpointConfig.UseClaimCheck(
25+
_ => new RedisClaimCheck("localhost"),
26+
new SystemJsonClaimCheckSerializer()
27+
);
28+
endpointConfig.Conventions()
29+
.DefiningClaimCheckPropertiesAs(
30+
prop => prop.Name.StartsWith("Large")
31+
);
32+
#endregion
33+
34+
builder.UseNServiceBus(endpointConfig);
35+
36+
builder.Services.AddHostedService<TextSenderHostedService>();
37+
38+
var host = builder.Build();
39+
40+
await host.RunAsync();
41+
}
42+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<LangVersion>preview</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
12+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Shared\Shared.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
6+
using NServiceBus;
7+
using Shared.Messages;
8+
9+
class TextSenderHostedService(IMessageSession messageSession, ILogger<TextSenderHostedService> log) : BackgroundService
10+
{
11+
const int Megabyte = 1024 * 1024;
12+
13+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
14+
{
15+
#region send-message
16+
await messageSession.Send(new ProcessText
17+
{
18+
LargeText = GetRandomText(1 * Megabyte)
19+
}, cancellationToken: stoppingToken);
20+
#endregion
21+
22+
log.LogInformation("Sent message with 1MB of random text");
23+
}
24+
25+
static string GetRandomText(int length)
26+
{
27+
return string.Create(length, length, (chars, state) =>
28+
{
29+
var random = new Random();
30+
for(var i = 0; i < state; i++)
31+
{
32+
chars[i] = (char)random.Next('a', 'z');
33+
}
34+
});
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using NServiceBus;
2+
3+
namespace Shared.Messages;
4+
5+
public class ProcessText : ICommand
6+
{
7+
public string LargeText { get; set; }
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Shared;
2+
3+
using System;
4+
using System.IO;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using NServiceBus.ClaimCheck;
8+
using StackExchange.Redis;
9+
10+
#region claim-check
11+
public class RedisClaimCheck(string configuration) : IClaimCheck
12+
{
13+
private IConnectionMultiplexer redis;
14+
15+
public async Task<Stream> Get(
16+
string key,
17+
CancellationToken cancellationToken = default)
18+
{
19+
var db = redis.GetDatabase();
20+
byte[] redisValue = await db.StringGetAsync(key);
21+
return new MemoryStream(redisValue);
22+
}
23+
24+
public async Task<string> Put(
25+
Stream stream,
26+
TimeSpan timeToBeReceived,
27+
CancellationToken cancellationToken = default)
28+
{
29+
var db = redis.GetDatabase();
30+
var key = $"particular:sample:claimcheck:{Guid.NewGuid()}";
31+
var value = ((MemoryStream)stream).ToArray();
32+
await db.StringSetAsync(
33+
key,
34+
value,
35+
timeToBeReceived == TimeSpan.MaxValue ? null : timeToBeReceived
36+
);
37+
return key;
38+
}
39+
40+
public async Task Start(CancellationToken cancellationToken = default)
41+
{
42+
redis = await ConnectionMultiplexer.ConnectAsync(configuration);
43+
}
44+
}
45+
#endregion
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="StackExchange.Redis" Version="2.*" />
10+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
12+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)