NetCore 2.2 Change SQLConnection using environment variable Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?MySQL: @variable vs. variable. What's the difference?Get SqlConnection from DbConnectionC# SqlConnection ConnectionStringrails application controller method is undefined in childrenEntity Framework - Use Dynamic connection string with Oracle ProviderHow to communicate domain-specific errors from API to be parsed by client codeSignalR + SQLNotifications in .NetCoreMultiple connection to DbContext in EFCore and .net Core2.0Polly Circuit Breaker policy and HttpClient with ASP.NET Core APIADAL vs MSAL for .netcore api
Can a non-EU citizen traveling with me come with me through the EU passport line?
Is 1 ppb equal to 1 μg/kg?
Right-skewed distribution with mean equals to mode?
When to stop saving and start investing?
What makes black pepper strong or mild?
Can Pao de Queijo, and similar foods, be kosher for Passover?
Is there a "higher Segal conjecture"?
What does the "x" in "x86" represent?
List *all* the tuples!
How do I keep my slimes from escaping their pens?
How widely used is the term Treppenwitz? Is it something that most Germans know?
Is there a Spanish version of "dot your i's and cross your t's" that includes the letter 'ñ'?
Is it true that "carbohydrates are of no use for the basal metabolic need"?
What causes the vertical darker bands in my photo?
Should I call the interviewer directly, if HR aren't responding?
Why is "Consequences inflicted." not a sentence?
Does polymorph use a PC’s CR or its level?
How to motivate offshore teams and trust them to deliver?
Models of set theory where not every set can be linearly ordered
Why is black pepper both grey and black?
How to assign captions for two tables in LaTeX?
Do I really need recursive chmod to restrict access to a folder?
Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?
What is a Meta algorithm?
NetCore 2.2 Change SQLConnection using environment variable
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?MySQL: @variable vs. variable. What's the difference?Get SqlConnection from DbConnectionC# SqlConnection ConnectionStringrails application controller method is undefined in childrenEntity Framework - Use Dynamic connection string with Oracle ProviderHow to communicate domain-specific errors from API to be parsed by client codeSignalR + SQLNotifications in .NetCoreMultiple connection to DbContext in EFCore and .net Core2.0Polly Circuit Breaker policy and HttpClient with ASP.NET Core APIADAL vs MSAL for .netcore api
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Need assistance with dynamically changing the connectionstring supplied in an API for netcore 2.2.
This is my sample code
Public StartUp
_connectionString = cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName);
Public ConfigureServices
services.AddDbContextPool(s => s.UseSqlServer(_connectionString));
The user can change the Environment Variable at anytime and I need to have the API pick up this change dynamically each time it is changed.
I can't find anywhere to see that this is occurring except setting up something in the controller, but that just seems wrong.
Any thoughts.
sql controller connection-string asp.net-core-webapi
add a comment |
Need assistance with dynamically changing the connectionstring supplied in an API for netcore 2.2.
This is my sample code
Public StartUp
_connectionString = cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName);
Public ConfigureServices
services.AddDbContextPool(s => s.UseSqlServer(_connectionString));
The user can change the Environment Variable at anytime and I need to have the API pick up this change dynamically each time it is changed.
I can't find anywhere to see that this is occurring except setting up something in the controller, but that just seems wrong.
Any thoughts.
sql controller connection-string asp.net-core-webapi
add a comment |
Need assistance with dynamically changing the connectionstring supplied in an API for netcore 2.2.
This is my sample code
Public StartUp
_connectionString = cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName);
Public ConfigureServices
services.AddDbContextPool(s => s.UseSqlServer(_connectionString));
The user can change the Environment Variable at anytime and I need to have the API pick up this change dynamically each time it is changed.
I can't find anywhere to see that this is occurring except setting up something in the controller, but that just seems wrong.
Any thoughts.
sql controller connection-string asp.net-core-webapi
Need assistance with dynamically changing the connectionstring supplied in an API for netcore 2.2.
This is my sample code
Public StartUp
_connectionString = cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName);
Public ConfigureServices
services.AddDbContextPool(s => s.UseSqlServer(_connectionString));
The user can change the Environment Variable at anytime and I need to have the API pick up this change dynamically each time it is changed.
I can't find anywhere to see that this is occurring except setting up something in the controller, but that just seems wrong.
Any thoughts.
sql controller connection-string asp.net-core-webapi
sql controller connection-string asp.net-core-webapi
asked Mar 8 at 16:13
qwerty1906qwerty1906
254
254
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Well, first, you need to stop using connection pooling. This allows multiple instances of your context to use the same connection, which is not something you want if that connection needs to change on the fly.
Then, you need to get the connection string inside the DbContext registration, so that each time an instance is created, it gets the connection string anew. As you have it now, it's global.
services.AddDbContext<MyContext>((provider, options) =>
// get connection string
options.UseSqlServer(connectionString);
);
I'm not sure how to write this code for you as it's not clear what's going on with cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName). In general, here, provider is going to be an instance of IServiceProvider, and you want to use that to get the various services you need to make this call happen.
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
1
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
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%2f55066993%2fnetcore-2-2-change-sqlconnection-using-environment-variable%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
Well, first, you need to stop using connection pooling. This allows multiple instances of your context to use the same connection, which is not something you want if that connection needs to change on the fly.
Then, you need to get the connection string inside the DbContext registration, so that each time an instance is created, it gets the connection string anew. As you have it now, it's global.
services.AddDbContext<MyContext>((provider, options) =>
// get connection string
options.UseSqlServer(connectionString);
);
I'm not sure how to write this code for you as it's not clear what's going on with cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName). In general, here, provider is going to be an instance of IServiceProvider, and you want to use that to get the various services you need to make this call happen.
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
1
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
add a comment |
Well, first, you need to stop using connection pooling. This allows multiple instances of your context to use the same connection, which is not something you want if that connection needs to change on the fly.
Then, you need to get the connection string inside the DbContext registration, so that each time an instance is created, it gets the connection string anew. As you have it now, it's global.
services.AddDbContext<MyContext>((provider, options) =>
// get connection string
options.UseSqlServer(connectionString);
);
I'm not sure how to write this code for you as it's not clear what's going on with cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName). In general, here, provider is going to be an instance of IServiceProvider, and you want to use that to get the various services you need to make this call happen.
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
1
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
add a comment |
Well, first, you need to stop using connection pooling. This allows multiple instances of your context to use the same connection, which is not something you want if that connection needs to change on the fly.
Then, you need to get the connection string inside the DbContext registration, so that each time an instance is created, it gets the connection string anew. As you have it now, it's global.
services.AddDbContext<MyContext>((provider, options) =>
// get connection string
options.UseSqlServer(connectionString);
);
I'm not sure how to write this code for you as it's not clear what's going on with cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName). In general, here, provider is going to be an instance of IServiceProvider, and you want to use that to get the various services you need to make this call happen.
Well, first, you need to stop using connection pooling. This allows multiple instances of your context to use the same connection, which is not something you want if that connection needs to change on the fly.
Then, you need to get the connection string inside the DbContext registration, so that each time an instance is created, it gets the connection string anew. As you have it now, it's global.
services.AddDbContext<MyContext>((provider, options) =>
// get connection string
options.UseSqlServer(connectionString);
);
I'm not sure how to write this code for you as it's not clear what's going on with cryptography.GetProtectedValue("RandomName", dbEnvironmentVariableName). In general, here, provider is going to be an instance of IServiceProvider, and you want to use that to get the various services you need to make this call happen.
answered Mar 8 at 16:34
Chris PrattChris Pratt
161k22247311
161k22247311
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
1
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
add a comment |
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
1
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
Cryptography just returns an unencrypted connection string. Ok on the connection pool, that makes sense. On the other part, I don't think this still accomplishes changing the connection while the API is up and running. When a new instance is created then I agree, but when the API is already started it would not reset the connectionstring using this method. Hope that makes sense.
– qwerty1906
Mar 8 at 20:16
1
1
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
The context is request-scoped, so each request will get it's own instance, with the current connection string at that time. You are correct that once the instance is created, the connection string value is locked, and that is the case regardless. Config reload itself only happens on a per-request basis, so that's not actually an issue in practice. However, if you intend to change the connection string mid-request, then you are in fact out of luck.
– Chris Pratt
Mar 11 at 12:42
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
Thanks, that is what I was figuring, just wanted to make sure.
– qwerty1906
Mar 11 at 21:56
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%2f55066993%2fnetcore-2-2-change-sqlconnection-using-environment-variable%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