Skip to content

Commit 1f4b9b3

Browse files
Update near realtime client sample to NServiceBus v10 (#7609)
* Remove the input loop service from the v9 sample * v10 version of the sample * Add pre-release marker file
1 parent b33748b commit 1f4b9b3

File tree

15 files changed

+293
-56
lines changed

15 files changed

+293
-56
lines changed
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+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<ProjectReference Include="..\StockEvents\StockEvents.csproj" />
9+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="10.0.0-preview.6.25358.103" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.SignalR.Client;
4+
5+
static class Program
6+
{
7+
static async Task Main()
8+
{
9+
Console.Title = "Client";
10+
11+
Console.WriteLine("Press any key to connect.");
12+
Console.ReadKey(true);
13+
14+
var hubConnection = new HubConnectionBuilder()
15+
.WithUrl("http://localhost:9756/StockTicksHub")
16+
.Build();
17+
18+
hubConnection.On<StockTick>("PushStockTick", stock => Console.WriteLine($"Stock update for {stock.Symbol} at {stock.Timestamp:O}. Press any key to exit."));
19+
20+
await hubConnection.StartAsync();
21+
22+
Console.WriteLine("Press any key to exit.");
23+
Console.ReadKey(true);
24+
25+
await hubConnection.StopAsync();
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<ImplicitUsings>true</ImplicitUsings>
6+
<LangVersion>preview</LangVersion>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\StockEvents\StockEvents.csproj" />
10+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.2" />
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.2" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using ClientHub;
2+
3+
Console.Title = "ClientHub";
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
var endpointConfiguration = new EndpointConfiguration("Samples.NearRealTimeClients.ClientHub");
8+
9+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
10+
endpointConfiguration.UseTransport(new LearningTransport());
11+
12+
endpointConfiguration.SendFailedMessagesTo("error");
13+
14+
var conventions = endpointConfiguration.Conventions();
15+
conventions.DefiningEventsAs(type => type == typeof(StockTick));
16+
17+
builder.UseNServiceBus(endpointConfiguration);
18+
19+
builder.Services.AddSignalR();
20+
21+
var app = builder.Build();
22+
app.MapHub<StockTicksHub>("/StockTicksHub");
23+
24+
const string url = "http://localhost:9756/";
25+
26+
var webAppTask = app.RunAsync(url);
27+
28+
Console.WriteLine($"SignalR server running at {url}");
29+
Console.WriteLine("NServiceBus subscriber running");
30+
Console.WriteLine("Press any key to exit");
31+
Console.ReadKey(true);
32+
33+
await Task.WhenAll(
34+
app.StopAsync(),
35+
webAppTask);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"ClientHub": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"environmentVariables": {
7+
"ASPNETCORE_ENVIRONMENT": "Development"
8+
},
9+
"applicationUrl": "https://localhost:63707;http://localhost:63708"
10+
}
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
namespace ClientHub;
3+
4+
5+
#region StockTickHandler
6+
7+
public class StockTickHandler(IHubContext<StockTicksHub> hub, ILogger<StockTickHandler> logger)
8+
: IHandleMessages<StockTick>
9+
{
10+
public Task Handle(StockTick message, IMessageHandlerContext context)
11+
{
12+
logger.LogInformation($"StockTick event received for Symbol {message.Symbol} with timestamp: {message.Timestamp:O}. Press any key to exit.");
13+
return hub.Clients.All.SendAsync("PushStockTick", message, context.CancellationToken);
14+
}
15+
}
16+
17+
#endregion
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace ClientHub;
4+
5+
#region StockTickHub
6+
7+
public class StockTicksHub : Hub<IEmitStockTicks>;
8+
9+
public interface IEmitStockTicks
10+
{
11+
Task PushStockTick(StockTick tick);
12+
}
13+
#endregion
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.29728.190
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Publisher", "Publisher\Publisher.csproj", "{32CD66D5-2733-4654-8012-EBAD45DDD246}"
6+
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{8831EEB6-C263-4285-9612-E2DA91FFF57C}"
8+
EndProject
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StockEvents", "StockEvents\StockEvents.csproj", "{1D96C0E6-C5B7-4077-96C3-36DABE107A5A}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientHub", "ClientHub\ClientHub.csproj", "{C64DB28D-0F8C-48EF-94F9-78347042EFE4}"
12+
EndProject
13+
Global
14+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
15+
Debug|Any CPU = Debug|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{32CD66D5-2733-4654-8012-EBAD45DDD246}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{32CD66D5-2733-4654-8012-EBAD45DDD246}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{8831EEB6-C263-4285-9612-E2DA91FFF57C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{8831EEB6-C263-4285-9612-E2DA91FFF57C}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{1D96C0E6-C5B7-4077-96C3-36DABE107A5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{1D96C0E6-C5B7-4077-96C3-36DABE107A5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{C64DB28D-0F8C-48EF-94F9-78347042EFE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{C64DB28D-0F8C-48EF-94F9-78347042EFE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
EndGlobalSection
27+
GlobalSection(SolutionProperties) = preSolution
28+
HideSolutionNode = FALSE
29+
EndGlobalSection
30+
EndGlobal
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using NServiceBus;
6+
7+
Console.Title = "Publisher";
8+
var builder = Host.CreateApplicationBuilder(args);
9+
var endpointConfiguration = new EndpointConfiguration("Samples.NearRealTimeClients.Publisher");
10+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
11+
endpointConfiguration.UseTransport(new LearningTransport());
12+
13+
endpointConfiguration.SendFailedMessagesTo("error");
14+
15+
#region MessageConventionsForNonNSB
16+
17+
var conventions = endpointConfiguration.Conventions();
18+
conventions.DefiningEventsAs(type => type == typeof(StockTick));
19+
20+
#endregion
21+
22+
endpointConfiguration.EnableInstallers();
23+
24+
Console.WriteLine("Press any key, the application is starting");
25+
Console.ReadKey();
26+
Console.WriteLine("Starting...");
27+
28+
builder.UseNServiceBus(endpointConfiguration);
29+
var host = builder.Build();
30+
31+
await host.StartAsync();
32+
33+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
34+
var rand = new Random();
35+
36+
Console.WriteLine("Press any key to start publishing");
37+
Console.ReadKey(true);
38+
39+
string[] symbols =
40+
[
41+
"MSFT",
42+
"AAPL",
43+
"GOOGL",
44+
"ORCL",
45+
"INTC",
46+
"HPQ",
47+
"CSCO"
48+
];
49+
50+
do
51+
{
52+
while (!Console.KeyAvailable)
53+
{
54+
var stockSymbol = symbols[rand.Next(0, symbols.Length - 1)];
55+
56+
var stockTick = new StockTick
57+
{
58+
Symbol = stockSymbol,
59+
Timestamp = DateTime.UtcNow
60+
};
61+
await messageSession.Publish(stockTick);
62+
63+
await Task.Delay(500);
64+
65+
Console.WriteLine($"Published StockTick Event with Symbol {stockSymbol}. Press escape to stop publishing events.");
66+
}
67+
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
68+
69+
await host.StopAsync();
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+
</PropertyGroup>
7+
<ItemGroup>
8+
<ProjectReference Include="..\StockEvents\StockEvents.csproj" />
9+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.2" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.2" />
11+
</ItemGroup>
12+
</Project>

0 commit comments

Comments
 (0)