services.Configure() or services.AddSingleton().Get()?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
As known there are two ways to get option classes in ASP.NET Core 2:
Using
services.Configure<>()
like this:
services.AddOption();
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
or using
services.AddSingleton(Configuration.Get())
like this:
services.AddSingleton(Configuration.GetSection("applicationSettings")
.Get<ApplicationOptions>());
But what advantages or disadvantages do these different approaches have?
c# asp.net-core configuration
add a comment |
As known there are two ways to get option classes in ASP.NET Core 2:
Using
services.Configure<>()
like this:
services.AddOption();
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
or using
services.AddSingleton(Configuration.Get())
like this:
services.AddSingleton(Configuration.GetSection("applicationSettings")
.Get<ApplicationOptions>());
But what advantages or disadvantages do these different approaches have?
c# asp.net-core configuration
Read this and decide which you prefer simpleinjector.readthedocs.io/en/latest/…
– Nkosi
Nov 22 '18 at 9:41
add a comment |
As known there are two ways to get option classes in ASP.NET Core 2:
Using
services.Configure<>()
like this:
services.AddOption();
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
or using
services.AddSingleton(Configuration.Get())
like this:
services.AddSingleton(Configuration.GetSection("applicationSettings")
.Get<ApplicationOptions>());
But what advantages or disadvantages do these different approaches have?
c# asp.net-core configuration
As known there are two ways to get option classes in ASP.NET Core 2:
Using
services.Configure<>()
like this:
services.AddOption();
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
or using
services.AddSingleton(Configuration.Get())
like this:
services.AddSingleton(Configuration.GetSection("applicationSettings")
.Get<ApplicationOptions>());
But what advantages or disadvantages do these different approaches have?
c# asp.net-core configuration
c# asp.net-core configuration
edited Nov 22 '18 at 13:13
poke
218k46339402
218k46339402
asked Nov 22 '18 at 5:47
ShefardPTShefardPT
142
142
Read this and decide which you prefer simpleinjector.readthedocs.io/en/latest/…
– Nkosi
Nov 22 '18 at 9:41
add a comment |
Read this and decide which you prefer simpleinjector.readthedocs.io/en/latest/…
– Nkosi
Nov 22 '18 at 9:41
Read this and decide which you prefer simpleinjector.readthedocs.io/en/latest/…
– Nkosi
Nov 22 '18 at 9:41
Read this and decide which you prefer simpleinjector.readthedocs.io/en/latest/…
– Nkosi
Nov 22 '18 at 9:41
add a comment |
2 Answers
2
active
oldest
votes
Short answer : The 1st way adds an Options
, and the 2nd way registers a plain singleton service.
I always prefer Options Pattern if possible.
Behind the scenes , the Configure<TOptions>()
will invoke services.AddSingleton<>()
to register a singleton service to configure options. However, To configure options, we should always use the 1st way. As the Configure<TOptions>(config)
(and all kind of other configure<>()
methods) will do all the heavy-lifting for us. :
For example, if we would like to store two different instance for the same type, how can we do that with a plain singleton service ? In fact, that' exactly what named options does.
Also, it hard to auto reload a plain singleton service according to a file change to
applicationSettings.json
.
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
add a comment |
Using Configure<ApplicationOptions>
allows the options pattern. The options pattern is a nice way to configure things using various configuration sources. In your example, you are configuring the ApplicationOptions
using a Microsoft.Extensions.Configuration source. But you can also configure it through other sources at the same time:
// configure using configuration
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
// then apply a configuration function
services.Configure<ApplicationOptions>(options =>
{
// overwrite previous values
options.Foo = "bar";
});
There are a few other ways to adjust the configuration as well, for example using post-configures which allows you to easily compose things that utilize options but may need to establish certain defaults or fallbacks.
Option objects will be configured at the time they are used, so when you call services.Configure()
, there is actually nothing being configured at that time. Instead, configurations are registered with the DI container. And then, when the options are resolved, all configurations for a certain type will be invoked (which allows for composition). This allows options to also support updating configuration; so when you update your appsettings.json
at run-time, options are able to receive the updated values.
In order to consume options, you need to inject IOptions<ApplicationOptions>
(or IOptionsSnapshot<ApplicationOptions>
if you need updating options). This is a wrapper around the options object which will invoke the options pattern.
On the other hand, calling AddSingleton<ApplicationOptions>
just registers a singleton instance as a fixed value. So what gets registered with the DI provider is whatever value Configuration.GetSection("applicationSettings").Get<ApplicationOptions>()
returns at that exact moment.
This has the benefit that you do not need to use the options pattern; instead of having to inject IOptions<ApplicationOptions>
into your types, you can just depend on ApplicationOptions
directly. So you don’t take a dependency on the Options framework. This is good for independent libraries that want to be used in different scenarios where the options pattern might not be available by default.
However, since this registers a fixed instance, you are also limited to those exact values. You cannot have those values update later when the configuration source is changed, and also cannot use that one configuration source in combination with other configurations.
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%2f53424593%2fservices-configure-or-services-addsingleton-get%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Short answer : The 1st way adds an Options
, and the 2nd way registers a plain singleton service.
I always prefer Options Pattern if possible.
Behind the scenes , the Configure<TOptions>()
will invoke services.AddSingleton<>()
to register a singleton service to configure options. However, To configure options, we should always use the 1st way. As the Configure<TOptions>(config)
(and all kind of other configure<>()
methods) will do all the heavy-lifting for us. :
For example, if we would like to store two different instance for the same type, how can we do that with a plain singleton service ? In fact, that' exactly what named options does.
Also, it hard to auto reload a plain singleton service according to a file change to
applicationSettings.json
.
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
add a comment |
Short answer : The 1st way adds an Options
, and the 2nd way registers a plain singleton service.
I always prefer Options Pattern if possible.
Behind the scenes , the Configure<TOptions>()
will invoke services.AddSingleton<>()
to register a singleton service to configure options. However, To configure options, we should always use the 1st way. As the Configure<TOptions>(config)
(and all kind of other configure<>()
methods) will do all the heavy-lifting for us. :
For example, if we would like to store two different instance for the same type, how can we do that with a plain singleton service ? In fact, that' exactly what named options does.
Also, it hard to auto reload a plain singleton service according to a file change to
applicationSettings.json
.
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
add a comment |
Short answer : The 1st way adds an Options
, and the 2nd way registers a plain singleton service.
I always prefer Options Pattern if possible.
Behind the scenes , the Configure<TOptions>()
will invoke services.AddSingleton<>()
to register a singleton service to configure options. However, To configure options, we should always use the 1st way. As the Configure<TOptions>(config)
(and all kind of other configure<>()
methods) will do all the heavy-lifting for us. :
For example, if we would like to store two different instance for the same type, how can we do that with a plain singleton service ? In fact, that' exactly what named options does.
Also, it hard to auto reload a plain singleton service according to a file change to
applicationSettings.json
.
Short answer : The 1st way adds an Options
, and the 2nd way registers a plain singleton service.
I always prefer Options Pattern if possible.
Behind the scenes , the Configure<TOptions>()
will invoke services.AddSingleton<>()
to register a singleton service to configure options. However, To configure options, we should always use the 1st way. As the Configure<TOptions>(config)
(and all kind of other configure<>()
methods) will do all the heavy-lifting for us. :
For example, if we would like to store two different instance for the same type, how can we do that with a plain singleton service ? In fact, that' exactly what named options does.
Also, it hard to auto reload a plain singleton service according to a file change to
applicationSettings.json
.
answered Nov 22 '18 at 9:56
itminusitminus
4,5931424
4,5931424
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
add a comment |
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
Thanks a lot for the quite comprehesive answer!
– ShefardPT
Nov 22 '18 at 10:26
add a comment |
Using Configure<ApplicationOptions>
allows the options pattern. The options pattern is a nice way to configure things using various configuration sources. In your example, you are configuring the ApplicationOptions
using a Microsoft.Extensions.Configuration source. But you can also configure it through other sources at the same time:
// configure using configuration
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
// then apply a configuration function
services.Configure<ApplicationOptions>(options =>
{
// overwrite previous values
options.Foo = "bar";
});
There are a few other ways to adjust the configuration as well, for example using post-configures which allows you to easily compose things that utilize options but may need to establish certain defaults or fallbacks.
Option objects will be configured at the time they are used, so when you call services.Configure()
, there is actually nothing being configured at that time. Instead, configurations are registered with the DI container. And then, when the options are resolved, all configurations for a certain type will be invoked (which allows for composition). This allows options to also support updating configuration; so when you update your appsettings.json
at run-time, options are able to receive the updated values.
In order to consume options, you need to inject IOptions<ApplicationOptions>
(or IOptionsSnapshot<ApplicationOptions>
if you need updating options). This is a wrapper around the options object which will invoke the options pattern.
On the other hand, calling AddSingleton<ApplicationOptions>
just registers a singleton instance as a fixed value. So what gets registered with the DI provider is whatever value Configuration.GetSection("applicationSettings").Get<ApplicationOptions>()
returns at that exact moment.
This has the benefit that you do not need to use the options pattern; instead of having to inject IOptions<ApplicationOptions>
into your types, you can just depend on ApplicationOptions
directly. So you don’t take a dependency on the Options framework. This is good for independent libraries that want to be used in different scenarios where the options pattern might not be available by default.
However, since this registers a fixed instance, you are also limited to those exact values. You cannot have those values update later when the configuration source is changed, and also cannot use that one configuration source in combination with other configurations.
add a comment |
Using Configure<ApplicationOptions>
allows the options pattern. The options pattern is a nice way to configure things using various configuration sources. In your example, you are configuring the ApplicationOptions
using a Microsoft.Extensions.Configuration source. But you can also configure it through other sources at the same time:
// configure using configuration
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
// then apply a configuration function
services.Configure<ApplicationOptions>(options =>
{
// overwrite previous values
options.Foo = "bar";
});
There are a few other ways to adjust the configuration as well, for example using post-configures which allows you to easily compose things that utilize options but may need to establish certain defaults or fallbacks.
Option objects will be configured at the time they are used, so when you call services.Configure()
, there is actually nothing being configured at that time. Instead, configurations are registered with the DI container. And then, when the options are resolved, all configurations for a certain type will be invoked (which allows for composition). This allows options to also support updating configuration; so when you update your appsettings.json
at run-time, options are able to receive the updated values.
In order to consume options, you need to inject IOptions<ApplicationOptions>
(or IOptionsSnapshot<ApplicationOptions>
if you need updating options). This is a wrapper around the options object which will invoke the options pattern.
On the other hand, calling AddSingleton<ApplicationOptions>
just registers a singleton instance as a fixed value. So what gets registered with the DI provider is whatever value Configuration.GetSection("applicationSettings").Get<ApplicationOptions>()
returns at that exact moment.
This has the benefit that you do not need to use the options pattern; instead of having to inject IOptions<ApplicationOptions>
into your types, you can just depend on ApplicationOptions
directly. So you don’t take a dependency on the Options framework. This is good for independent libraries that want to be used in different scenarios where the options pattern might not be available by default.
However, since this registers a fixed instance, you are also limited to those exact values. You cannot have those values update later when the configuration source is changed, and also cannot use that one configuration source in combination with other configurations.
add a comment |
Using Configure<ApplicationOptions>
allows the options pattern. The options pattern is a nice way to configure things using various configuration sources. In your example, you are configuring the ApplicationOptions
using a Microsoft.Extensions.Configuration source. But you can also configure it through other sources at the same time:
// configure using configuration
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
// then apply a configuration function
services.Configure<ApplicationOptions>(options =>
{
// overwrite previous values
options.Foo = "bar";
});
There are a few other ways to adjust the configuration as well, for example using post-configures which allows you to easily compose things that utilize options but may need to establish certain defaults or fallbacks.
Option objects will be configured at the time they are used, so when you call services.Configure()
, there is actually nothing being configured at that time. Instead, configurations are registered with the DI container. And then, when the options are resolved, all configurations for a certain type will be invoked (which allows for composition). This allows options to also support updating configuration; so when you update your appsettings.json
at run-time, options are able to receive the updated values.
In order to consume options, you need to inject IOptions<ApplicationOptions>
(or IOptionsSnapshot<ApplicationOptions>
if you need updating options). This is a wrapper around the options object which will invoke the options pattern.
On the other hand, calling AddSingleton<ApplicationOptions>
just registers a singleton instance as a fixed value. So what gets registered with the DI provider is whatever value Configuration.GetSection("applicationSettings").Get<ApplicationOptions>()
returns at that exact moment.
This has the benefit that you do not need to use the options pattern; instead of having to inject IOptions<ApplicationOptions>
into your types, you can just depend on ApplicationOptions
directly. So you don’t take a dependency on the Options framework. This is good for independent libraries that want to be used in different scenarios where the options pattern might not be available by default.
However, since this registers a fixed instance, you are also limited to those exact values. You cannot have those values update later when the configuration source is changed, and also cannot use that one configuration source in combination with other configurations.
Using Configure<ApplicationOptions>
allows the options pattern. The options pattern is a nice way to configure things using various configuration sources. In your example, you are configuring the ApplicationOptions
using a Microsoft.Extensions.Configuration source. But you can also configure it through other sources at the same time:
// configure using configuration
services.Configure<ApplicationOptions>(Configuration.GetSection("applicationSettings"));
// then apply a configuration function
services.Configure<ApplicationOptions>(options =>
{
// overwrite previous values
options.Foo = "bar";
});
There are a few other ways to adjust the configuration as well, for example using post-configures which allows you to easily compose things that utilize options but may need to establish certain defaults or fallbacks.
Option objects will be configured at the time they are used, so when you call services.Configure()
, there is actually nothing being configured at that time. Instead, configurations are registered with the DI container. And then, when the options are resolved, all configurations for a certain type will be invoked (which allows for composition). This allows options to also support updating configuration; so when you update your appsettings.json
at run-time, options are able to receive the updated values.
In order to consume options, you need to inject IOptions<ApplicationOptions>
(or IOptionsSnapshot<ApplicationOptions>
if you need updating options). This is a wrapper around the options object which will invoke the options pattern.
On the other hand, calling AddSingleton<ApplicationOptions>
just registers a singleton instance as a fixed value. So what gets registered with the DI provider is whatever value Configuration.GetSection("applicationSettings").Get<ApplicationOptions>()
returns at that exact moment.
This has the benefit that you do not need to use the options pattern; instead of having to inject IOptions<ApplicationOptions>
into your types, you can just depend on ApplicationOptions
directly. So you don’t take a dependency on the Options framework. This is good for independent libraries that want to be used in different scenarios where the options pattern might not be available by default.
However, since this registers a fixed instance, you are also limited to those exact values. You cannot have those values update later when the configuration source is changed, and also cannot use that one configuration source in combination with other configurations.
answered Nov 22 '18 at 13:11
pokepoke
218k46339402
218k46339402
add a comment |
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%2f53424593%2fservices-configure-or-services-addsingleton-get%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
Read this and decide which you prefer simpleinjector.readthedocs.io/en/latest/…
– Nkosi
Nov 22 '18 at 9:41