在asp网络核心控制台应用程序中阅读appsettings [英] Read appsettings in asp net core console application

查看:180
本文介绍了在asp网络核心控制台应用程序中阅读appsettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的控制台应用程序中阅读appsetting以及设置他的EntityFramework连接字符串。



我做了很多google,但没有找到任何单一解决方案甚至不在微软的文档中。



这是我的问题。


  1. 如何设置EntityFramework连接字符串?我的实体框架项目是分开的,对于我的MVC项目我做了如下代码。



< blockquote>

  string connectionString = Configuration.GetConnectionString(DefaultConnection); 
services.AddDbContext< MyDBContext>(option => option.UseSqlServer(connectionString,m => m.MigrationsAssembly(MyMVCDLL)));
services.Configure< AppSettings>(Configuration.GetSection(AppSettings));





  1. 阅读appsetting?

  2. 如何在控制面板中实现DI以获取应用程序?



<

解决方案

首先,不要保存敏感数据(登录,密码,API密钥)在 appsettings.json 中,因为您可能会不小心将其提交给Verison Control,因此可能导致凭据泄漏。为此,您必须使用用户密码工具进行开发,请参阅用户密码文档的详细信息。



其次,请阅读 Configuration.GetConnectionString(DefaultConnection); 方法的工具提示文档。它清楚地表明,GetConnectionString是一个


GetSection(ConnectionStrings)[name]


就是说,你的appsettings.json必须如下所示:

  {
...,
ConnectionStrings:
{
DefaultConnection:Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;
}
}

或使用用户秘密时:


dotnet user-secrets set ConnectionStrings:DefaultConnection Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;




更新



在控制台应用程序中使用它是完全相同的。配置包不是ASP.NET Core特有的,可以自己使用。



所需的软件包(取决于您要使用的软件包


Microsoft.Extensions.Configuration:1.0.0,
Microsoft.Extensions.Configuration.EnvironmentVariables:1.0.0,
Microsoft.Extensions.Configuration.FileExtensions: 1.0.0,
Microsoft.Extensions.Configuration.Json:1.0.0,
Microsoft.Extensions.Configuration.UserSecrets:1.0.0,


构建配置的代码与ASP.NET Core完全相同,而不是在启动中执行。 cs 您可以在主要方法中执行此操作:

  var builder = new ConfigurationBuilder()
.AddJsonFile(appsettings.json,可选:true,reloadOnChange:true)
//您不能使用环境特定的配置文件,如
// becuase IHostingEnvironment是一个ASP.NET Core特定的界面
//.AddJsonFile($\"a pp.ettings。{env.EnvironmentName} .json,可选:true)
.AddUserSecrets()
.AddEnvironmentVariables();


I'm trying to read the appsetting on my console application as well as to set he EntityFramework connection string.

I did a lot of google but did not found any single solution not even on Microsoft's documentation.

Here is the my questions.

  1. How to set the EntityFramework connection string?, my entity framework project is separate, for my MVC project I did this as below code.

string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<MyDBContext>(option =>option.UseSqlServer(connectionString, m => m.MigrationsAssembly("MyMVCDLL")));
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

  1. How to read the appsetting?
  2. How to implement the DI in console applciation to get the appsettings?

Can someone help me with this.

解决方案

First, don't save sensitive data (login, password, API keys) in appsettings.json, as you can accidentally commit it to Verison Control and hence risk your credentials leaking. For this you have to use User Secrets tool for development, see the User Secret documentation for details.

Second, read the tooltip documentation of Configuration.GetConnectionString("DefaultConnection"); method. It clearly states that `GetConnectionString is a

Shorthand for GetSection("ConnectionStrings")[name]

That being said, your appsettings.json has to look like this:

{
    ...,
    "ConnectionStrings":
    {
        "DefaultConnection" : "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"
    }
}

or when using user secrets:

dotnet user-secrets set ConnectionStrings:DefaultConnection Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Update

Using it in the console application is exactly the same. The configuration package isn't ASP.NET Core specific and can be used on its own.

The required packages are (depending on which of them you want to use

"Microsoft.Extensions.Configuration": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",

And the code to build the configuration is exactly the same as in ASP.NET Core. Just instead of doing it in Startup.cs you do it in the Main method:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    // You can't use environment specific configuration files like this
    // becuase IHostingEnvironment is an ASP.NET Core specific interface
    //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddUserSecrets()
    .AddEnvironmentVariables();

这篇关于在asp网络核心控制台应用程序中阅读appsettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆