Пример #1
0
 public MultitenancyRequestServicesContainerMiddleware(
     RequestDelegate next,
     IHttpContextAccessor contextAccessor,
     IServiceFactoryForMultitenancy <TTenant> serviceFactoryForMultitenancy)
 {
     this.next            = next ?? throw new ArgumentNullException(nameof(next));
     this.contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
     this.serviceFactoryForMultitenancy = serviceFactoryForMultitenancy ?? throw new ArgumentNullException(nameof(serviceFactoryForMultitenancy));
 }
        /// <summary>
        /// Configure action per tenant.
        /// </summary>
        /// <typeparam name="TTenant">Tenant object.</typeparam>
        /// <param name="app">Defines a class that provides the mechanisms to configure an application's request.</param>
        /// <param name="configuration">Define a configuration per tenant.</param>
        /// <returns>IApplicationBuilder.</returns>
        public static IApplicationBuilder UsePerTenant <TTenant>(this IApplicationBuilder app, Action <TenantPipelineBuilderContext <TTenant>, IApplicationBuilder> configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException($"Argument {nameof(app)} must not be null");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException($"Argument {nameof(configuration)} must not be null");
            }


            IOptionsMonitor <MultitenancyOptions <TTenant> > optionsMonitor = app.ApplicationServices.GetRequiredService <IOptionsMonitor <MultitenancyOptions <TTenant> > >();
            ILog <TenantPipelineMiddleware <TTenant> >       logger         = app.ApplicationServices.GetRequiredService <ILog <TenantPipelineMiddleware <TTenant> > >();
            IServiceFactoryForMultitenancy <TTenant>         serviceFactoryForMultitenancy = app.ApplicationServices.GetRequiredService <IServiceFactoryForMultitenancy <TTenant> >();

            app.Use(next => new TenantPipelineMiddleware <TTenant>(next, app, configuration, optionsMonitor, logger, serviceFactoryForMultitenancy).Invoke);
            return(app);
        }
        public TenantPipelineMiddleware(
            RequestDelegate next,
            IApplicationBuilder rootApp,
            Action <TenantPipelineBuilderContext <TTenant>,
                    IApplicationBuilder> configuration, IOptionsMonitor <MultitenancyOptions <TTenant> > optionsMonitor,
            ILog <TenantPipelineMiddleware <TTenant> > logger,
            IServiceFactoryForMultitenancy <TTenant> serviceFactoryForMultitenancy)
        {
            this.next    = next ?? throw new ArgumentNullException($"Argument {nameof(next)} must not be null");
            this.rootApp = rootApp ?? throw new ArgumentNullException($"Argument {nameof(rootApp)} must not be null");
            this.logger  = logger ?? throw new ArgumentNullException(nameof(logger));
            this.serviceFactoryForMultitenancy = serviceFactoryForMultitenancy ?? throw new ArgumentNullException(nameof(serviceFactoryForMultitenancy));

            this.configuration  = configuration ?? throw new ArgumentNullException($"Argument {nameof(configuration)} must not be null");
            this.optionsMonitor = optionsMonitor ?? throw new ArgumentNullException($"Argument {nameof(optionsMonitor)} must not be null");
            this.optionsMonitor.OnChange(vals =>
            {
                pipelinesBranchBuilder.Clear();

                // log change.
                this.logger.Debug($"Config changed: {string.Join(", ", vals)}");
            });
        }