启动时的aspboilerplate呼叫服务

时间:2019-07-16 06:04:42

标签: c# asp.net-core aspnetboilerplate

我需要获取初创企业中的租户列表,以便我可以验证一些项目。

我尝试在Startup.cs中添加以下行

services.AddTransient<TenantAppService>();
var tenantService = services.BuildServiceProvider().GetService<ITenantAppService>();
var tennants = tenantService.GetAll(null);

但是tenantService始终为空

Startup.cs

public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        string connectionString = _appConfiguration.GetConnectionString("Default");
        var cb = new SqlConnectionStringBuilder(connectionString);

        var migrationsAssembly = typeof(PlatformDbContext).GetTypeInfo().Assembly.GetName().Name;

        IdentityRegistrar.Register(services);
        services
           .AddIdentityServer(options =>
           {
               //options.Discovery.CustomEntries.Add("custom_endpoint", "~/api/custom");
           })
         .AddAbpIdentityServer<User>(options=>
         {
             options.UpdateAbpClaimTypes = true;  // default is true
             options.UpdateJwtSecurityTokenHandlerDefaultInboundClaimTypeMap = true; // default is true
         })
         .AddAbpPersistedGrants<IAbpPersistedGrantDbContext>()
         .AddConfigurationStore(options =>
                               options.ConfigureDbContext = builder =>
                                   builder.UseSqlServer(connectionString, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)))          
         .AddSigningCredential(new SigningCredentials(AuthConfigHelper.GetSecurityKey(), SecurityAlgorithms.RsaSha256));

        services.AddTransient<TenantAppService>();
        var tenantService = services.BuildServiceProvider().GetService<ITenantAppService>();
        var tennants = tenantService.GetAll(null);

        AuthConfigurer.Configure(services, _appConfiguration);

        services.AddSignalR();

        // Configure CORS for angular2 UI
        services.AddCors(
            options => options.AddPolicy(
                _defaultCorsPolicyName,
                builder => builder
                    .WithOrigins(
                        // App:CorsOrigins in appsettings.json can contain more than one address separated by comma.
                        _appConfiguration["App:CorsOrigins"]
                            .Split(",", StringSplitOptions.RemoveEmptyEntries)
                            .Select(o => o.RemovePostFix("/"))
                            .ToArray()
                    )
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()
            )
        );

        // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
        services.AddSwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new Info { Title = "Platform API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);

            // Define the BearerAuth scheme that's in use
            options.AddSecurityDefinition("bearerAuth", new ApiKeyScheme()
            {
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                Name = "Authorization",
                In = "header",
                Type = "apiKey"
            });

            // Registering File upload operation
            options.OperationFilter<FormFileSwaggerFilter>();
        });

        // MVC
        services.AddMvc(
            options => options.Filters.Add(new CorsAuthorizationFilterFactory(_defaultCorsPolicyName))
        );

        // Configure Abp and Dependency Injection
        return services.AddAbp<PlatformWebHostModule>(
            // Configure Log4Net logging
            options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                f => f.UseAbpLog4Net().WithConfig("log4net.config")
            )
        );
    }

我需要将租户或tenantService实例传递给AuthConfigurer.Configure(services, _appConfiguration);以便在AuthConfigurer.Configure内部进行验证

我该如何实现?

1 个答案:

答案 0 :(得分:0)

您可以使用IocManager来解决所有依赖关系。例如,

var jobManager = Configuration.IocManager.Resolve<IQuartzScheduleJobManager>();

对于您的问题,您可以使用:

var tenantService = Configuration.IocManager.Resolve<ITenantAppService>();
var tennants = tenantService.GetAll(null);