Benutzer-Werkzeuge

Webseiten-Werkzeuge


Action disabled: revisions
net_core:appsettings.json_custom_config

Eigene Konfigurationsklasse für die appsettings.json

Erweitert man die appsettings.json , so möchte man natürlich bequem auf die Einstellungen zugreifen wie bei der Default-Konfiguration:

    public IConfiguration Configuration { get; }
 
....
 ... Configuration.GetConnectionString( "DatenbankKennung" );
...

appsetting.json

Die Konfigurationsdatei selbst:

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "InternalDatabase" : "Host=localhost;Database=DATABASENAME;username=DATABASEUSER;password=PASSWORD!!;"
  },
  "AllowedHosts": "*",
  "UserBackend":{
    "Openssl" : "/usr/bin/openssl",
    "CaPath" : "/var/deincaname/",
    "InternalBackend" : false,
    "Ads": {
      "Server" : "localhost",
      "BaseDn" : "local",
      "AllowUserCert" : "SomeAdGroup",
      "AllowEmailCert" : "AnotherAdGroup",
      "AllowCryptCert" : "AntherAnotherAdGroup:D"
    },
    "___comment1" : "dieser Parameter wird nicht mit aufgenommen ;)",
    "___comment2" : "2. Object innerhalb der UserBackend-Struktur....",
    "Database":{
      "Server" : "localhost",
      "DbName" : "dbname",
      "User" : "dbuser",
      "Password" : "dbpass"
    }
  }
}

UserBackend Klasse

UserBackend.cs
    public class UserBackend
    {
        public string Openssl { get; set; }
 
        public string CaPath { get; set; }
 
        public Ads Ads { get; set; }
 
        public Database Database { get; set; }
 
        public bool InternalBackend { get; set; }
 
        public bool CheckConfiguration( )
        {
            return false;
        }
    }

Ads und Database - Klassen

NeededClasses.cs
    public abstract class AllowCertQuery
    {
        public string AllowUserCert { get; set; }
        public string AllowEmailCert { get; set; }
        public string AllowCryptCert { get; set; }
    }
 
    public class Ads : AllowCertQuery
    {
        public string Server { get; set; }
        public string BaseDn { get; set; }
    }
 
    public class Database : AllowCertQuery
    {
        public string Server { get; set; }
        public string DbName { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
 
    }

Zusammenbau der Konfiguration in Startup.cs

So: Noch schnell die Konfiguration lesen und bereitstellen ;)

Startup.cs
public  UserBackend userBackend { get; private set; }
 
public void ConfigureServices( IServiceCollection services )
{
        //appsetting.json einlesen und parsen lassen :)
        var config = new ConfigurationBuilder( )
                .SetBasePath( AppDomain.CurrentDomain.BaseDirectory )
                .AddJsonFile( "appsettings.json" ).Build( );
 
        //Die Sektion UserBackend rausfummeln...
        var section = config.GetSection( nameof( SuperCA.Configuration.UserBackend ) );
 
        //Die Sektion mit json-parser in ein Objekt überführen ;) UserBackend
        userBackend = section.Get<UserBackend>( );
 
        //some stuff zBsp. die Konfiguration prüfen.. kommt natürlich immer FALSE
        if( !userBackend.CheckConfiguration( ) )
        {
                _logger.LogError("UserBackend-Service configuration has some errors");
        }
 
        //auch sehr angenehm ist, dass man gleich die neue Konfiguration für DI (Dependency Injection) bekannt macht
        services.AddSingleton<UserBackend>( userBackend );
 
}
net_core/appsettings.json_custom_config.txt · Zuletzt geändert: 2022/08/24 12:49 von raiser