How to merge multiple arrays from ASP.Net Core configuration files?
I would like dynamically load and register services in my application. To do that I need to be able to load configuration files from different projects in solution and merge values from them into single json array.
Unfortunately by default in ASP.Net Core configuration overrides values.
I register files with following code (part of Program.cs file):
public static IWebHostBuilder CreateWebHostBuilder(string args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostBuilderContext, configurationbuilder) =>
{
var env = webHostBuilderContext.HostingEnvironment;
configurationbuilder.SetBasePath(env.ContentRootPath);
configurationbuilder.AddJsonFile("appsettings.json", false, true);
var path = Path.Combine(env.ContentRootPath, "App_Config\Include");
foreach(var file in Directory.EnumerateFiles(path, "*.json",SearchOption.AllDirectories))
{
configurationbuilder.AddJsonFile(file, false, true);
}
configurationbuilder.AddEnvironmentVariables();
})
.UseStartup<Startup>();
The code searches for all files with *.json
extension inside App_ConfigInclude
directory and adds all of them to the configuration builder.
Structure of the files look following way:
{
"ServicesConfiguration": {
"Services": [
{
"AssemblyName": "ParsingEngine.ServicesConfigurator, ParsingEngine"
}
]
}
}
As you can see I have got main section ServicesConfiguration
then Services
array with objects which have one attribute AssemblyName
.
To read those values I use ServicesConfiguration
class with list:
public class ServicesConfiguration
{
public List<ServiceAssembly> Services { get; set; }
}
And that list uses ServiceAssembly
class:
public class ServiceAssembly
{
public string AssemblyName { get; set; }
}
To load that configuration I use IOptions
at constructor level (DI):
Microsoft.Extensions.Options.IOptions<ServicesConfiguration> servicesConfiguration,
And configuration seems to be loaded - but values from files are not merged but overridden by last found file.
Any ideas how to fix that?
c# asp.net-core
add a comment |
I would like dynamically load and register services in my application. To do that I need to be able to load configuration files from different projects in solution and merge values from them into single json array.
Unfortunately by default in ASP.Net Core configuration overrides values.
I register files with following code (part of Program.cs file):
public static IWebHostBuilder CreateWebHostBuilder(string args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostBuilderContext, configurationbuilder) =>
{
var env = webHostBuilderContext.HostingEnvironment;
configurationbuilder.SetBasePath(env.ContentRootPath);
configurationbuilder.AddJsonFile("appsettings.json", false, true);
var path = Path.Combine(env.ContentRootPath, "App_Config\Include");
foreach(var file in Directory.EnumerateFiles(path, "*.json",SearchOption.AllDirectories))
{
configurationbuilder.AddJsonFile(file, false, true);
}
configurationbuilder.AddEnvironmentVariables();
})
.UseStartup<Startup>();
The code searches for all files with *.json
extension inside App_ConfigInclude
directory and adds all of them to the configuration builder.
Structure of the files look following way:
{
"ServicesConfiguration": {
"Services": [
{
"AssemblyName": "ParsingEngine.ServicesConfigurator, ParsingEngine"
}
]
}
}
As you can see I have got main section ServicesConfiguration
then Services
array with objects which have one attribute AssemblyName
.
To read those values I use ServicesConfiguration
class with list:
public class ServicesConfiguration
{
public List<ServiceAssembly> Services { get; set; }
}
And that list uses ServiceAssembly
class:
public class ServiceAssembly
{
public string AssemblyName { get; set; }
}
To load that configuration I use IOptions
at constructor level (DI):
Microsoft.Extensions.Options.IOptions<ServicesConfiguration> servicesConfiguration,
And configuration seems to be loaded - but values from files are not merged but overridden by last found file.
Any ideas how to fix that?
c# asp.net-core
Well.. one way I think it could work (although not the prettiest, for sure) is: You could merge the jsons yourself, (get the file, parse it and put somewhere) do the merging, ending up with theList<ServiceAssembly> Services
you want, then serialize that again into a json file and callAddJson
passing it.
– jpgrassi
Dec 31 '18 at 9:44
Thinking better, you could even forget about adding json and using the "traditional" way. You can just read theJSON
files yourself, parse them into the class and just register that class as a singleton. This way you can do whatever you want with the JSON files and plus you avoid having to use theIOptions
pattern which most people (me included) dislike.
– jpgrassi
Dec 31 '18 at 10:02
add a comment |
I would like dynamically load and register services in my application. To do that I need to be able to load configuration files from different projects in solution and merge values from them into single json array.
Unfortunately by default in ASP.Net Core configuration overrides values.
I register files with following code (part of Program.cs file):
public static IWebHostBuilder CreateWebHostBuilder(string args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostBuilderContext, configurationbuilder) =>
{
var env = webHostBuilderContext.HostingEnvironment;
configurationbuilder.SetBasePath(env.ContentRootPath);
configurationbuilder.AddJsonFile("appsettings.json", false, true);
var path = Path.Combine(env.ContentRootPath, "App_Config\Include");
foreach(var file in Directory.EnumerateFiles(path, "*.json",SearchOption.AllDirectories))
{
configurationbuilder.AddJsonFile(file, false, true);
}
configurationbuilder.AddEnvironmentVariables();
})
.UseStartup<Startup>();
The code searches for all files with *.json
extension inside App_ConfigInclude
directory and adds all of them to the configuration builder.
Structure of the files look following way:
{
"ServicesConfiguration": {
"Services": [
{
"AssemblyName": "ParsingEngine.ServicesConfigurator, ParsingEngine"
}
]
}
}
As you can see I have got main section ServicesConfiguration
then Services
array with objects which have one attribute AssemblyName
.
To read those values I use ServicesConfiguration
class with list:
public class ServicesConfiguration
{
public List<ServiceAssembly> Services { get; set; }
}
And that list uses ServiceAssembly
class:
public class ServiceAssembly
{
public string AssemblyName { get; set; }
}
To load that configuration I use IOptions
at constructor level (DI):
Microsoft.Extensions.Options.IOptions<ServicesConfiguration> servicesConfiguration,
And configuration seems to be loaded - but values from files are not merged but overridden by last found file.
Any ideas how to fix that?
c# asp.net-core
I would like dynamically load and register services in my application. To do that I need to be able to load configuration files from different projects in solution and merge values from them into single json array.
Unfortunately by default in ASP.Net Core configuration overrides values.
I register files with following code (part of Program.cs file):
public static IWebHostBuilder CreateWebHostBuilder(string args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostBuilderContext, configurationbuilder) =>
{
var env = webHostBuilderContext.HostingEnvironment;
configurationbuilder.SetBasePath(env.ContentRootPath);
configurationbuilder.AddJsonFile("appsettings.json", false, true);
var path = Path.Combine(env.ContentRootPath, "App_Config\Include");
foreach(var file in Directory.EnumerateFiles(path, "*.json",SearchOption.AllDirectories))
{
configurationbuilder.AddJsonFile(file, false, true);
}
configurationbuilder.AddEnvironmentVariables();
})
.UseStartup<Startup>();
The code searches for all files with *.json
extension inside App_ConfigInclude
directory and adds all of them to the configuration builder.
Structure of the files look following way:
{
"ServicesConfiguration": {
"Services": [
{
"AssemblyName": "ParsingEngine.ServicesConfigurator, ParsingEngine"
}
]
}
}
As you can see I have got main section ServicesConfiguration
then Services
array with objects which have one attribute AssemblyName
.
To read those values I use ServicesConfiguration
class with list:
public class ServicesConfiguration
{
public List<ServiceAssembly> Services { get; set; }
}
And that list uses ServiceAssembly
class:
public class ServiceAssembly
{
public string AssemblyName { get; set; }
}
To load that configuration I use IOptions
at constructor level (DI):
Microsoft.Extensions.Options.IOptions<ServicesConfiguration> servicesConfiguration,
And configuration seems to be loaded - but values from files are not merged but overridden by last found file.
Any ideas how to fix that?
c# asp.net-core
c# asp.net-core
asked Dec 31 '18 at 8:16
Łukasz SkowrońskiŁukasz Skowroński
1337
1337
Well.. one way I think it could work (although not the prettiest, for sure) is: You could merge the jsons yourself, (get the file, parse it and put somewhere) do the merging, ending up with theList<ServiceAssembly> Services
you want, then serialize that again into a json file and callAddJson
passing it.
– jpgrassi
Dec 31 '18 at 9:44
Thinking better, you could even forget about adding json and using the "traditional" way. You can just read theJSON
files yourself, parse them into the class and just register that class as a singleton. This way you can do whatever you want with the JSON files and plus you avoid having to use theIOptions
pattern which most people (me included) dislike.
– jpgrassi
Dec 31 '18 at 10:02
add a comment |
Well.. one way I think it could work (although not the prettiest, for sure) is: You could merge the jsons yourself, (get the file, parse it and put somewhere) do the merging, ending up with theList<ServiceAssembly> Services
you want, then serialize that again into a json file and callAddJson
passing it.
– jpgrassi
Dec 31 '18 at 9:44
Thinking better, you could even forget about adding json and using the "traditional" way. You can just read theJSON
files yourself, parse them into the class and just register that class as a singleton. This way you can do whatever you want with the JSON files and plus you avoid having to use theIOptions
pattern which most people (me included) dislike.
– jpgrassi
Dec 31 '18 at 10:02
Well.. one way I think it could work (although not the prettiest, for sure) is: You could merge the jsons yourself, (get the file, parse it and put somewhere) do the merging, ending up with the
List<ServiceAssembly> Services
you want, then serialize that again into a json file and call AddJson
passing it.– jpgrassi
Dec 31 '18 at 9:44
Well.. one way I think it could work (although not the prettiest, for sure) is: You could merge the jsons yourself, (get the file, parse it and put somewhere) do the merging, ending up with the
List<ServiceAssembly> Services
you want, then serialize that again into a json file and call AddJson
passing it.– jpgrassi
Dec 31 '18 at 9:44
Thinking better, you could even forget about adding json and using the "traditional" way. You can just read the
JSON
files yourself, parse them into the class and just register that class as a singleton. This way you can do whatever you want with the JSON files and plus you avoid having to use the IOptions
pattern which most people (me included) dislike.– jpgrassi
Dec 31 '18 at 10:02
Thinking better, you could even forget about adding json and using the "traditional" way. You can just read the
JSON
files yourself, parse them into the class and just register that class as a singleton. This way you can do whatever you want with the JSON files and plus you avoid having to use the IOptions
pattern which most people (me included) dislike.– jpgrassi
Dec 31 '18 at 10:02
add a comment |
1 Answer
1
active
oldest
votes
So you have an idea on what I meant in my comments here's a potential answer
Since you have to load different "config" files from different projects and apply some merging logic to them, I would just avoid using the "default" configuration system to load the JSON
files into the app. Instead, I would just do it myself. So:
- Read and deserialize the JSON into a type and keep it on a list
- Go through the list containing all configs and apply your merging logic
- Register the single
ServicesConfiguration
as aSingleton
- Remove the code you had on your
Program.cs
to load the customJSON
files
Here's how you could do it:
ServicesRootConfiguration
(new class, to be able to deserialize the json)
public class ServicesRootConfiguration
{
public ServicesConfiguration ServicesConfiguration { get; set; }
}
Startup.cs
public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
_hostingEnvironment = env;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// other configuration omitted for brevity
// build your custom configuration from json files
var myCustomConfig = BuildCustomConfiguration(_hostingEnvironment);
// Register the configuration as a Singleton
services.AddSingleton(myCustomConfig);
}
private static ServicesConfiguration BuildCustomConfiguration(IHostingEnvironment env)
{
var allConfigs = new List<ServicesRootConfiguration>();
var path = Path.Combine(env.ContentRootPath, "App_Config");
foreach (var file in Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories))
{
var config = JsonConvert.DeserializeObject<ServicesRootConfiguration>(File.ReadAllText(file));
allConfigs.Add(config);
}
// do your logic to "merge" the each config into a single ServicesConfiguration
// here I simply select the AssemblyName from all files.
var mergedConfig = new ServicesConfiguration
{
Services = allConfigs.SelectMany(c => c.ServicesConfiguration.Services).ToList()
};
return mergedConfig;
}
}
Then in your Controller
just normally get the instance by DI.
public class HomeController : Controller
{
private readonly ServicesConfiguration _config;
public HomeController(ServicesConfiguration config)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
}
}
With this approach, you ended up with the same behavior as you would get from normally registering the IOptions
. But, you avoid having a dependency on it and having to use the uggly .Value
(urgh). Even better, you could register it as an Interface so it makes your life easier during testing/mocking.
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985133%2fhow-to-merge-multiple-arrays-from-asp-net-core-configuration-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So you have an idea on what I meant in my comments here's a potential answer
Since you have to load different "config" files from different projects and apply some merging logic to them, I would just avoid using the "default" configuration system to load the JSON
files into the app. Instead, I would just do it myself. So:
- Read and deserialize the JSON into a type and keep it on a list
- Go through the list containing all configs and apply your merging logic
- Register the single
ServicesConfiguration
as aSingleton
- Remove the code you had on your
Program.cs
to load the customJSON
files
Here's how you could do it:
ServicesRootConfiguration
(new class, to be able to deserialize the json)
public class ServicesRootConfiguration
{
public ServicesConfiguration ServicesConfiguration { get; set; }
}
Startup.cs
public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
_hostingEnvironment = env;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// other configuration omitted for brevity
// build your custom configuration from json files
var myCustomConfig = BuildCustomConfiguration(_hostingEnvironment);
// Register the configuration as a Singleton
services.AddSingleton(myCustomConfig);
}
private static ServicesConfiguration BuildCustomConfiguration(IHostingEnvironment env)
{
var allConfigs = new List<ServicesRootConfiguration>();
var path = Path.Combine(env.ContentRootPath, "App_Config");
foreach (var file in Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories))
{
var config = JsonConvert.DeserializeObject<ServicesRootConfiguration>(File.ReadAllText(file));
allConfigs.Add(config);
}
// do your logic to "merge" the each config into a single ServicesConfiguration
// here I simply select the AssemblyName from all files.
var mergedConfig = new ServicesConfiguration
{
Services = allConfigs.SelectMany(c => c.ServicesConfiguration.Services).ToList()
};
return mergedConfig;
}
}
Then in your Controller
just normally get the instance by DI.
public class HomeController : Controller
{
private readonly ServicesConfiguration _config;
public HomeController(ServicesConfiguration config)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
}
}
With this approach, you ended up with the same behavior as you would get from normally registering the IOptions
. But, you avoid having a dependency on it and having to use the uggly .Value
(urgh). Even better, you could register it as an Interface so it makes your life easier during testing/mocking.
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
add a comment |
So you have an idea on what I meant in my comments here's a potential answer
Since you have to load different "config" files from different projects and apply some merging logic to them, I would just avoid using the "default" configuration system to load the JSON
files into the app. Instead, I would just do it myself. So:
- Read and deserialize the JSON into a type and keep it on a list
- Go through the list containing all configs and apply your merging logic
- Register the single
ServicesConfiguration
as aSingleton
- Remove the code you had on your
Program.cs
to load the customJSON
files
Here's how you could do it:
ServicesRootConfiguration
(new class, to be able to deserialize the json)
public class ServicesRootConfiguration
{
public ServicesConfiguration ServicesConfiguration { get; set; }
}
Startup.cs
public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
_hostingEnvironment = env;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// other configuration omitted for brevity
// build your custom configuration from json files
var myCustomConfig = BuildCustomConfiguration(_hostingEnvironment);
// Register the configuration as a Singleton
services.AddSingleton(myCustomConfig);
}
private static ServicesConfiguration BuildCustomConfiguration(IHostingEnvironment env)
{
var allConfigs = new List<ServicesRootConfiguration>();
var path = Path.Combine(env.ContentRootPath, "App_Config");
foreach (var file in Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories))
{
var config = JsonConvert.DeserializeObject<ServicesRootConfiguration>(File.ReadAllText(file));
allConfigs.Add(config);
}
// do your logic to "merge" the each config into a single ServicesConfiguration
// here I simply select the AssemblyName from all files.
var mergedConfig = new ServicesConfiguration
{
Services = allConfigs.SelectMany(c => c.ServicesConfiguration.Services).ToList()
};
return mergedConfig;
}
}
Then in your Controller
just normally get the instance by DI.
public class HomeController : Controller
{
private readonly ServicesConfiguration _config;
public HomeController(ServicesConfiguration config)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
}
}
With this approach, you ended up with the same behavior as you would get from normally registering the IOptions
. But, you avoid having a dependency on it and having to use the uggly .Value
(urgh). Even better, you could register it as an Interface so it makes your life easier during testing/mocking.
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
add a comment |
So you have an idea on what I meant in my comments here's a potential answer
Since you have to load different "config" files from different projects and apply some merging logic to them, I would just avoid using the "default" configuration system to load the JSON
files into the app. Instead, I would just do it myself. So:
- Read and deserialize the JSON into a type and keep it on a list
- Go through the list containing all configs and apply your merging logic
- Register the single
ServicesConfiguration
as aSingleton
- Remove the code you had on your
Program.cs
to load the customJSON
files
Here's how you could do it:
ServicesRootConfiguration
(new class, to be able to deserialize the json)
public class ServicesRootConfiguration
{
public ServicesConfiguration ServicesConfiguration { get; set; }
}
Startup.cs
public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
_hostingEnvironment = env;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// other configuration omitted for brevity
// build your custom configuration from json files
var myCustomConfig = BuildCustomConfiguration(_hostingEnvironment);
// Register the configuration as a Singleton
services.AddSingleton(myCustomConfig);
}
private static ServicesConfiguration BuildCustomConfiguration(IHostingEnvironment env)
{
var allConfigs = new List<ServicesRootConfiguration>();
var path = Path.Combine(env.ContentRootPath, "App_Config");
foreach (var file in Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories))
{
var config = JsonConvert.DeserializeObject<ServicesRootConfiguration>(File.ReadAllText(file));
allConfigs.Add(config);
}
// do your logic to "merge" the each config into a single ServicesConfiguration
// here I simply select the AssemblyName from all files.
var mergedConfig = new ServicesConfiguration
{
Services = allConfigs.SelectMany(c => c.ServicesConfiguration.Services).ToList()
};
return mergedConfig;
}
}
Then in your Controller
just normally get the instance by DI.
public class HomeController : Controller
{
private readonly ServicesConfiguration _config;
public HomeController(ServicesConfiguration config)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
}
}
With this approach, you ended up with the same behavior as you would get from normally registering the IOptions
. But, you avoid having a dependency on it and having to use the uggly .Value
(urgh). Even better, you could register it as an Interface so it makes your life easier during testing/mocking.
So you have an idea on what I meant in my comments here's a potential answer
Since you have to load different "config" files from different projects and apply some merging logic to them, I would just avoid using the "default" configuration system to load the JSON
files into the app. Instead, I would just do it myself. So:
- Read and deserialize the JSON into a type and keep it on a list
- Go through the list containing all configs and apply your merging logic
- Register the single
ServicesConfiguration
as aSingleton
- Remove the code you had on your
Program.cs
to load the customJSON
files
Here's how you could do it:
ServicesRootConfiguration
(new class, to be able to deserialize the json)
public class ServicesRootConfiguration
{
public ServicesConfiguration ServicesConfiguration { get; set; }
}
Startup.cs
public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
_hostingEnvironment = env;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// other configuration omitted for brevity
// build your custom configuration from json files
var myCustomConfig = BuildCustomConfiguration(_hostingEnvironment);
// Register the configuration as a Singleton
services.AddSingleton(myCustomConfig);
}
private static ServicesConfiguration BuildCustomConfiguration(IHostingEnvironment env)
{
var allConfigs = new List<ServicesRootConfiguration>();
var path = Path.Combine(env.ContentRootPath, "App_Config");
foreach (var file in Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories))
{
var config = JsonConvert.DeserializeObject<ServicesRootConfiguration>(File.ReadAllText(file));
allConfigs.Add(config);
}
// do your logic to "merge" the each config into a single ServicesConfiguration
// here I simply select the AssemblyName from all files.
var mergedConfig = new ServicesConfiguration
{
Services = allConfigs.SelectMany(c => c.ServicesConfiguration.Services).ToList()
};
return mergedConfig;
}
}
Then in your Controller
just normally get the instance by DI.
public class HomeController : Controller
{
private readonly ServicesConfiguration _config;
public HomeController(ServicesConfiguration config)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
}
}
With this approach, you ended up with the same behavior as you would get from normally registering the IOptions
. But, you avoid having a dependency on it and having to use the uggly .Value
(urgh). Even better, you could register it as an Interface so it makes your life easier during testing/mocking.
answered Dec 31 '18 at 10:45
jpgrassijpgrassi
2,49421933
2,49421933
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
add a comment |
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
Looks good, thanks!
– Łukasz Skowroński
Dec 31 '18 at 10:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985133%2fhow-to-merge-multiple-arrays-from-asp-net-core-configuration-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well.. one way I think it could work (although not the prettiest, for sure) is: You could merge the jsons yourself, (get the file, parse it and put somewhere) do the merging, ending up with the
List<ServiceAssembly> Services
you want, then serialize that again into a json file and callAddJson
passing it.– jpgrassi
Dec 31 '18 at 9:44
Thinking better, you could even forget about adding json and using the "traditional" way. You can just read the
JSON
files yourself, parse them into the class and just register that class as a singleton. This way you can do whatever you want with the JSON files and plus you avoid having to use theIOptions
pattern which most people (me included) dislike.– jpgrassi
Dec 31 '18 at 10:02