【发布时间】:2018-11-19 14:48:39
【问题描述】:
我正在尝试找出一种有效的方法来为不同的构建配置我的Startup.cs,而无需使用大量讨厌的 IfDef。如果我要配置一个普通的类,我通常会在 Dependency Injector 中换掉一个组件,一切都会完成,但是由于启动类是用来配置 DI 的,所以这是不可能的。
目前我正在对以下内容进行硬编码:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
}).AddIdentityServerAuthentication(AuthenticationRule.ConfigureForStaging());
//.AddIdentityServerAuthentication(AuthenticationRule.ConfigureForProduction());
我有一堆语句注释掉了代码库的吞吐量
const string connection = @"Server=StagingServer.com;Database=MyStagingDB;Trusted_Connection=True;";
//const string connection = @"Server=localhost;Database=MyLocalDB;Trusted_Connection=True;";
这不是很有效,而且很容易出错。如何配置我的启动类以轻松切换构建?
【问题讨论】:
-
您肯定应该使用不同的
appSettings.{Environment}.json文件。当默认情况下从该文件中获取连接字符串时,您对连接字符串进行硬编码这一事实令人担忧。请注意,大多数配置选项都接受Bind()调用,该调用直接从Configuration变量中获取数据 -
@CamiloTerevinto,谢谢,我会把一些东西移到那里,如果我需要稍微不同的行为应该怎么做?