dotnet/orleans

8.0-rc1 - AddServiceByName is gone.

turowicz opened this issue · 9 comments

How to migrate? I understand there will be a guide coming soon but for now the DI is the only thing that doesn't compile after bumping package versions.

It was replaced with .Net 8 KeyedServices https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#keyed-di-services

I don't know an alternative for the issue you're having with it

Does injecting custom grain storage work the same way?

services.AddSingleton<IGrainStorage>(f => f.GetServiceByName<IGrainStorage>(name));
services.AddSingletonNamedService<IGrainStorage>(name, (f, n) => f.GetService<OrleansFileStorage>());
services.AddSingletonNamedService<IGrainStorage>(pubSub, (f, n) => f.GetService<OrleansFileStorage>());
services.AddSingleton(f => new OrleansFileStorage(f.GetService<ILogger>()));

would turn to:

services.AddSingleton<IGrainStorage>(f => f.GetKeyedService<IGrainStorage>(name));
services.AddKeyedSingleton<IGrainStorage>(name, (f, n) => f.GetService<OrleansFileStorage>());
services.AddKeyedSingleton<IGrainStorage>(pubSub, (f, n) => f.GetService<OrleansFileStorage>());
services.AddSingleton(f => new OrleansFileStorage(f.GetService<ILogger>()));

Does injecting custom grain storage work the same way?

services.AddSingleton<IGrainStorage>(f => f.GetServiceByName<IGrainStorage>(name));
services.AddSingletonNamedService<IGrainStorage>(name, (f, n) => f.GetService<OrleansFileStorage>());
services.AddSingletonNamedService<IGrainStorage>(pubSub, (f, n) => f.GetService<OrleansFileStorage>());
services.AddSingleton(f => new OrleansFileStorage(f.GetService<ILogger>()));

would turn to:

services.AddSingleton<IGrainStorage>(f => f.GetKeyedService<IGrainStorage>(name));
services.AddKeyedSingleton<IGrainStorage>(name, (f, n) => f.GetService<OrleansFileStorage>());
services.AddKeyedSingleton<IGrainStorage>(pubSub, (f, n) => f.GetService<OrleansFileStorage>());
services.AddSingleton(f => new OrleansFileStorage(f.GetService<ILogger>()));

Yes! You could also use the helper method AddGrainStorage.

@benjaminpetit would the above then become this?

siloBuilder.Services.AddGrainStorage(name, (f, n) => f.GetService<OrleansFileStorage>());
siloBuilder.Services.AddGrainStorage(pubSub, (f, n) => f.GetService<OrleansFileStorage>());
siloBuilder.Services.AddSingleton(f => new OrleansFileStorage(f.GetService<ILogger>());

Correct. As a nitpick I would probably change the f.GetService<OrleansFileStorage>() to f.GetRequiredService<OrleansFileStorage>().

Its not as required as one might think ;) thanks anyways!