Skip to content

Commit a8197d7

Browse files
authored
Update file-share-databus sample to NServiceBus 10 (#7512)
1 parent 0885abd commit a8197d7

File tree

12 files changed

+256
-1
lines changed

12 files changed

+256
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29728.190
5+
MinimumVisualStudioVersion = 15.0.26730.12
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sender", "Sender\Sender.csproj", "{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Receiver", "Receiver\Receiver.csproj", "{6987F415-B4D5-4380-ADED-EED5AF170608}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{6987F415-B4D5-4380-ADED-EED5AF170608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{6987F415-B4D5-4380-ADED-EED5AF170608}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
using NServiceBus.Logging;
4+
5+
#region MessageWithLargePayloadHandler
6+
7+
public class MessageWithLargePayloadHandler :
8+
IHandleMessages<MessageWithLargePayload>
9+
{
10+
static ILog log = LogManager.GetLogger<MessageWithLargePayloadHandler>();
11+
12+
public Task Handle(MessageWithLargePayload message, IMessageHandlerContext context)
13+
{
14+
log.Info($"Message received, size of blob property: {message.LargeBlob.Value.Length} Bytes");
15+
return Task.CompletedTask;
16+
}
17+
}
18+
19+
#endregion
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Shared;
2+
3+
Console.Title = "Receiver";
4+
5+
var endpointConfiguration = new EndpointConfiguration("Samples.ClaimCheck.Receiver");
6+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
7+
endpointConfiguration.UseTransport(new LearningTransport());
8+
9+
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, SystemJsonClaimCheckSerializer>();
10+
var storagePath = new SolutionDirectoryFinder().GetDirectory("storage");
11+
claimCheck.BasePath(storagePath);
12+
13+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
14+
15+
Console.WriteLine("Press any key to exit");
16+
Console.ReadKey();
17+
18+
await endpointInstance.Stop();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
10+
<ProjectReference Include="..\Shared\Shared.csproj" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using NServiceBus;
4+
using NServiceBus.ClaimCheck;
5+
using Shared;
6+
7+
8+
Console.Title = "Sender";
9+
10+
var builder = Host.CreateApplicationBuilder(args);
11+
12+
var endpointConfiguration = new EndpointConfiguration("Samples.ClaimCheck.Sender");
13+
var storagePath = new SolutionDirectoryFinder().GetDirectory("storage");
14+
15+
#region ConfigureDataBus
16+
17+
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, SystemJsonClaimCheckSerializer>();
18+
claimCheck.BasePath(storagePath);
19+
20+
#endregion
21+
22+
endpointConfiguration.UsePersistence<LearningPersistence>();
23+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
24+
endpointConfiguration.UseTransport(new LearningTransport());
25+
26+
builder.UseNServiceBus(endpointConfiguration);
27+
28+
var host = builder.Build();
29+
30+
await host.StartAsync();
31+
32+
var messageSession = host.Services.GetService<IMessageSession>();
33+
34+
Console.WriteLine("Press 'D' to send a claim check large message");
35+
Console.WriteLine("Press 'N' to send a normal large message exceed the size limit and throw");
36+
Console.WriteLine("Press any other key to exit");
37+
38+
while (true)
39+
{
40+
var key = Console.ReadKey();
41+
Console.WriteLine();
42+
43+
if (key.Key == ConsoleKey.N)
44+
{
45+
await SendMessageTooLargePayload(messageSession);
46+
continue;
47+
}
48+
49+
if (key.Key == ConsoleKey.D)
50+
{
51+
await SendMessageLargePayload(messageSession, storagePath);
52+
continue;
53+
}
54+
55+
break;
56+
}
57+
58+
await host.StopAsync();
59+
60+
61+
static async Task SendMessageLargePayload(IMessageSession messageSession, string storagePath)
62+
{
63+
#region SendMessageLargePayload
64+
65+
var message = new MessageWithLargePayload
66+
{
67+
SomeProperty = "This message contains a large blob that will be sent via claim check",
68+
LargeBlob = new ClaimCheckProperty<byte[]>(new byte[1024 * 1024 * 5]) //5MB
69+
70+
};
71+
await messageSession.Send("Samples.ClaimCheck.Receiver", message);
72+
73+
#endregion
74+
75+
Console.WriteLine($"Message sent, the payload is stored in: {storagePath}");
76+
}
77+
78+
static async Task SendMessageTooLargePayload(IMessageSession messageSession)
79+
{
80+
#region SendMessageTooLargePayload
81+
82+
var message = new AnotherMessageWithLargePayload
83+
{
84+
LargeBlob = new byte[1024 * 1024 * 5] //5MB
85+
};
86+
await messageSession.Send("Samples.ClaimCheck.Receiver", message);
87+
88+
#endregion
89+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
11+
<ProjectReference Include="..\Shared\Shared.csproj" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NServiceBus;
2+
3+
#region AnotherMessageWithLargePayload
4+
5+
public class AnotherMessageWithLargePayload :
6+
ICommand
7+
{
8+
public byte[] LargeBlob { get; set; }
9+
}
10+
11+
#endregion
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using NServiceBus;
2+
3+
#region MessageWithLargePayload
4+
5+
//the claim check is allowed to clean up transmitted properties older than the TTBR
6+
[TimeToBeReceived("00:01:00")]
7+
public class MessageWithLargePayload :
8+
ICommand
9+
{
10+
public string SomeProperty { get; set; }
11+
public ClaimCheckProperty<byte[]> LargeBlob { get; set; }
12+
}
13+
14+
#endregion
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<LangVersion>preview</LangVersion>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
9+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace Shared
6+
{
7+
public class SolutionDirectoryFinder
8+
{
9+
public string Root { get; }
10+
11+
public SolutionDirectoryFinder(string root) => Root = root;
12+
13+
public SolutionDirectoryFinder()
14+
{
15+
var directory = AppContext.BaseDirectory;
16+
17+
while (true)
18+
{
19+
if (Directory.EnumerateFiles(directory).Any(file => file.EndsWith(".sln")))
20+
{
21+
Root = directory;
22+
return;
23+
}
24+
25+
var parent = Directory.GetParent(directory) ?? throw new Exception(
26+
"Couldn't find the solution directory for the ClaimCheck storage. " +
27+
"If the endpoint is outside the solution folder structure, " +
28+
"make sure to specify a storage directory using an absolute path.");
29+
30+
directory = parent.FullName;
31+
}
32+
}
33+
34+
public string GetDirectory(string relativePath)
35+
{
36+
var fullPath = Path.GetFullPath(Path.Combine(Root, relativePath));
37+
Directory.CreateDirectory(fullPath);
38+
return fullPath;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)