C
C#16mo ago
Shiv

✅ AppContext in web api project

Team, Do we have something like appcontext in .net core web api project where we store all the frequently used objects and all the users need to access the objects(application scope). (To reduce the call to db)
47 Replies
Angius
Angius16mo ago
You mean a cache? Dependency injection? User claims?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Yes , should I go for Redis then? @ZZZZZZZZZZZZZZZZZZZZZZZZZ @TeBeClone
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
One example is some common settings data, I will hit the db once and store in the app scope so every user can access it..
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
the entire app not users
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
not frequently backend data
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Once when we deploy ...not in the middle as of now..
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
yes appsettings is one option..
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX16mo 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;
}
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
But only few settings are fixed like I said above, which can go for config file... I want some more configurable data as well ...which may get changed ocassionally... so thought of db ...
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
IIS
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Inside our own VM
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Yes going through it
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
OK got it
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Should I install Redis as a seperate thing? How to deploy ?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Yes
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius16mo ago
I just use a singleton synchronized with a JSON file ¯\_(ツ)_/¯
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius16mo ago
Well, you can change the value in the singleton live And then save the changes to a file
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius16mo ago
Ah, well, sure you can persist it in a db Can't see why, but yeah
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius16mo ago
I'll take your word for it, since I never had to work with IIS
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
dis advantage means performance wise ?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius16mo ago
Never had any issues ¯\_(ツ)_/¯ Settings are always in memory, in the singleton, and should I need to restart the app they're also persisted in a file
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Yes
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Shiv
ShivOP16mo ago
Got it ... Thanks for the detailed explaination.... learnt many things from your conversation @TeBeClone @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angius16mo ago
For reference, I just have this class injected as a singleton and it works really well so far: https://paste.mod.gg/ikmjgdrhahtu/0
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server