前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手把手教你写dotnet core(读取配置文件)

手把手教你写dotnet core(读取配置文件)

作者头像
李国宝
发布2020-01-02 17:05:42
2.1K0
发布2020-01-02 17:05:42
举报
文章被收录于专栏:编程技术向北,人生删除指南

dotnet core(读取配置文件)

第一篇:手把手教你写dotnet core(入门篇)

第二篇:手把手教你ASP.NET Core

今天我们来学习怎么读取dotnet core程序的配置文件.

一般dotnet core配置文件都位于项目目录下,名为appsettings.json

直接读文件

代码语言:javascript
复制
{
    "MySQLConnectionString": "server=mysql地址;port=端口号;database=数据库名字;uid=账号;pwd=密码;charset='utf-8';Allow User Variables=True;Connection Timeout=30;SslMode=None;",
    "RedisConnectionString": "redis数据库地址:端口,name=名字,keepAlive=1800,syncTimeout=10000,connectTimeout=360000,password=访问密码,ssl=False,abortConnect=False,responseTimeout=360000,defaultDatabase=1",
    "EmailAccount": "QQ邮箱账号",
    "EmailPassword": "QQ邮箱密码",
    "EmailSMTPDomain": "smtp.qq.com",
    "EmailSMTPPort": 587,
    "SenderAddress": "QQ邮箱账号",
    "ReceiverAddress": "QQ邮箱账号",
    "ReceiverName": "liguobao-test",
    "EncryptionConfigCIV": "加密向量,16个16进制数字",
    "EncryptionConfigCKEY": "加密秘钥,16个16进制数字",
    "ESURL":"http://127.0.0.1:9201/",
    "ESUserName":"",
    "ESPassword":"",
    "QQAPPID":"",
    "QQAPPKey":"",
    "QQAuthReturnURL":""
}

那么我们直接去读json然后序列化成对象是不是就可以了.

没毛病,确实是可以这样玩的. 代码大概是这样的…

代码语言:javascript
复制

public class AppSettings
{
    public string MySQLConnectionString {get;set;}

    public string RedisConnectionString {get;set;}

    // ....
}

public AppSettings JsonHelper(string jsonFilePath)
{
    _movieJsonFilePath = jsonFilePath;
    if (!File.Exists(jsonFilePath))
    {
        var pvFile = File.Create(jsonFilePath);
        pvFile.Flush();
        pvFile.Dispose();
        return;

    }
    using (var stream = new FileStream(jsonFilePath, FileMode.OpenOrCreate))
    {
        try
        {
            StreamReader sr = new StreamReader(stream);
            JsonSerializer serializer = new JsonSerializer
            {
                NullValueHandling = NullValueHandling.Ignore,
                Converters = { new JavaScriptDateTimeConverter() }
            };
            //构建Json.net的读取流  
            using (var reader = new JsonTextReader(sr))
            {
                return serializer.Deserialize<AppSettings>(reader);
            }

        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

直接读文件然后反序列化实在有点麻烦,有没有简单点的办法啊.

客官,只要给钱什么都有.

使用ConfigurationBuilder 读取

直接上代码:

代码语言:javascript
复制
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var mySQLConnectionString = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json").Build()["MySQLConnectionString"];

    // ConnectionStrings节点下的MySQLConnectionString
    // var mySQLConnectionString = new ConfigurationBuilder()
    //         .SetBasePath(Directory.GetCurrentDirectory())
    //     .AddJsonFile("appsettings.json").Build()["ConnectionStrings:MySQLConnectionString"];
}

嗯,就是这么简单粗暴,什么都不管…

辣鸡,还有没有更优雅点的方法啊.

哦,这样的要求么?那么我们上DI(自动注入)吧.

DI读取配置文件

代码语言:javascript
复制
//Startup.cs
public IConfiguration Configuration { get; }

public Startup(IHostingEnvironment env)
{
    //够赞
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    Configuration = builder.Build();
}

 public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    //将Configuration注入到APPConfiguration实例中
    services.AddOptions().Configure<AppSettings>(Configuration);
}

然后我们在Controller中使用构造函数注入的方式获取APPConfiguration实例

代码语言:javascript
复制
private AppSettings configuration;

public AccountController(IOptions<AppSettings> configurationOption)
{
    this.configuration = configurationOption.Value;
}

然后就可以愉快的使用了.

本文结束…

拜…

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • dotnet core(读取配置文件)
    • 直接读文件
      • 使用ConfigurationBuilder 读取
        • DI读取配置文件
        相关产品与服务
        文件存储
        文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档