protected override void RegisterNancyEnvironment(ITenantContainerAdaptor container, INancyEnvironment environment) { container.Configure((services) => { services.AddSingleton <INancyEnvironment>(environment); }); }
public void Options_Populate_Root_Resolve_Nested_Using_TenantContainerAdaptor() { ServiceCollection services = new ServiceCollection(); services.AddOptions(); services.AddLogging(); services.Configure <MyOptions>((a) => { a.Prop = true; }); ServiceProvider serviceProvider = services.BuildServiceProvider(); StructureMap.Container container = new StructureMap.Container(); Dotnettency.Container.StructureMap.ContainerExtensions.Populate(container, services); // container.Populate(services); ITenantContainerAdaptor sp = container.GetInstance <ITenantContainerAdaptor>(); ITenantContainerAdaptor childSp = sp.CreateChildContainer("Child"); var nestedSp = childSp.CreateChildContainer("Nested"); IOptions <MyOptions> options = nestedSp.GetRequiredService <IOptions <MyOptions> >(); Assert.True(options.Value?.Prop); }
public ITenantContainerAdaptor CreateNestedContainerAndConfigure(string Name, Action <IServiceCollection> configure) { ITenantContainerAdaptor container = CreateNestedContainer(Name); Configure(configure); return(container); }
public Task <IEnumerable <TModule> > GetModulesForTenant(ITenantContainerAdaptor container, TTenant tenant) { return(Task.Run(() => { return _getModulesDelegate(container, tenant); })); }
public async Task Invoke(HttpContext context, ITenantNancyBootstrapperAccessor <TTenant> tenantNancyBootstrapper, ITenantContainerAccessor <TTenant> tenantContainerAccessor, ITenantRequestContainerAccessor <TTenant> tenantRequestContainerAccessor) { // get the nancy bootstrapper, // adjust its request container - give it the current request container to return. // get the nancy engine // var tenantContainer = await tenantContainerAccessor.TenantContainer.Value; var tenantRequestContainer = await tenantRequestContainerAccessor.TenantRequestContainer.Value; var nancyBootstrapper = await tenantNancyBootstrapper.Bootstrapper.Value; if (tenantRequestContainer == null || nancyBootstrapper == null) { await _next.Invoke(context); return; } // swap out nancy request services. ITenantContainerAdaptor old = nancyBootstrapper.RequestContainerAdaptor; try { nancyBootstrapper.RequestContainerAdaptor = tenantRequestContainer.RequestContainer; var engine = nancyBootstrapper.GetEngine(); var nancyHandler = new NancyHandler(engine); await nancyHandler.ProcessRequest(context, NancyPassThroughOptions.PassThroughWhenStatusCodesAre(global::Nancy.HttpStatusCode.NotFound), _next); } finally { nancyBootstrapper.RequestContainerAdaptor = old; } }
protected override void RegisterBootstrapperTypes(ITenantContainerAdaptor applicationContainer) { applicationContainer.Configure((services) => { services.AddSingleton <INancyModuleCatalog>(this); services.AddSingleton <IFileSystemReader, DefaultFileSystemReader>(); }); }
public TenantContainerBuilder(IServiceCollection defaultServices, ITenantContainerAdaptor parentContainer, Action <TTenant, IServiceCollection> configureTenant, ITenantContainerEventsPublisher <TTenant> containerEventsPublisher) { _defaultServices = defaultServices; _parentContainer = parentContainer; _configureTenant = configureTenant; _containerEventsPublisher = containerEventsPublisher; }
protected override void RegisterRequestContainerModules(ITenantContainerAdaptor container, IEnumerable <ModuleRegistration> moduleRegistrationTypes) { container.Configure((services) => { foreach (var registrationType in moduleRegistrationTypes) { services.AddTransient(typeof(INancyModule), registrationType.ModuleType); } }); }
public ITenantRequestContainerAccessor <TTenant> WithTenantContainer(ITenantContainerAdaptor tenantContainer) { TenantRequestContainer = new Lazy <Task <PerRequestContainer> >(async() => { var requestContainer = tenantContainer.CreateNestedContainer(true); return(new PerRequestContainer(requestContainer)); }); return(this); }
protected override void RegisterInstances(ITenantContainerAdaptor container, IEnumerable <InstanceRegistration> instanceRegistrations) { container.Configure((services) => { foreach (var instanceRegistration in instanceRegistrations) { services.AddSingleton(instanceRegistration.RegistrationType, instanceRegistration.Implementation); } }); }
protected override void RegisterTypes(ITenantContainerAdaptor container, IEnumerable <TypeRegistration> typeRegistrations) { container.Configure((services) => { foreach (var typeRegistration in typeRegistrations) { RegisterType( typeRegistration.RegistrationType, typeRegistration.ImplementationType, container.Role == ContainerRole.Scoped ? Lifetime.PerRequest : typeRegistration.Lifetime, services); } }); }
public async Task DisplayInfo(HttpContext context) { ILogger <Startup> logger = context.RequestServices.GetRequiredService <ILogger <Startup> >(); logger.LogDebug("App Run.."); ITenantContainerAdaptor container = context.RequestServices as ITenantContainerAdaptor; logger.LogDebug("App Run Container Is: {id}, {containerNAme}, {role}", container.ContainerId, container.ContainerName, container.Role); // Use ITenantAccessor to access the current tenant. ITenantAccessor <Tenant> tenantAccessor = container.GetRequiredService <ITenantAccessor <Tenant> >(); Tenant tenant = await tenantAccessor.CurrentTenant.Value; // This service was registered as singleton in tenant container. SomeTenantService someTenantService = container.GetService <SomeTenantService>(); // The tenant shell to access context for the tenant - even if the tenant is null ITenantShellAccessor <Tenant> tenantShellAccessor = context.RequestServices.GetRequiredService <ITenantShellAccessor <Tenant> >(); TenantShell <Tenant> tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; var myOptions = context.RequestServices.GetRequiredService <IOptions <MyOptions> >(); string tenantShellId = tenantShell == null ? "{NULL TENANT SHELL}" : tenantShell.Id.ToString(); string tenantName = tenant == null ? "{NULL TENANT}" : tenant.Name; string injectedTenantName = someTenantService?.TenantName ?? "{NULL SERVICE}"; // Accessing a content file. string fileContent = someTenantService?.GetContentFile("/Info.txt"); context.Response.ContentType = new MediaTypeHeaderValue("application/json").ToString(); var result = new { TenantShellId = tenantShellId, TenantName = tenantName, TenantScopedServiceId = someTenantService?.Id, InjectedTenantName = injectedTenantName, TenantContentFile = fileContent, OptionsFoo = myOptions.Value.Foo }; string jsonResult = JsonConvert.SerializeObject(result); await context.Response.WriteAsync(jsonResult, Encoding.UTF8); logger.LogDebug("App Run Finished.."); }
public static AdaptedContainerBuilderOptions <TTenant> Autofac <TTenant>( this ContainerBuilderOptions <TTenant> options, Action <TTenant, IServiceCollection> configureTenant) where TTenant : class { Func <ITenantContainerAdaptor> adaptorFactory = new Func <ITenantContainerAdaptor>(() => { // host level container. ContainerBuilder builder = new ContainerBuilder(); builder.Populate(options.Builder.Services); builder.AddDotnettencyContainerServices(); // Build the root container. IContainer container = builder.Build(); ITenantContainerAdaptor adaptedContainer = container.Resolve <ITenantContainerAdaptor>(); // Get the service that allows us to publish events relating to tenant container events. container.TryResolve <ITenantContainerEventsPublisher <TTenant> >(out ITenantContainerEventsPublisher <TTenant> containerEventsPublisher); // Update the root container with a service that can be used to build per tenant container! ContainerBuilder updateBuilder = new ContainerBuilder(); var defaultServices = options.DefaultServices; updateBuilder.RegisterInstance(new TenantContainerBuilder <TTenant>(defaultServices, adaptedContainer, configureTenant, containerEventsPublisher)).As <ITenantContainerBuilder <TTenant> >(); updateBuilder.Update(container); return(adaptedContainer); //ITenantContainerAdaptor adaptor = container.Resolve<ITenantContainerAdaptor>(); //return adaptor; // return adaptedContainer; }); AdaptedContainerBuilderOptions <TTenant> adapted = new AdaptedContainerBuilderOptions <TTenant>(options, adaptorFactory); return(adapted); }
public TenantContainerServiceScope(ITenantContainerAdaptor container) { _container = container; ServiceProvider = _container; }
public TenantContainerServiceScopeFactory(ITenantContainerAdaptor container) { _container = container; }
public StructureMapTenantContainerBuilder(ITenantContainerAdaptor parentContainer, Action <TTenant, ConfigurationExpression> configureTenant) { _parentContainer = parentContainer; _configureTenant = configureTenant; }
protected override INancyModule GetModule(ITenantContainerAdaptor container, Type moduleType) { var sp = container; return((INancyModule)sp.GetService(moduleType)); }
public TenantContainerBuilder(ITenantContainerAdaptor parentContainer, Action <TTenant, IServiceCollection> configureTenant) { _parentContainer = parentContainer; _configureTenant = configureTenant; }
public async Task <ITenantContainerAdaptor> Get(TTenant currentTenant) { ITenantContainerAdaptor newContainer = await BuildContainer(currentTenant); return(newContainer); }
protected override IEnumerable <INancyModule> GetAllModules(ITenantContainerAdaptor container) { var sp = this.ApplicationContainer; return(sp.GetServices <INancyModule>()); }
//public int ITenantContainerAdaptor { get; set; } public TenantContainerNancyBootstrapper(ITenantContainerAdaptor applicationContainer) { _applicationContainer = applicationContainer; }
public PerRequestContainer(ITenantContainerAdaptor requestContainer) { RequestContainer = requestContainer; }
protected override IEnumerable <IRequestStartup> RegisterAndGetRequestStartupTasks(ITenantContainerAdaptor container, Type[] requestStartupTypes) { var sp = container; return(requestStartupTypes.Select(sp.GetService).Cast <IRequestStartup>().ToArray()); }