更改环境变量运行时间

发布于 2025-01-20 10:41:28 字数 1930 浏览 5 评论 0原文

如何更改环境变量运行时间? 使用登录页面我正在选择开发或生产。 基于该,我必须专门使用ConnectionsTring设备

"ConnectionStrings": {
"PROJECTNAME_Development": "SERVER_DETAILS",
"PROJECTNAME_Production": "SERVER_DETAILS",
}

这是我的loginservices.cs文件

public class LoginService: ILoginService
{
        private readonly string _connectionString = string.Empty;
        private readonly IConfiguration _configuration;
}
public LoginService (IConfiguration configuration)
{
        _configuration = configuration;
        //If ADMIN SELECT Development
        _connectionString = _configuration.GetConnectionString("PROJECTNAME_Development");
        //If ADMIN SELECT Production
        _connectionString = _configuration.GetConnectionString("PROJECTNAME_Production");
}

更新1 我尝试了此代码。 它可以正常工作,但是还有其他方法,例如在一个位置我进行更改,环境变量值将更新。

我的控制器

 [HttpPost]
        public IActionResult Login(LoginModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.DevelopmentServer = "Development";
                    // model.DevelopmentServer = "Production";
                    ...
                     var Result = ILoginService.AdminLogin(model);  
  
 public LoginService(IConfiguration configuration, IOptions<ConnectionStringDetails> connectionStrings)
        {
            _proDBCon = connectionStrings.Value.PROJECTNAME_Development;
            _proDBConProduction = connectionStrings.Value.PROJECTNAME_Production;
        }

在同一文件loginservice.cs文件中

public LoginInfo AdminLogin(LoginModel model)
{
 if (model.DevelopmentServer == "Development") {
                _connectionString = _proDBCon;
            }
            else {
                _connectionString = _proDBConProduction;
            }
}

How can I change the environment variable RUN TIME?
Using Login PAGE I am selecting Development or Production.
Based on that I have to use my ConnectionString device specifically.

"ConnectionStrings": {
"PROJECTNAME_Development": "SERVER_DETAILS",
"PROJECTNAME_Production": "SERVER_DETAILS",
}

Here are my LoginServices.CS file

public class LoginService: ILoginService
{
        private readonly string _connectionString = string.Empty;
        private readonly IConfiguration _configuration;
}
public LoginService (IConfiguration configuration)
{
        _configuration = configuration;
        //If ADMIN SELECT Development
        _connectionString = _configuration.GetConnectionString("PROJECTNAME_Development");
        //If ADMIN SELECT Production
        _connectionString = _configuration.GetConnectionString("PROJECTNAME_Production");
}

UPDATE 1
I tried this code. It works fine but is there any other way like at one location I make a change and Environment Variable value gets updated.

My Controller

 [HttpPost]
        public IActionResult Login(LoginModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.DevelopmentServer = "Development";
                    // model.DevelopmentServer = "Production";
                    ...
                     var Result = ILoginService.AdminLogin(model);  
  
 public LoginService(IConfiguration configuration, IOptions<ConnectionStringDetails> connectionStrings)
        {
            _proDBCon = connectionStrings.Value.PROJECTNAME_Development;
            _proDBConProduction = connectionStrings.Value.PROJECTNAME_Production;
        }

In the same file LoginService.cs File

public LoginInfo AdminLogin(LoginModel model)
{
 if (model.DevelopmentServer == "Development") {
                _connectionString = _proDBCon;
            }
            else {
                _connectionString = _proDBConProduction;
            }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2025-01-27 10:41:28

假设您需要不惜一切代价从UI中切换两个数据库,则您将无法使用容器/注射完全完成它。您必须使用从UI传递的值来确定应使用哪个数据库连接字符串。

public class LoginService {
   // ...
   private string GetConnectionString(string connectionStringNameFromUi){
       return _configuration.GetConnectionString
   }
}

同时,我将考虑部署两个应用程序的两个单独的实例,一个为开发人员配置,另一个为prod配置。这样,您将获得更多开箱即用的(注射容器和环境配置)。

Assuming that you need to switch between two databases from UI at any cost, you won't be able to complete it entirely using containers/injections. You have to use the value passed from UI to determine which database connection string should be used.

public class LoginService {
   // ...
   private string GetConnectionString(string connectionStringNameFromUi){
       return _configuration.GetConnectionString
   }
}

Meanwhile, I would consider deploying two separate instances of an application, one configured for dev and another for prod. In such way, you would get more out of the box (injections container and environment configuration).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文