Skip to content

Commit

Permalink
Update source code to address Orleans 7 DI issue (#37791)
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist authored Oct 30, 2023
1 parent 2e0aa2f commit 0577a60
Showing 1 changed file with 14 additions and 38 deletions.
52 changes: 14 additions & 38 deletions docs/orleans/host/client.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Orleans clients
description: Learn how to write .NET Orleans clients.
ms.date: 01/13/2023
ms.date: 10/30/2023
zone_pivot_groups: orleans-version
---

Expand Down Expand Up @@ -204,66 +204,42 @@ When connecting to a cluster in a different process (on a different machine), a
```csharp
public class ClusterClientHostedService : IHostedService
{
public IClusterClient Client { get; }
private readonly IClusterClient _client;

public ClusterClientHostedService(ILoggerProvider loggerProvider)
public ClusterClientHostedService(IClusterClient client)
{
Client = new ClientBuilder()
.UseLocalhostClustering()
.ConfigureLogging(builder => builder.AddProvider(loggerProvider))
.Build();
_client = client;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
// A retry filter could be provided here.
await Client.Connect();
await _client.Connect();
}

public async Task StopAsync(CancellationToken cancellationToken)
{
await Client.Close();
await _client.Close();

Client.Dispose();
_client.Dispose();
}
}
```

The service is then registered like this:

```csharp
await new HostBuilder()
.ConfigureServices(services =>
await Host.CreateDefaultBuilder(args)
.UseOrleansClient(builder =>
{
services.AddSingleton<ClusterClientHostedService>();
services.AddSingleton<IHostedService>(
sp => sp.GetService<ClusterClientHostedService>());
services.AddSingleton<IClusterClient>(
sp => sp.GetService<ClusterClientHostedService>().Client);
services.AddSingleton<IGrainFactory>(
sp => sp.GetService<ClusterClientHostedService>().Client);
builder.UseLocalhostClustering();
})
.ConfigureLogging(builder => builder.AddConsole())
.RunConsoleAsync();
```

At this point, an `IClusterClient` instance could be consumed anywhere that dependency injection is supported, such as in an ASP.NET controller:

```csharp
public class HomeController : Controller
{
private readonly IClusterClient _client;

public HomeController(IClusterClient client) => _client = client;

public IActionResult Index()
.ConfigureServices(services =>
{
var grain = _client.GetGrain<IMyGrain>();
var model = grain.GetModel();

return View(model);
services.AddHostedService<ClusterClientHostedService>();
}
}
)
.RunConsoleAsync();
```

### Example
Expand Down

0 comments on commit 0577a60

Please sign in to comment.