diff --git a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs index ecfd01752..11bba1458 100644 --- a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs @@ -120,7 +120,7 @@ private static void AddApplicationIdentifier(IServiceCollection services) { services.AddSingleton(sp => { - var appIdentifier = GetApplicationId(); + string appIdentifier = GetApplicationId(); return new AppIdentifier(appIdentifier); }); } @@ -132,14 +132,10 @@ private static string GetApplicationId() var id = appMetadataJObject?.SelectToken("id")?.Value(); - if (id == null) - { - throw new KeyNotFoundException( - "Could not find id in applicationmetadata.json. Please ensure applicationmeta.json is well formed and contains a key for id." + return id + ?? throw new KeyNotFoundException( + "Could not find id in applicationmetadata.json. Please ensure the file is well formed and contains a key for `id`" ); - } - - return id; } /// @@ -154,9 +150,7 @@ public static void AddAppServices( IWebHostEnvironment env ) { - // Services for Altinn App services.TryAddTransient(); - AddValidationServices(services, configuration); services.TryAddTransient(); services.TryAddTransient(); services.TryAddSingleton(); @@ -175,13 +169,14 @@ IWebHostEnvironment env services.TryAddTransient(); services.TryAddTransient(); services.AddTransient(); + services.AddSingleton(); services.Configure(configuration.GetSection("PEPSettings")); services.Configure(configuration.GetSection("PlatformSettings")); services.Configure(configuration.GetSection("AccessTokenSettings")); services.Configure(configuration.GetSection(nameof(FrontEndSettings))); services.Configure(configuration.GetSection(nameof(PdfGeneratorSettings))); - services.AddSingleton(); + AddValidationServices(services, configuration); AddAppOptions(services); AddExternalApis(services); AddActionServices(services); @@ -209,32 +204,29 @@ private static void AddValidationServices(IServiceCollection services, IConfigur { services.AddTransient(); services.AddTransient(); - if (configuration.GetSection("AppSettings").Get()?.RequiredValidation == true) + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + var appSettings = configuration.GetSection("AppSettings").Get(); + if (appSettings?.RequiredValidation is true) { services.AddTransient(); } - if (configuration.GetSection("AppSettings").Get()?.ExpressionValidation == true) + if (appSettings?.ExpressionValidation is true) { services.AddTransient(); } - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); } /// /// Checks if a service is already added to the collection. /// - /// true if the services allready exists in the collection, otherwise false + /// true if the services already exists in the collection, otherwise false public static bool IsAdded(this IServiceCollection services, Type serviceType) { - if (services.Any(x => x.ServiceType == serviceType)) - { - return true; - } - - return false; + return services.Any(x => x.ServiceType == serviceType); } private static void AddEventServices(IServiceCollection services) @@ -243,7 +235,8 @@ private static void AddEventServices(IServiceCollection services) services.AddTransient(); services.TryAddSingleton(); - // The event subscription client depends uppon a maskinporten messagehandler beeing + // TODO: Event subs could be handled by the new automatic Maskinporten auth, once implemented. + // The event subscription client depends upon a Maskinporten message handler being // added to the client during setup. As of now this needs to be done in the apps // if subscription is to be added. This registration is to prevent the DI container // from failing for the apps not using event subscription. If you try to use @@ -317,18 +310,17 @@ private static void AddAppOptions(IServiceCollection services) private static void AddExternalApis(IServiceCollection services) { services.AddTransient(); - services.TryAddTransient(); } private static void AddProcessServices(IServiceCollection services) { + services.AddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddSingleton(); services.TryAddTransient(); services.TryAddTransient(); - services.AddTransient(); services.TryAddTransient(); services.AddTransient(); @@ -340,14 +332,14 @@ private static void AddProcessServices(IServiceCollection services) services.AddTransient(); services.AddTransient(); - //PROCESS TASKS + // Process tasks services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - //SERVICE TASKS + // Service tasks services.AddTransient(); services.AddTransient(); }