public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) { return(_scope.Locate(_propertyName)); }
private async void ConfigureContainer() { var containerBuilder = new DependencyInjectionContainer(configuration => { configuration.AutoRegisterUnknown = false; configuration.Behaviors.AllowInjectionScopeLocation = true; configuration.SingletonPerScopeShareContext = true; }); _container = containerBuilder; _container.Configure(block => { block.AddModule(new DatabaseModule()); block.ExportModuleScope <ModuleActivator>(); }); var isConnected = await ConnectDatabase(_container, Settings.Default.DatabasePath); if (!isConnected) { var isConnectedToNew = CreateDatabase(_container); if (!isConnectedToNew) { throw new Exception("Cannot create new database!"); } } var rootModuleLoader = _container.Locate <ModuleActivator>(); var mainModuleToken = new MainModuleToken("TeacherAssistant"); var mainModule = await rootModuleLoader.ActivateAsync(mainModuleToken); mainModule.GetEntryComponent(); }
private static void ConfigureServices(IInjectionScope scope) { var tenantRegistry = scope.Locate <IAppTenantRegistry>(); var tenants = tenantRegistry.GetAll().ToList(); scope.Configure(c => { c.Export <TenantGrainActivator>().As <IGrainActivator>().Lifestyle.Singleton(); //c // //.Export<MockLoLHeroDataClient>() // .Export<MockHotsHeroDataClient>() // .As<IHeroDataClient>() // ; // todo: use multi tenancy lib c.ExportFactory <IExportLocatorScope, ITenant>(exportScope => exportScope.GetTenantContext()); //c.Export<MockHotsHeroDataClient>().AsKeyed<IHeroDataClient>("hots").Lifestyle.Singleton(); //c.Export<MockLoLHeroDataClient>().AsKeyed<IHeroDataClient>("lol").Lifestyle.Singleton(); //c.ExportFactory<IExportLocatorScope, ITenant, IHeroDataClient>((scope, tenant) => //{ // var tenant = RequestContext.Get("tenant") ?? tenant?.Key; // if (tenant == null) throw new ArgumentNullException("tenant", "Tenant must be defined"); // return scope.Locate<IHeroDataClient>(withKey: tenant); //}); //c.ExportForAllTenants<IHeroDataClient, MockLoLHeroDataClient>(Tenants.All, x => x.Lifestyle.Singleton()); //c.ForTenant(Tenants.LeageOfLegends).PopulateFrom(x => x.AddHeroesLoLGrains()); //c.ForTenant(Tenants.HeroesOfTheStorm).PopulateFrom(x => x.AddHeroesHotsGrains()); c.ForTenants(tenants, tb => { tb .ForTenant(AppTenantRegistry.LeagueOfLegends.Key, tc => tc.PopulateFrom(x => x.AddAppLoLGrains())) .ForTenant(x => x.Key == AppTenantRegistry.HeroesOfTheStorm.Key, tc => tc.PopulateFrom(x => x.AddAppHotsGrains())) ; }); /* * * // register with filter tenant * c.ForTenants(tenants, tb => * { * tb.ForTenant(x => x.Platform == "x").PopulateFrom(x => x.AddHeroesHotsGrains()); * }); * * // register one per type * c.For<IHeroDataClient>(tb => * { * tb.For(x => x.Key == "lol").Use<MockLoLHeroDataClient>(); * }); * */ }); }
/// <summary> /// Populate a container with service descriptors /// </summary> /// <param name="exportLocator">export locator</param> /// <param name="descriptors">descriptors</param> public static IServiceProvider Populate(this IInjectionScope exportLocator, IEnumerable <ServiceDescriptor> descriptors) { exportLocator.Configure(c => { c.Export <GraceServiceProvider>().As <IServiceProvider>(); c.Export <GraceLifetimeScopeServiceScopeFactory>().As <IServiceScopeFactory>(); Register(c, descriptors); }); return(exportLocator.Locate <IServiceProvider>()); }
/// <summary> /// Populate a container with service descriptors /// </summary> /// <param name="exportLocator">export locator</param> /// <param name="descriptors">descriptors</param> public static IServiceProvider Populate(this IInjectionScope exportLocator, IEnumerable <ServiceDescriptor> descriptors) { exportLocator.Configure(c => { c.ExcludeTypeFromAutoRegistration(nameof(Microsoft) + ".*"); c.Export <GraceServiceProvider>().As <IServiceProvider>().ExternallyOwned(); c.Export <GraceLifetimeScopeServiceScopeFactory>().As <IServiceScopeFactory>(); Register(c, descriptors); }); return(exportLocator.Locate <IServiceProvider>()); }
/// <summary> /// Configure injection scope for multi tenancy, this will resolve and use <see cref="ITenantRegistry<TTenant>"/> in order to obtain tenants. /// </summary> /// <typeparam name="TTenant">Tenant content model to use.</typeparam> /// <typeparam name="TTenantRegistry">Tenant registry to resolve tenants from.</typeparam> /// <param name="scope"></param> /// <param name="configure"></param> /// <returns></returns> public static IInjectionScope ForTenants <TTenant, TTenantRegistry>(this IInjectionScope scope, Action <TenantsContainerBuilder <TTenant> > configure ) where TTenant : class, ITenant where TTenantRegistry : ITenantRegistry <TTenant> { var tenantRegistry = scope.Locate <TTenantRegistry>(); var tenants = tenantRegistry.GetAll(); scope.Configure(c => { c.ExportTenant <TTenant>(); c.ForTenants(tenants, configure); }); return(scope); }
/// <inheritdoc /> public IEnumerable <IActivationStrategy> ProvideExports(IInjectionScope scope, IActivationExpressionRequest request) { var fullName = request.ActivationType.FullName; if ((request.ActivationType.GetTypeInfo().IsInterface || request.ActivationType.GetTypeInfo().IsAbstract) && _namespaceConfig.Namespaces.Any(proxyNamespace => fullName.StartsWith(proxyNamespace))) { if (_namespaceConfig.Serializer == null) { _namespaceConfig.Serializer = scope.LocateOrDefault <IClientSerializer>(); #if NETCOREAPP3_1 _namespaceConfig.Serializer = new JsonClientSerializer(); #endif if (_namespaceConfig.Serializer == null) { throw new Exception("IClientSerializer implementation is not exported"); } } var implementationGenerator = scope.Locate <IServiceImplementationGenerator>(); var implementationRequest = new ImplementationRequest { DefaultSerializer = _namespaceConfig.Serializer, ExposeDefaultMethod = ExposeDefaultMethod.PostOnly, ClientProvider = _clientProvider, InterfaceType = request.ActivationType, NamingConventionService = _namespaceConfig.NamingConvention }; var implementationType = implementationGenerator.GenerateImplementationForInterface(implementationRequest); var strategy = new CompiledExportStrategy(implementationType, scope, request.Services.LifestyleExpressionBuilder); strategy.AddExportAs(request.ActivationType); return(new IActivationStrategy[] { strategy }); } return(Array.Empty <IActivationStrategy>()); }
/// <summary> /// Registers an object for message handling /// </summary> /// <typeparam name="TMessage">message type</typeparam> /// <param name="injectionScope"></param> /// <param name="injectionContext"></param> /// <param name="injectionTaget"></param> /// <param name="messageHandlerInfo"></param> /// <param name="token"></param> /// <param name="holdRefernce"></param> /// <param name="background"></param> public static void RegisterMethodAsMessageHandler <TMessage>(IInjectionScope injectionScope, IInjectionContext injectionContext, object injectionTaget, MethodInfoWrapper messageHandlerInfo, object token, bool holdRefernce, bool background) { Action <TMessage> messageHandler = (Action <TMessage>)messageHandlerInfo.MethodInfo.CreateDelegate(typeof(Action <TMessage>), injectionTaget); IDispatchedMessenger messenger = injectionScope.Locate <IDispatchedMessenger>(); if (messenger == null) { throw new Exception("Attempting to register handler " + messageHandlerInfo.MethodInfo.Name + " and could not locate IDispatchedMessenger"); } messenger.Register(messageHandler, token, holdRefernce, background); }
/// <summary>Provide exports for a missing type</summary> /// <param name="scope">scope to provide value</param> /// <param name="request">request</param> /// <returns>set of activation strategies</returns> public IEnumerable <IActivationStrategy> ProvideExports(IInjectionScope scope, IActivationExpressionRequest request) { var fullName = request.ActivationType.FullName; if ((request.ActivationType.GetTypeInfo().IsInterface || request.ActivationType.GetTypeInfo().IsAbstract) && _proxyNamespaces.Any(proxyNamespace => fullName.StartsWith(proxyNamespace))) { var proxyGenerator = scope.Locate <IProxyGenerator>(); var proxyType = proxyGenerator.GenerateProxyType(request.ActivationType, _callByName); var strategy = new CompiledExportStrategy(proxyType, scope, request.Services.LifestyleExpressionBuilder); strategy.AddExportAs(request.ActivationType); yield return(strategy); } }
/// <summary> /// Locates an ILifestyle /// </summary> /// <param name="injectionScope"></param> /// <param name="injectionContext"></param> /// <returns></returns> private static ILifestyle LocateContainer(IInjectionScope injectionScope, IInjectionContext injectionContext) { ILifestyle returnValue = null; try { IPerRequestLifestyleProvider provider = injectionScope.Locate<IPerRequestLifestyleProvider>(injectionContext); if (provider != null) { returnValue = provider.ProvideContainer(); } } catch (Exception exp) { Logger.Error("Exception throw while trying to locate IPerRequestLifestyleProvider", "SingletonPerRequest", exp); } return returnValue ?? new SingletonPerInjectionContextLifestyle(); }