Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8812ba7

Browse files
egilIEvangelist
andauthored
Upgrade chat room orleans sample to dotnet 9 (#7014)
* Upgrade ChatRoom orleans sample to dotnet 9 * Set default name to Anonymous if not specified * Remove unused using * Ensure chatroom message history doesn't grow above 100 * Update orleans/ChatRoom/ChatRoom.Service/ChannelGrain.cs --------- Co-authored-by: David Pine <[email protected]>
1 parent 4c61fc9 commit 8812ba7

File tree

10 files changed

+60
-39
lines changed

10 files changed

+60
-39
lines changed

orleans/ChatRoom/ChatRoom.Client/ChatRoom.Client.csproj

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<OutputType>Exe</OutputType>
8-
<ServerGarbageCollection>true</ServerGarbageCollection>
9-
</PropertyGroup>
8+
</PropertyGroup>
109

1110
<ItemGroup>
1211
<None Remove="logo.png" />
@@ -17,8 +16,10 @@
1716
</ItemGroup>
1817

1918
<ItemGroup>
20-
<PackageReference Include="Spectre.Console" Version="0.48.0" />
21-
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.48.0" />
19+
<PackageReference Include="Microsoft.Orleans.Client" Version="9.0.1" />
20+
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
21+
<PackageReference Include="Spectre.Console" Version="0.49.1" />
22+
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.49.1" />
2223
</ItemGroup>
2324

2425
<ItemGroup>

orleans/ChatRoom/ChatRoom.Client/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
using ChatRoom;
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Hosting;
5-
using Orleans.Runtime;
65
using Spectre.Console;
76

87
using var host = new HostBuilder()
98
.UseOrleansClient(clientBuilder =>
109
{
11-
clientBuilder.UseLocalhostClustering()
10+
clientBuilder
11+
.UseLocalhostClustering()
1212
.AddMemoryStreams("chat");
1313
})
1414
.Build();

orleans/ChatRoom/ChatRoom.Common/ChatMsg.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
namespace ChatRoom;
1+
namespace ChatRoom;
22

3-
[GenerateSerializer]
3+
[GenerateSerializer, Immutable]
44
public record class ChatMsg(
55
string? Author,
66
string Text)
77
{
88
[Id(0)]
9-
public string Author { get; init; } = Author ?? "Alexey";
9+
public string Author { get; init; } = Author ?? "Anonymous";
1010

1111
[Id(1)]
1212
public DateTimeOffset Created { get; init; } = DateTimeOffset.Now;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<RootNamespace>ChatRoomt</RootNamespace>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<PackageReference Include="Microsoft.Orleans.Sdk" Version="8.0.0" />
11-
<PackageReference Include="Microsoft.Orleans.Streaming" Version="8.0.0" />
11+
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
12+
<PackageReference Include="Microsoft.Orleans.Streaming" Version="9.0.1" />
1213
</ItemGroup>
1314

1415
</Project>

orleans/ChatRoom/ChatRoom.Common/IChannelGrain.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Orleans.Runtime;
2-
3-
namespace ChatRoom;
1+
namespace ChatRoom;
42

53
public interface IChannelGrain : IGrainWithStringKey
64
{

orleans/ChatRoom/ChatRoom.Service/ChannelGrain.cs

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
using Orleans.Runtime;
2-
using Orleans.Streams;
1+
using Orleans.Streams;
32

43
namespace ChatRoom;
54

6-
public class ChannelGrain : Grain, IChannelGrain
5+
public sealed class ChannelGrain : Grain, IChannelGrain
76
{
8-
private readonly List<ChatMsg> _messages = new(100);
9-
private readonly List<string> _onlineMembers = new(10);
7+
private readonly List<ChatMsg> _messages = [];
8+
private readonly List<string> _onlineMembers = [];
109

11-
private IAsyncStream<ChatMsg> _stream = null!;
10+
// Initialized in OnActivateAsync that runs before
11+
// other methods that uses _stream field can be invoked.
12+
private IAsyncStream<ChatMsg> _stream = default!;
1213

1314
public override Task OnActivateAsync(CancellationToken cancellationToken)
1415
{
1516
var streamProvider = this.GetStreamProvider("chat");
1617

17-
var streamId = StreamId.Create(
18-
"ChatRoom", this.GetPrimaryKeyString());
18+
var streamId = StreamId.Create("ChatRoom", this.GetPrimaryKeyString());
1919

20-
_stream = streamProvider.GetStream<ChatMsg>(
21-
streamId);
20+
_stream = streamProvider.GetStream<ChatMsg>(streamId);
2221

2322
return base.OnActivateAsync(cancellationToken);
2423
}
@@ -29,8 +28,8 @@ public async Task<StreamId> Join(string nickname)
2928

3029
await _stream.OnNextAsync(
3130
new ChatMsg(
32-
"System",
33-
$"{nickname} joins the chat '{this.GetPrimaryKeyString()}' ..."));
31+
Author: "System",
32+
Text: $"{nickname} joins the chat '{this.GetPrimaryKeyString()}' ..."));
3433

3534
return _stream.StreamId;
3635
}
@@ -41,8 +40,8 @@ public async Task<StreamId> Leave(string nickname)
4140

4241
await _stream.OnNextAsync(
4342
new ChatMsg(
44-
"System",
45-
$"{nickname} leaves the chat..."));
43+
Author: "System",
44+
Text: $"{nickname} leaves the chat..."));
4645

4746
return _stream.StreamId;
4847
}
@@ -51,6 +50,11 @@ public async Task<bool> Message(ChatMsg msg)
5150
{
5251
_messages.Add(msg);
5352

53+
if (_messages.Count > 100)
54+
{
55+
_messages.RemoveAt(0);
56+
}
57+
5458
await _stream.OnNextAsync(msg);
5559

5660
return true;

orleans/ChatRoom/ChatRoom.Service/ChatRoom.Service.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<OutputType>Exe</OutputType>
88
<ServerGarbageCollection>true</ServerGarbageCollection>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.Orleans.Server" Version="8.0.0" />
12+
<PackageReference Include="Microsoft.Orleans.Server" Version="9.0.1" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using Microsoft.Extensions.Hosting;
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.Logging;
23

34
await Host.CreateDefaultBuilder(args)
45
.UseOrleans(siloBuilder =>
56
{
6-
siloBuilder
7-
.UseLocalhostClustering()
7+
siloBuilder.UseLocalhostClustering()
88
.AddMemoryGrainStorage("PubSubStore")
9-
.AddMemoryStreams("chat");
9+
.AddMemoryStreams("chat")
10+
.ConfigureLogging(logging => logging.AddConsole());
1011
})
1112
.RunConsoleAsync();
13+

orleans/ChatRoom/ChatRoom.slnLaunch

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{
3+
"Name": "Run client and service",
4+
"Projects": [
5+
{
6+
"Path": "ChatRoom.Client\\ChatRoom.Client.csproj",
7+
"Action": "Start"
8+
},
9+
{
10+
"Path": "ChatRoom.Service\\ChatRoom.Service.csproj",
11+
"Action": "Start"
12+
}
13+
]
14+
}
15+
]

orleans/ChatRoom/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Each chat channel has a corresponding `ChannelGrain` which is identified by the
2626

2727
## Sample prerequisites
2828

29-
This sample is written in C# and targets .NET 8.0. It requires the [.NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) or later.
29+
This sample is written in C# and targets .NET 9.0. It requires the [.NET 9.0 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) or later.
3030

3131
## Building the sample
3232

0 commit comments

Comments
 (0)