示例#1
0
 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);
        }
示例#3
0
        public ITenantContainerAdaptor CreateNestedContainerAndConfigure(string Name, Action <IServiceCollection> configure)
        {
            ITenantContainerAdaptor container = CreateNestedContainer(Name);

            Configure(configure);
            return(container);
        }
示例#4
0
 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;
            }
        }
示例#6
0
 protected override void RegisterBootstrapperTypes(ITenantContainerAdaptor applicationContainer)
 {
     applicationContainer.Configure((services) =>
     {
         services.AddSingleton <INancyModuleCatalog>(this);
         services.AddSingleton <IFileSystemReader, DefaultFileSystemReader>();
     });
 }
示例#7
0
 public TenantContainerBuilder(IServiceCollection defaultServices,
                               ITenantContainerAdaptor parentContainer,
                               Action <TTenant, IServiceCollection> configureTenant,
                               ITenantContainerEventsPublisher <TTenant> containerEventsPublisher)
 {
     _defaultServices          = defaultServices;
     _parentContainer          = parentContainer;
     _configureTenant          = configureTenant;
     _containerEventsPublisher = containerEventsPublisher;
 }
示例#8
0
 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);
        }
示例#10
0
 protected override void RegisterInstances(ITenantContainerAdaptor container, IEnumerable <InstanceRegistration> instanceRegistrations)
 {
     container.Configure((services) =>
     {
         foreach (var instanceRegistration in instanceRegistrations)
         {
             services.AddSingleton(instanceRegistration.RegistrationType, instanceRegistration.Implementation);
         }
     });
 }
示例#11
0
 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);
         }
     });
 }
示例#12
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..");
        }
示例#13
0
        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;
 }
示例#16
0
 public StructureMapTenantContainerBuilder(ITenantContainerAdaptor parentContainer, Action <TTenant, ConfigurationExpression> configureTenant)
 {
     _parentContainer = parentContainer;
     _configureTenant = configureTenant;
 }
示例#17
0
        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);
        }
示例#20
0
        protected override IEnumerable <INancyModule> GetAllModules(ITenantContainerAdaptor container)
        {
            var sp = this.ApplicationContainer;

            return(sp.GetServices <INancyModule>());
        }
示例#21
0
        //public int ITenantContainerAdaptor { get; set; }

        public TenantContainerNancyBootstrapper(ITenantContainerAdaptor applicationContainer)
        {
            _applicationContainer = applicationContainer;
        }
 public PerRequestContainer(ITenantContainerAdaptor requestContainer)
 {
     RequestContainer = requestContainer;
 }
示例#23
0
        protected override IEnumerable <IRequestStartup> RegisterAndGetRequestStartupTasks(ITenantContainerAdaptor container, Type[] requestStartupTypes)
        {
            var sp = container;

            return(requestStartupTypes.Select(sp.GetService).Cast <IRequestStartup>().ToArray());
        }