C
C#2mo ago
M!RFAN

.Net 8 Web Api Config

Deployed webapi in IIS. Previously using .net 4.5.* where changing config in application settings in UI and restarting for dynamic change. Now in .Net 8 using Environment.GetenvironmentVariable, on publishing generated web.config file but adding values doesn't reflect and added values to pool also none but when added to system level it works but it takes time to recognise. Tried ChatGPT and older relevant question here and none working can someone point me in the best direction? TIA
25 Replies
Gela
Gela2mo ago
Could you elaborate? I didn't understand anything
M!RFAN
M!RFANOP2mo ago
In my .net 8 webapi, I'm using Environment.GetEnvitonmentVariable to get some env values While on development those values are stored in launchsettings.json but now after publishing to IIS, how to set those environment variables
Insire
Insire2mo ago
Stack Overflow
Publish to IIS, setting Environment Variable
Reading these two questions/answers I was able to run an Asp.net 5 app on IIS 8.5 server. Asp.net vNext early beta publish to IIS in windows server How to configure an MVC6 app to work on IIS? The
M!RFAN
M!RFANOP2mo ago
Tried But it doesn't read from there
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
but in IIS using application settings I can dynamically set config values and connection strings while using.net 4.5.*. So is there any way to do like that in ,net 8
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
More like I can't understand the docs. Sorry
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
I mean after publishing I get web.config and appsettings file
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
how would you do in my situation where I need to set some config values but later need to change it but using IIS application settings tab
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
Ok so manually editing appsettings.json can trigger hot reload and the iis works with my new config values right?
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
for say in aws lambda I need to redeploy my function only when I need to change config value in appsettings. So is there any alternative in this case But I want to set env vars in pool and let my webapi to get from there using Environment.GetEnvironmentVariable without IConfiguration. I tried setting it in pool en vars but still not working unless i set it in system level
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
Im REALLY REALLY SORRY Worked on Legacy system for way too long and don't want my peers to be uncomfortable when I'm migrating . Guess we all need to upgrade I'm deeply grateful and Really SORRY for my legacy approach
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
But I need a config value in class library. How to get that
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
M!RFAN
M!RFANOP2mo ago
can I have some example because stackoverflow is mostly about JSONFIle on class Lib
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2mo ago
appsettings.json:
{
"My": {
"Foo": {
"Kix": 5
}
}
}
{
"My": {
"Foo": {
"Kix": 5
}
}
}
src/Foo/FooOptions.cs:
public class FooOptions
{
public const string SectionName = "My:Foo";

public string Bar {get;set;} = "default value for bar";
public int Kix {get;set;} = -1;
public DateTime? Pouet {get;set;} = default;
}
public class FooOptions
{
public const string SectionName = "My:Foo";

public string Bar {get;set;} = "default value for bar";
public int Kix {get;set;} = -1;
public DateTime? Pouet {get;set;} = default;
}
src/Foo/FooServiceCollectionExtensions.cs:
namespace Microsoft.Extensions.DependencyInjection; // <==== recommanded for service.Add so that you don't clutter Startup file

public class FooServiceCollectionExtensions
{
public static IServiceCollection AddFoo(this IServiceCollection services) =>
services
.AddOptions<FooOptions>()
.BindConfiguration(FooOptions.SectionName)
.Validate(options => options.Kix >= 0, $"The configuration key '{FooOptions.SectionName}:{nameof(Kix)}' cannot be negative")
;

public static IServiceCollection AddFoo(this IServiceCollection services, Action<FooOptions> configure) =>
services
.AddFoo()
.Configure(configure);
namespace Microsoft.Extensions.DependencyInjection; // <==== recommanded for service.Add so that you don't clutter Startup file

public class FooServiceCollectionExtensions
{
public static IServiceCollection AddFoo(this IServiceCollection services) =>
services
.AddOptions<FooOptions>()
.BindConfiguration(FooOptions.SectionName)
.Validate(options => options.Kix >= 0, $"The configuration key '{FooOptions.SectionName}:{nameof(Kix)}' cannot be negative")
;

public static IServiceCollection AddFoo(this IServiceCollection services, Action<FooOptions> configure) =>
services
.AddFoo()
.Configure(configure);
Program.cs / Startup.cs:
services.AddFoo();
// or
services.AddFoo(fooOptions => fooOptions.Kix = 12);
services.AddFoo();
// or
services.AddFoo(fooOptions => fooOptions.Kix = 12);
Bar.cs:
public class Bar
{
private readonly FooOptions _fooOptions;

// .Value in ctor is fine only if it's always ever a non-changing value (no reload and/or no scoped resolution)
public Bar(IOptions<FooOptions> fooOptions)
=> _fooOptions = fooOptions.Value;
}
public class Bar
{
private readonly FooOptions _fooOptions;

// .Value in ctor is fine only if it's always ever a non-changing value (no reload and/or no scoped resolution)
public Bar(IOptions<FooOptions> fooOptions)
=> _fooOptions = fooOptions.Value;
}
M!RFAN
M!RFANOP2mo ago
I really need to study this
Want results from more Discord servers?
Add your server