Outils pour utilisateurs

Outils du site


web:asp.net:core:configuration

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
web:asp.net:core:configuration [2017/10/31 21:08] sgariepyweb:asp.net:core:configuration [2023/11/17 20:09] (Version actuelle) – [Providers] sgariepy
Ligne 4: Ligne 4:
  
  
 +====== Exemple simple ======
  
 +
 +Dans le constructeur de ''Startup.cs'':
 +
 +<code csharp>
 +var builder = new ConfigurationBuilder()
 +    .SetBasePath(env.ContentRootPath)
 +    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
 +    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
 +    .AddEnvironmentVariables();
 +this.Configuration = builder.Build();
 +</code>
 +
 +Le fichier ''appsettings.json'' :
 +
 +<code csharp>
 +{
 +  "Options": [
 +    "option1",
 +    "option2",
 +    "option3"
 +  ]
 +}
 +</code>
 +
 +
 +La classe ''MyOptions'' :
 +
 +<code csharp>
 +public class MyOptions
 +{
 +    public string[] Options { get; set; }
 +}
 +</code>
 +
 +
 +Dans le fichier ''Startup.cs'', méthode ''ConfigureServices'':
 +
 +<code csharp>
 +MyOptions myOptions = new MyOptions();
 +
 +services.Configure<MyOptions>(this.Configuration);
 +this.Configuration.Bind(myOptions);
 +</code>
 +
 +Autre exemple:
 +
 +  {
 +    "Section": {
 +      "MyOptions": [ "option1", "option2" ]
 +    }
 +  }
 +
 +
 +<code csharp>
 +MyOptions myOptions = new MyOptions();
 +this.Configuration.GetSection("Section").Bind(myOptions);
 +</code>
 +
 +Autre exemple:
 +
 +  {
 +    "MyOptions": {
 +      "Options": [ "option1", "option2" ]
 +    }
 +  }
 +
 +
 +<code csharp>
 +services.AddOptions();
 +
 +services.Configure<MyOptions>(this.Configuration.GetSection("MyOptions"));
 +</code>
 +
 +Dans une classe consommatrice, on peut faire :
 +
 +
 +<code csharp>
 +public class MyClass
 +{
 +    private readonly string[] _myOptions;
 +
 +    public MyClass(IOptions<MyOptions> myOptions)
 +    {
 +        this._options = myOptions.Value.Options;
 +    }
 +}
 +</code>
 +
 +
 +====== Providers ======
 +
 +
 +^ Provider  ^ Exemple  ^ Notes ^
 +| appsettings.json  | ''{ "key": "value" }''  |   |
 +| appsettings.{ENVIRONMENT}.json  | ''{ "key": "other value" }''  |   |
 +| User Secrets (Developement) | ''dotnet user-secrets set "key" "development value"'' | |
 +| Environment variables  | Powershell: ''setx key "environment value"'', bash: ''export key="environment value"''  | | 
 +| Command-line arguments  | ''dotnet run --key "important value"''  |  | 
 +
 +
 +L'environnement est lu de ''DOTNET_ENVIRONMENT'' ou ''ASPNETCORE_ENVIRONMENT''.
 +
 +
 +
 +Source: [[https://stenbrinke.nl/blog/configuration-and-secret-management-in-dotnet/|Everything you need to know about configuration and secret management in .NET]]
  
  
web/asp.net/core/configuration.1509480530.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)