public void Options_Populate_Root_Resolve_Child_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");

            IOptions <MyOptions> options = childSp.GetRequiredService <IOptions <MyOptions> >();

            Assert.True(options.Value?.Prop);
        }
Exemplo n.º 2
0
        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..");
        }