dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
10.07k stars 2.03k forks source link

How to use dependency injection to inject services grains? #2010

Closed richcorless closed 8 years ago

richcorless commented 8 years ago

Hi,

There are a number of mentions of using DI to inject services into grains and being able to use your preferred container, just not how. Could someone point me in the direction of some sample code, tutorial, or hints of where to begin please?

Thanks, Rich

kwal commented 8 years ago

Orleans uses Microsoft.Framework.DependencyInjection

When building your cluster configuration, you can set the type that will handle your registration:

clusterConfig.Defaults.StartupTypeName = typeof(ServiceRegistration).AssemblyQualifiedName;

In that class, just make sure to have a method with the following signature:

public IServiceProvider ConfigureServices(IServiceCollection services)

From there you can register what you need using your container of choice.

Full example/test can be found here.

richcorless commented 8 years ago

Hi Kwal,

Thank you, that's exactly what I needed. Have it working with SimpleInjector now.

Rich