Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading 'Hello World' sample #8116

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions samples/HelloWorld/HelloGrain.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Threading.Tasks;
using Orleans;
namespace HelloWorld;

namespace HelloWorld
public sealed class HelloGrain : Grain, IHelloGrain
{
public class HelloGrain : Grain, IHelloGrain
{
public Task<string> SayHello(string greeting) => Task.FromResult($"Hello, {greeting}!");
}
public ValueTask<string> SayHello(string greeting) =>
ValueTask.FromResult($"Hello, {greeting}!");
}
16 changes: 6 additions & 10 deletions samples/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Orleans.Core" Version="3.6.2" />
<PackageReference Include="Microsoft.Orleans.OrleansRuntime" Version="3.6.2" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="7.0.0" />
</ItemGroup>
</Project>
10 changes: 3 additions & 7 deletions samples/HelloWorld/IHelloGrain.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System.Threading.Tasks;
using Orleans;
namespace HelloWorld;

namespace HelloWorld
public interface IHelloGrain : IGrainWithStringKey
{
public interface IHelloGrain : IGrainWithStringKey
{
Task<string> SayHello(string greeting);
}
ValueTask<string> SayHello(string greeting);
}
11 changes: 6 additions & 5 deletions samples/HelloWorld/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using HelloWorld;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Orleans;
using Orleans.Hosting;

// Configure the host
using var host = new HostBuilder()
Expand All @@ -20,8 +17,12 @@
var friend = grainFactory.GetGrain<IHelloGrain>("friend");

// Call the grain and print the result to the console
var result = await friend.SayHello("Good morning!");
Console.WriteLine("\n\n{0}\n\n", result);
var result = await friend.SayHello("Good morning!");
Console.WriteLine($"""

{result}

""");

Console.WriteLine("Orleans is running.\nPress Enter to terminate...");
Console.ReadLine();
Expand Down
13 changes: 7 additions & 6 deletions samples/HelloWorld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ The sample consists of a single project which starts the Orleans-based applicati

To start our tour of the application, open [`IHelloGrain.cs`](./IHelloGrain.cs) and you will find the following interface declaration:

``` C#
```csharp
public interface IHelloGrain : IGrainWithStringKey
{
Task<string> SayHello(string greeting);
ValueTask<string> SayHello(string greeting);
}
```

Expand All @@ -23,16 +23,17 @@ We know it's a grain interface because it implements `IGrainWithStringKey`.
This means that when we want to get a reference to a grain implementing `IHelloGrain`, we will identify the grain instance using an string value.
In our case, as you will see later in [`Program.cs`](./Program.cs), we will use the string `"friend"` to identify the grain we wish to communicate with, but this could be any string:

``` C#
```csharp
var friend = grainFactory.GetGrain<IHelloGrain>("friend");
```

Now, open [`HelloGrain.cs`](./HelloGrain.cs) and we will see the implementation of our `IHelloGrain` interface:

``` C#
```csharp
public class HelloGrain : Grain, IHelloGrain
{
public Task<string> SayHello(string greeting) => Task.FromResult($"Hello, {greeting}!");
public ValueTask<string> SayHello(string greeting) =>
ValueTask.FromResult($"Hello, {greeting}!");
}
```

Expand All @@ -43,7 +44,7 @@ That type is used to identify the grain classes in an application.

Open [`Program.cs`](./Program.cs) to see how Orleans is configured:

``` C#
```csharp
using var host = new HostBuilder()
.UseOrleans(builder =>
{
Expand Down
Binary file removed samples/HelloWorld/code.png
Binary file not shown.