/// /// Registers grains from specified assembly which are sub classes of . /// Grains that implement are registered with a proxy implementation /// that is scoped to the grain's lifetime and retrieves the context from the grain itself avoiding circular dependencies. /// /// The container to add registrations to /// Assembly to scan for grain types public static void RegisterGrains(Container container, Assembly grainsAssembly) { Guard.CheckNullArgument(container, nameof(container)); var grainConcreteTypes = (from exportedType in grainsAssembly.GetExportedTypes() where exportedType.IsSubclassOf(typeof(Grain)) && !exportedType.IsAbstract select exportedType) .ToArray(); foreach (var grainType in grainConcreteTypes) { container.Register(grainType, grainType, Lifestyle.Transient); } var contextTypesProvidedByGrains = (from grainType in grainConcreteTypes from serviceType in grainType.GetInterfaces() where serviceType.IsGenericOf(typeof(IContextProvider<>)) let contextProviderServiceType = serviceType let providedContextType = contextProviderServiceType.GetGenericArguments()[0] from inheritedContextType in providedContextType.GetInheritedTypes() select inheritedContextType) .Distinct(); // We explicitly register context providers without single open-generic batch registrations for two reasons: // 1. It's more efficient to resolve services that were registered standard way, without open-generic batch registrations // 2. It allows to register context providers that aren't grains without conflicts foreach (var contextType in contextTypesProvidedByGrains) { container.Register(typeof(IContextProvider<>).MakeGenericType(contextType), typeof(GrainContextProviderProxy<>).MakeGenericType(contextType), Lifestyle.Singleton); } }