Skip to content

Commit

Permalink
Upgrade chat room orleans sample to dotnet 9 (#7014)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
egil and IEvangelist authored Jan 21, 2025
1 parent 4c61fc9 commit 8812ba7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 39 deletions.
13 changes: 7 additions & 6 deletions orleans/ChatRoom/ChatRoom.Client/ChatRoom.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>
</PropertyGroup>

<ItemGroup>
<None Remove="logo.png" />
Expand All @@ -17,8 +16,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.48.0" />
<PackageReference Include="Microsoft.Orleans.Client" Version="9.0.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions orleans/ChatRoom/ChatRoom.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using ChatRoom;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Orleans.Runtime;
using Spectre.Console;

using var host = new HostBuilder()
.UseOrleansClient(clientBuilder =>
{
clientBuilder.UseLocalhostClustering()
clientBuilder
.UseLocalhostClustering()
.AddMemoryStreams("chat");
})
.Build();
Expand Down
6 changes: 3 additions & 3 deletions orleans/ChatRoom/ChatRoom.Common/ChatMsg.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace ChatRoom;
namespace ChatRoom;

[GenerateSerializer]
[GenerateSerializer, Immutable]
public record class ChatMsg(
string? Author,
string Text)
{
[Id(0)]
public string Author { get; init; } = Author ?? "Alexey";
public string Author { get; init; } = Author ?? "Anonymous";

[Id(1)]
public DateTimeOffset Created { get; init; } = DateTimeOffset.Now;
Expand Down
7 changes: 4 additions & 3 deletions orleans/ChatRoom/ChatRoom.Common/ChatRoom.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ChatRoomt</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="8.0.0" />
<PackageReference Include="Microsoft.Orleans.Streaming" Version="8.0.0" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
<PackageReference Include="Microsoft.Orleans.Streaming" Version="9.0.1" />
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions orleans/ChatRoom/ChatRoom.Common/IChannelGrain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Orleans.Runtime;

namespace ChatRoom;
namespace ChatRoom;

public interface IChannelGrain : IGrainWithStringKey
{
Expand Down
32 changes: 18 additions & 14 deletions orleans/ChatRoom/ChatRoom.Service/ChannelGrain.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using Orleans.Runtime;
using Orleans.Streams;
using Orleans.Streams;

namespace ChatRoom;

public class ChannelGrain : Grain, IChannelGrain
public sealed class ChannelGrain : Grain, IChannelGrain
{
private readonly List<ChatMsg> _messages = new(100);
private readonly List<string> _onlineMembers = new(10);
private readonly List<ChatMsg> _messages = [];
private readonly List<string> _onlineMembers = [];

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

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

var streamId = StreamId.Create(
"ChatRoom", this.GetPrimaryKeyString());
var streamId = StreamId.Create("ChatRoom", this.GetPrimaryKeyString());

_stream = streamProvider.GetStream<ChatMsg>(
streamId);
_stream = streamProvider.GetStream<ChatMsg>(streamId);

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

await _stream.OnNextAsync(
new ChatMsg(
"System",
$"{nickname} joins the chat '{this.GetPrimaryKeyString()}' ..."));
Author: "System",
Text: $"{nickname} joins the chat '{this.GetPrimaryKeyString()}' ..."));

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

await _stream.OnNextAsync(
new ChatMsg(
"System",
$"{nickname} leaves the chat..."));
Author: "System",
Text: $"{nickname} leaves the chat..."));

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

if (_messages.Count > 100)
{
_messages.RemoveAt(0);
}

await _stream.OnNextAsync(msg);

return true;
Expand Down
6 changes: 3 additions & 3 deletions orleans/ChatRoom/ChatRoom.Service/ChatRoom.Service.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Server" Version="8.0.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="9.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 6 additions & 4 deletions orleans/ChatRoom/ChatRoom.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

await Host.CreateDefaultBuilder(args)
.UseOrleans(siloBuilder =>
{
siloBuilder
.UseLocalhostClustering()
siloBuilder.UseLocalhostClustering()
.AddMemoryGrainStorage("PubSubStore")
.AddMemoryStreams("chat");
.AddMemoryStreams("chat")
.ConfigureLogging(logging => logging.AddConsole());
})
.RunConsoleAsync();

15 changes: 15 additions & 0 deletions orleans/ChatRoom/ChatRoom.slnLaunch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"Name": "Run client and service",
"Projects": [
{
"Path": "ChatRoom.Client\\ChatRoom.Client.csproj",
"Action": "Start"
},
{
"Path": "ChatRoom.Service\\ChatRoom.Service.csproj",
"Action": "Start"
}
]
}
]
2 changes: 1 addition & 1 deletion orleans/ChatRoom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Each chat channel has a corresponding `ChannelGrain` which is identified by the

## Sample prerequisites

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

## Building the sample

Expand Down

0 comments on commit 8812ba7

Please sign in to comment.