zoukankan      html  css  js  c++  java
  • 深入浅出Blazor webassembly之程序配置


    =================================
    launchsettings.json profiles 定义文件
    =================================
    文件位置: ropertieslaunchSettings.json, 该文件在 dotnet core 项目启动时会被自动加载, 不同的dotnet项目类型, 会有不同格式的launchsettings.json, 一旦项目类型确定后, 这个json文件的可配置项也就确定下来了, 所以我们不能在该json文件中增加配置项.

    对于 BlazorWebAssembly 类型的项目, 这个配置文件包含两个部分, 一个是 iisSettings 配置(不重要, 暂时忽略), 另一个是 profiles 配置. 默认 BlazorWebAssembly 提供了两个 profile, 我们也可以增加新的 profile, 比如增加 stg/dev/prod 几个环境的profile.

    但需要说明的是, 因为 launchsettings.json 格式是固定的, 这里仅仅是增加了多个stg/dev/prod profile 名, 我们不能直接在这里增加自定义的配置项.

    如何为 dotnet 程序指定 profile?
    在 dotnet run 命令行中加上  --launch-profile 参数, 即可指定要加载的 profile.
    dotnet run  --launch-profile <launch-profile>  

    可以在该 json 文件头指定其 schema 信息, 这样 vscode 就能提供自动补全编辑功能.
    "$schema":"http://json.schemastore.org/launchsettings.json"


    定义一个 blazorDemo1-dev profile:

        "blazorDemo1-dev": {
          "commandName": "Project",
          "dotnetRunMessages": "true",
          "launchBrowser": true,
          "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
          "applicationUrl": "http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "dev"
          }
        }

    需要特别说明的是:
    commandName 是指定 asp.net core 的启动模式, 取值应该设置为 Project,  但我估计对于blazor wasm 应该不重要
    ASPNETCORE_ENVIRONMENT 设定具体的env名, 这个env在 appsettings.json 文件中会使用到.
     
    启用 blazorDemo1-dev profile的命名:

    dotnet run  --launch-profile  blazorDemo1-dev

    =================================
    appsettings.json 应用程序自定义配置文件
    =================================
    launchSettings.json 不允许增加自定义配置项, 但 wwwrootappsettings.json 可以增加, 甚至可为不同的 env 定义不同的 appsettings.json 文件, 文件名规范是:  appsettings.{ASPNETCORE_ENVIRONMENT}.json

    以 blazorDemo1-dev profile 为例, 因为 ASPNETCORE_ENVIRONMENT 取值为 dev, 所以可增加一个 appsettings.dev.json 配置文件, 这样就相当于有两个配置文件, appsettings.json 和  appsettings.dev.json , 如果一个配置项在两个文件中都定义了, 自然以指定环境的配置文件为准.

    需要说明的是 appsettings.json 配置文件, 在浏览器端是完全可见的, 所以不能用来存储敏感信息.
    appsettings.json 文件内容:

    {
        "appid":"appid123",     //普通key value
        "aaa.a1":"default",     //构造多层 section , 代码上不认为是多层  
        "bbb":{                 //构造真实的多层 section       
            "b1":"b1value",
            "b2":"b2value"
        },
        "ccc:c1":"c1value",      //另一种构造真实的多层 section 的写法,完全等价于上面 bbb section  
        "ccc:c2":"c2value"   
    }

    Program.cs 文件读取配置的代码:

     public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
    
        builder.RootComponents.Add<App>("#app");
        builder.Services.AddScoped(sp => new HttpClient { 
            BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
    
        //读取 appsettings.json 文件的 aaa.a1 取值
        var a1=builder.Configuration["aaa.a1"]; 
        Console.WriteLine("aaa.a1="+a1);
        
        //多层section取值, 方法1: 使用Bind的方式先获取第一层 section, 然后使用 Dictionary<k,v> 来获取第二层 section值
        var dict=new Dictionary<string, string>();
        builder.Configuration.Bind("bbb",dict);   
        Console.WriteLine("bbb:b1="+dict["b1"]); 
        
        //多层section取值, 方法2: 使用冒号表达叶子节点, 即可直接获取叶子节点的值  
        var b1Another=builder.Configuration["bbb:b1"]; 
        Console.WriteLine("bbb:b1="+b1Another);
        
        await builder.Build().RunAsync();
    }

    =================================
    为应用程序增加基于内存的Configuration data
    =================================

    上面讲过 appsettings.json 会被自动下载到浏览器端, 所以绝对不能存敏感, 这里给出一个安全性稍微好一点的方法, 将配置项写死到C#代码中, 微软提供了MemoryConfigurationSource 类, 使用这个类设置配置项, 在读取的时候, 完全和 appsettings.json 一样, 非常方便.

    Program.cs 文件设置配置的代码:

    using Microsoft.Extensions.Configuration.Memory;
    
    public static async Task Main(string[] args)
    {
        //other code
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
    
        var dict2=new Dictionary<string, string>();
        dict2.add("xxx.x1":"x1value"); //包含xxx.x1一个层次section 
        dict2.add("yyy:y1":"y1value"); //包含yyy 和 y1两个层次section 
        dict2.add("yyy:y2":"y2value"); //包含yyy 和 y2两个层次section 
        var memoryCfg=new MemoryConfigurationSource(){InitialData=dict2};
        builder.Configuration.Add(memoryCfg);
        
        //other code
    }

    =================================
    在 razor 文件中使用依赖注入的方式获取 configuration data
    =================================

    前面示例都是在 Program.cs 中读取 appsettings 设置, 其实在 razor 文件中, 读取也非常方便, 注入一个 IConfiguration 对象.

    @using Microsoft.Extensions.Configuration
    @inject IConfiguration Configuration 
    
    public void getCfgValue()
    {
    
        //读取 appsettings.json 文件    
        var x1=Configuration["xxx.x1"]; 
        Console.WriteLine("xxx.x1="+x1);
        
        var y1=Configuration["yyy:y1"]; 
        Console.WriteLine("yyy:y1="+y1);    
     
    }
  • 相关阅读:
    Silverlight4实现三维企业网站
    (学)Lazarus 字符串压缩、解压缩
    (原)Oracel 函数返回 Decimal 丢失小数位问题
    (原)如何提高软件运行速度
    (转) ORA01033: ORACLE 正在初始化或关闭
    (学)正在写一个陌生行业的方案,努力ing
    (学)Telerik GridFoot 如何加合计
    (思)爱的路上千万里
    (学)Telerik RadGridView 中Column 数据字段绑定
    写在2011年伊始
  • 原文地址:https://www.cnblogs.com/harrychinese/p/blazor_appsettings.html
Copyright © 2011-2022 走看看