public static AdaptedContainerBuilderOptions <TTenant> WithStructureMap <TTenant>(this ContainerBuilderOptions <TTenant> options,
                                                                                          Action <TTenant, IServiceCollection> configureTenant)
            where TTenant : class
        {
            var adaptorFactory = new Func <ITenantContainerAdaptor>(() =>
            {
                // host level container.
                var container = new StructureMap.Container();
                container.Populate(options.Builder.Services);
                var adaptedContainer = container.GetInstance <ITenantContainerAdaptor>();
                // add ITenantContainerBuilder<TTenant> service to the host container
                // This service can be used to build a child container (adaptor) for a particular tenant, when required.
                container.Configure(_ =>
                                    _.For <ITenantContainerBuilder <TTenant> >()
                                    .Use(new TenantContainerBuilder <TTenant>(adaptedContainer, configureTenant))
                                    );

                var adaptor = container.GetInstance <ITenantContainerAdaptor>();
                return(adaptor);
            });

            var adapted = new AdaptedContainerBuilderOptions <TTenant>(options, adaptorFactory);

            options.Builder.Services.TryAddScoped((_) => adapted);

            return(adapted);
        }
Example #2
0
        public static ContainerBuilderOptions <TTenant> WithStructureMapServiceCollection <TTenant>(this ContainerBuilderOptions <TTenant> options,
                                                                                                    Action <TTenant, IServiceCollection> configureTenant)
            where TTenant : class
        {
            // Set a func, that is only invoked when the ServiceProvider is required. This ensures it runs after all other configuration methods
            // which is important as other methods can still add new services to the servicecollection after this one is invoked and we
            // dont want them to be missed when populating the container.
            options.Builder.BuildServiceProvider = new Func <IServiceProvider>(() =>
            {
                var container = new StructureMap.Container();
                container.Populate(options.Builder.Services);

                container.Configure(_ =>
                                    _.For <ITenantContainerBuilder <TTenant> >()
                                    .Use(new StructureMapTenantContainerBuilder <TTenant>(container, (tenant, configurationExpression) =>
                {
                    var tenantServices = new ServiceCollection();
                    configureTenant(tenant, tenantServices);
                    configurationExpression.Populate(tenantServices);
                }))
                                    );

                // now configure nested container per tenant.
                return(container.GetInstance <IServiceProvider>());
            });

            return(options);
        }
        public void Options_Populate_Root_Resolve_Root()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddOptions();
            services.Configure <MyOptions>((a) =>
            {
                a.Prop = true;
            });
            // ServiceProvider serviceProvider = services.BuildServiceProvider();


            StructureMap.Container container = new StructureMap.Container();
            container.Populate(services);

            // container.Populate(services);

            IServiceProvider     sp      = container.GetInstance <IServiceProvider>();
            IOptions <MyOptions> options = sp.GetRequiredService <IOptions <MyOptions> >();

            Assert.True(options.Value?.Prop);
        }
Example #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton<IConfigurationRoot>(Configuration);
            services.Configure<AppSettings>(Configuration.GetSection("Application"));
            var appSettings = Configuration.Get<AppSettings>("Application");


            // Add framework services.
            services.AddMvc();    
            //services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache

            // Using Redis as Memory cache
            services.AddDistributedRedisCache(options =>
            {
                options.Configuration = appSettings.Redis.Url;
                options.InstanceName = appSettings.Redis.InstanceName;
                options.ResolveDns();
            });
            services.AddSession();
            services.AddSignalR();

            // Predefine set of policies        
            services.AddAuthorization(options =>
            {
                options.AddPolicy("ADMIN", policy => {
                    policy.RequireClaim("admin");
                });

                 options.AddPolicy("READ", policy => {
                    policy.RequireClaim("read");
                });

                options.AddPolicy("WRITE", policy => {
                    policy.RequireClaim("write");
                });

                options.AddPolicy("GET", policy => {
                    policy.RequireClaim("get");
                });

                options.AddPolicy("POST", policy => {
                    policy.RequireClaim("post");
                });

                 options.AddPolicy("DELETE", policy => {
                    policy.RequireClaim("delete");
                });
            });

            // Add Authentication services.
            services.AddAuthentication(sharedOptions => sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);

            // Initialize container            
            container = new Container();
            container.Configure(c =>
            {
                Serilog.ILogger appLogger = new LoggerConfiguration()
                     .WriteTo.Logzio(appSettings.LogzIO.AuthToken,
                     application: appSettings.LogzIO.Application,
                     reporterType: appSettings.LogzIO.ReporterType,
                     environment: appSettings.LogzIO.Environment)
                     .CreateLogger();
                c.For<IApplicationContext>().Use<HttpAPIContext>();
                c.ForSingletonOf<AppSettings>().Use(appSettings);
                c.ForSingletonOf<Serilog.ILogger>().Use(appLogger);
            });

            services.InitializeAutoMapper();


            // Resolve container 
            DependencyResolver.RegisterResolver(new StructureMapIOCContainer(container)).RegisterImplmentation();
            container.Populate(services);

            return container.GetInstance<IServiceProvider>();
        }